c++ - Comparing numbers in strings to numbers in int? -


i'm trying make program open txt file containing list of names in format (ignore bullets):

  • 3 mark
  • 4 ralph
  • 1 ed
  • 2 kevin

and create file w/ organized names based on number in front of them:

  • 1 ed
  • 2 kevin
  • 3 mark
  • 4 ralph

i think i'm experiencing trouble in line 40, try compare numbers stored in strings number stored in int.

i can't think of other way tackle this, advice wonderful!

#include <iostream>  #include <fstream>  #include <vector>  #include <cstdlib>    using namespace std;    int main()  {      ifstream in;      ofstream out;      string line;      string collection[5];      vector <string> lines;      vector <string> newlines;      in.open("infile.txt");      if (in.fail())      {          cout << "input file opening failed. \n";          exit(1);      }      out.open("outfile.txt");      if (out.fail())      {          cout << "output file opening failed. \n";          exit(1);      }        while (!in.eof())      {          getline(in, line);          lines.push_back(line);      }    (int = 0; < lines.size(); i++)      {         collection[i] = lines[i];      }    (int j = 0; j < lines.size(); j++)      {          (int x = 0; x < lines.size(); x--)          {              if (collection[x][0] == j)              newlines.push_back(collection[x]);          }      }      (int k = 0; k < newlines.size(); k++)  {      out << newlines[k] << endl;  }    in.close( );  out.close( );    return 0;  }

using debugger tell went wrong, let me highlight mistake:

if (collection[x][0] == j) 

you're expecting string 3 mark. first character of string '3', has ascii value of 51, , numerical value you'll when trying work way! never equal j, unless you've got lot of lines in file, , search system not work @ wanted. need convert character integer, , comparison.

c++ offers many way process data via streams, including parsing simple datafiles , converting text numbers , vice versa. here's simple standalone function read datafile have (only arbitrary text including spaces after number on each line).

#include <algorithm>  // snip  struct file_entry { int i; std::string text; };  std::vector<file_entry> parse_file(std::istream& in) {     std::vector<file_entry> data;      while (!in.eof())     {       file_entry e;        in >> e.i; // read first number on line       e.ignore(); // skip space between number , text       std::getline(in, e.text); // read whole of rest of line        data.push_back(e);     }      return data; } 

because standard way >> works involves reading until next space (or end of line), if want read chunk of text contains whitespace, easier use std::getline slurp whole of rest of current line.

note: i've made no attempt handle malformed lines in textfile, or number of other possible error conditions. writing proper file parser outside of scope of question, there plenty of tutorials out there on using c++'s stream functionality appropriately.

now have file in convenient structure, can use other standard c++ features sort it, rather reinventing wheel , trying yourself:

int sort_file_entry(file_entry a, file_entry b) {     return a.i < b.i; }  int main() {     // set streams, etc.      std::vector<file_entry> lines = parse_file(in);      std::sort(lines.begin(), lines.end(), sort_file_entry);      // can write sorted vector out disk. } 

again, full introduction how iterators , containers work outside scope of answer, internet has no shortage of introductory c++ guides out there. luck!


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -