Store Program

I'm making a program where it first displays the food items in a .txt file. The user enters the food item name and amount of the food item. The program then outputs the total, tip (15% of total), and grand total (tip + total). I can't enter anything when I run the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
    string Item;
    int Amount = 0;
    double Total = 0;
    double Price = 0.0;
    double Tip = 0.0;
    double GrandTotal = 0;
    ifstream Read;
    
    Read.open("Store.txt", ios::in);
    cout << "Store.txt" << endl;
    
    cout << "Enter the item you want: ";
    getline(Read, Item);
    for(int x = 0; x < 2; x++)
    {
            Read >> Amount >> Price;
            Total = Total + (Amount * Price);
            Tip = Total * .15;
            GrandTotal = Total + Tip;
    }
    
    Read.close();
    cout << "Your total is: $" << Total << endl;
    cout << "Your tip is: $" << Tip << endl;
    cout << "Yout grand total is: $" << GrandTotal << endl << endl;
    cout << "Have a nice day!" << endl;
    
    system("PAUSE");
    return 0;
}


Here is the .txt file:

1
2
3
4
5
Cheeseburger 2.99
French Fries 1.99
Taco 2.50
Pizza 2.50
Salad 1.50


Thanks for helping.
Well, the problem is you never actually get any input from the user, only from a file (on line 20).
Well I updated it a little.

I need help for reading the things in the text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
    string Item;
    int Amount = 0;
    char Another = ' ';
    double Total = 0;
    double Price = 0.0;
    double Tip = 0.0;
    double GrandTotal = 0;
    char we;
    ifstream Read;
    
    Read.open("Store.txt", ios::in);
    
    cout << "MENU" << endl << endl;
    for(int x = 0; x < 5; x++)
    {
             getline(Read, Item, '#');
             Read >> Price;
             cout << Item << " $" << Price;
    }
    
    cout << endl << endl;
    
    Another = 'y';
    while(Another == 'y')
    {
                  cout << "Enter the item you want: ";
                  getline(cin, Item);
                  cout << "Enter the amount of the item: ";
                  cin >> Amount;
                  cout << "Do you want to order another item (y/n)? ";
                  cin >> Another;
                  cin.ignore();
                  
                  Total = Total + (Amount * Price);
                  Tip = Total * 0.15;
                  GrandTotal = Total + Tip;
    }
    
    cout << endl << endl;
    
    Read.close();
    cout << "Your total is: $" << Total << setprecision(5) << endl;
    cout << "Your tip is: $" << Tip << setprecision(3) << endl;
    cout << "Yout grand total is: $" << GrandTotal << setprecision(4) << endl << endl;
    cout << "Have a nice day!" << endl;
    
    system("PAUSE");
    return 0;
}
I would suggest making a function to load the contents of the file into variables. Use getline(file_object, string_name, char_deliminator) to load the item name. i.e.
1
2
3
4
5
ifstream file;
string line;

file.open("file.txt");
getline(file, line, ' '); //extracts chars into string until it hits a space 

I think you can see where I'm going with this :)

EDIT:
maybe not, i just looked at your code more in depth
After you get the string from the file, run it through some conditionals
1
2
if (line=="Cheeseburger")
file >> CBcost; //integer variable 

Just do the same for the rest of the variables. Also, you could use an array and an item# to reduce the amount of code (if statements), but that is a bit more complex.
Last edited on
I didn't really understand that. I can't get the program to read in the price of the item in the .txt file.

Can someone help?

This is due tomorrow so I need some help quickly.
I kinda made an update. It will just end after I enter an item.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
    string Item;
    int Amount = 0;
    char Another = ' ';
    double Total = 0;
    double Price = 0.0;
    double Tip = 0.0;
    double GrandTotal = 0;
    ifstream Read;
    
    Read.open("Store.txt", ios::in);
    
    cout << "MENU" << endl << endl;
    for(int x = 0; x < 5; x++)
    {
             getline(Read, Item, '#');
             Read >> Price;
             cout << Item << " $" << Price;
    }
    
    cout << endl << endl;
    
    Another = 'y';
    while(Another == 'y')
    {
                  cout << "Enter the item you want: ";
                  getline(Read, Item);
                  Read >> Price;
                  cin.ignore();
                  cout << "Do you want to order another item (y/n)? ";
                  cin >> Another;
                  
                  Total = Total + (Amount * Price);
                  Tip = Total * 0.15;
                  GrandTotal = Total + Tip;
    }
    
    cout << endl << endl;
    
    Read.close();
    cout << "Your total is: $" << Total << setprecision(5) << endl;
    cout << "Your tip is: $" << Tip << setprecision(3) << endl;
    cout << "Yout grand total is: $" << GrandTotal << setprecision(4) << endl << endl;
    cout << "Have a nice day!" << endl;
    
    system("PAUSE");
    return 0;
}
I kinda made an update but still not working. This program is due on Friday so hopefully someone can help me with it.

Text File:
1
2
3
4
5
Cheeseburger#2.99
French Fries#1.99
Taco#2.50
Pizza#2.50
Salad#1.50 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
    string Item;
    int Amount = 0;
    char Another = ' ';
    double Total = 0;
    double Price = 0.0;
    double Tip = 0.0;
    double GrandTotal = 0;
    ifstream Read;
    
    Read.open("Store.txt", ios::in);
    
    cout << "MENU" << endl << endl;
    for(int x = 0; x < 5; x++)
    {
             getline(Read, Item, '#');
             Read >> Price;
             cout << Item << " $" << Price;
    }
    
    cout << endl << endl;
    
    Another = 'y';
    while(Another == 'y')
    {
                  cout << "Enter the item you want: ";
                  getline(Read, Item);
                  Read >> Price;
                  cin.ignore();
                  cout << "Do you want to order another item (y/n)? ";
                  cin >> Another;
                  
                  Total = Total + (Amount * Price);
                  Tip = Total * 0.15;
                  GrandTotal = Total + Tip;
    }
    
    cout << endl << endl;
    
    Read.close();
    cout << "Your total is: $" << Total << setprecision(5) << endl;
    cout << "Your tip is: $" << Tip << setprecision(3) << endl;
    cout << "Yout grand total is: $" << GrandTotal << setprecision(4) << endl << endl;
    cout << "Have a nice day!" << endl;
    
    system("PAUSE");
    return 0;
}
My teacher told me to put in cin.ignore() somewhere after getline but I'm not sure where.
Topic archived. No new replies allowed.