Hello All,

I am writing a program to act as a cash register. I reads in from a file that looks like this:

A Cheeseburger 2.97
B Soda 1.90
C Sundae 3.50
...and so on.....
The point is for the user to enter a single letter and then the item and the price appear. The user should be able to keep ordering items until they hit the "=". Then the outer loop stops.

The 1st time through the loop, everything works fine, but after that, the inner loop doesnt work.

Any suggestions???
Thanks,
Jordan

int main()
{
 time_t t;
 time(&t);
 yl;
 cls;
 
 string  order;
 stringstream ssprice;
 double total= 0, pric;
 int count = 0;
 cout <<"J.Maniscalco   9827   Payroll3   " << ctime(&t) << endl << endl;
 print <<"J.Maniscalco   9827   Payroll3   " << ctime(&t) << endl << endl;
  while (! infile)
      {
             cout << "Error opening file  " << endl;
             exit(1);
       }
  while (order != "=")
  {
        cout << "Enter key"  << endl;
        cin >> order;
        string info;
        while (getline (infile, info))
        {
              string name = info;
              string key, item, price;
              key = name.substr(0,1);
              item = name.substr(4,22);
              price = name.substr(27,4);
              if(key == order)
              {
                     cout << item << price << endl;
              }
     }//end while  
  }  
  cout << total << endl;      
       
 frz;
 return 0;
} // end of main function

Recommended Answers

All 3 Replies

>The 1st time through the loop, everything works fine, but after that, the
>inner loop doesnt work.
You reach the end of the file after you run through the loop the first time. The second time you run it, you try to read from where you left off (which is at the end of the file). So you won't get anything.

Instead of constantly reading the file again and again (and resetting the file pointer again and again), why don't you just read it into an array structure when your program starts, and then you just have to search through the array to find the right entry. Better yet (only if you've learned how to do this), make it so you can access the letter through the subscript:

food_item items[128]
// for example: this accesses A's price
cout << items['A'].price << endl;

Is there anyway to do this without an array of structures?
If not....

Can anyone give me a few tips on setting up and executing an array of stuctures.

Thanks again,
Jordan

1. Define a structure.
2. Create an array of structures.
3. Read a line from the file and fill the array accordingly.
4. Accept input from the user.
5. Search the array given the input and pull out the required data.
6. Display the data
7. Repeat steps 4 - 6 till the user enters an '='.

Its pretty simple if you know how to do things. Read up 'struct' in your C++ textbook or search for some online tutorials to help you in that aspect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.