-4

i need to read a file to my simulation in C++, the file has to be on this format:

# Comments
# Comments
nameOfTheMarket
timeOfSimulation
averageTime
numberOfCashiers
cashierName1 quality1 salary1
cashierName2 quality2 salary2
    -          -       -
cashierNameN qualityN salaryN
#End

It's the first time i deal with files, but i was reading around and tried to do this way:

int main ()
{
string marketName, cashierName;
int counter = 0;
int counter2 = 0;
int simulationTime,averageTime, numberOfCashiers;
string info, test;
ifstream input("/home/meucomputador/workspace/Testezin/src/naocompre.dat");
if(input.is_open()) {
    while(!input.eof()) {
           getline(input,info);
           cout<<info<<endl;
           if(info[0] != '#') {
               counter++;
           }
           switch(counter) {
               case 1:
                   marketName = info;
                   break;
               case 2:
                   stringstream(info) >> simulationTime;
                   break;
               case 3:
                   stringstream(info) >> averageTime;
               break;
               case 4:
                stringstream(info) >> numberOfCashiers;
                   break;
               default:
                   //how to read the 3 information at the same line?
                  for(int i = 0; i < info.length(); i++) {
                      while(info[i] != ' ')
                          counter2++;
                  }
                  cashierName = info.substr(0,counter2+1);

           }
    }

}
    return 0;
};

My difficulty consists on reading the cashier's names , quality and salary. Do i really need to use the "while(info[i] != ' ')"? That's not working.

  • 1
    [`while(!input.eof()) {`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is fundamentally wrong for most cases. Also tell us what you observed when stepping through your code with the debugger. – πάντα ῥεῖ Oct 21 '16 at 18:32
  • while(!input.eof()) { is going ok so far, i get the right data on all switch cases. My problem consists on reading the cashier's names, quality and salary. The code is simply to show how far I am, because i don't know what to do on the default part of the switch – Antonio Gomes Oct 21 '16 at 18:35
  • Again: What did you observe when stepping through your code? – πάντα ῥεῖ Oct 21 '16 at 18:36
  • I get all the correct data when i execute my code, except the default block of the switch, that i did not even executed because i did not have a clue how to implement and get separeted words by space. – Antonio Gomes Oct 21 '16 at 18:40
  • Check `std::istringstream` how to get _words separated by space_ from a line got with `getline()`. – πάντα ῥεῖ Oct 21 '16 at 18:43

1 Answers1

0

instead of info.length() use stoi(numberOfCashiers) in your loop to read the next rows using getline

when you split the string, the are many ways you can do this e.g.

std::istringstream is(input);

std::string cashiername;
int quality=0;  // not sure about the data type u need.
int salary=0;

is >> cashiername >> quality >> salary
AndersK
  • 35,813
  • 6
  • 60
  • 86