Cannot read from a text file!

I've posted my code before but i'm still having problems. Well first the program does not read from the Store.txt file that holds the name and price of each item. What I just want is for the program to read the prices and calculate the right total price and then print it out in the receipt. Any ideas anyone?
Oh and also I would like to thank chervil for helping me in last post!

Store.txt
1
2
3
4
5
6
7
8
9
CheeseBurger#5
DeluxeBurger#12
SmallPizza#7
MediumPizza#12
LargePizza#18
FamilyPizza#27
Coke#2.50
Sprite#2.50
Fanta#2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

ifstream Read;
string Item;
int Amount = 1;
double Total = 0;
double Price = 0.0;
double Tip = 0.0;
double GrandTotal = 0;

struct Menuitem 
{
    string name;
    double Price;
};

vector <Menuitem> menu;

void Welcome_Message()
{
    cout << "\n\t\tHELLO AND WELCOME TO TI.LAUME PIZZERIA!\t\t\n";
    cout << "\t\t========================================\t\t" << endl;
    cout << "\n\t\tTHE PLACE WHERE ALL YOUR PIZZA DESIRES\t\t\n";
    cout << "\t\tARE QUENCHED BY OUR DELICIOUS PIZZAS!\t\t" << endl;
    cout << "\t\t========================================\t\t" << endl;
    cout << endl << endl;    
}

void LoadMenu()
{
    ifstream Read("store.txt");
    Menuitem Item = {"", 0};
    
    menu.push_back(Item);
            
    while (getline(Read, Item.name, '#') && Read >> Item.Price)
    {
        Read.ignore(100, '\n'); 
        menu.push_back(Item);
    }
    
}

void Menu()
{
    cout << "\t~PIZZA MENU~\t" << endl;
    cout << "########################" << endl;

    for (size_t i = 1; i<menu.size(); ++i)    
    {
        cout << "  " << i << "  "
             << left  << setw(15) << menu[i].name
             << '$' << setw(2) << menu[i].Price
             << '\n'; 
    }
    cout << "########################" << endl;    
}
void Receipt()
{
	cout << endl << endl;
	cout << "\n=========================================\n";
	cout << "\tReceipt\t";
    Read.close();
    cout << "\n-----------------------------------------\n";
    cout << "Your total is: $" << Total << setprecision(5) << endl;
    cout << "Your tip is: $" << Tip << setprecision(3) << endl;
    cout << "\n----------------------------\n";
    cout << "Yout grand total is: $" << GrandTotal << setprecision(4) << endl;
    cout << "----------------------------";
    cout << endl ;
    cout << "Thank You for choosing Ti Laume Pizzeria!" << endl;
    cout << "\n-----------------------------------------\n";
    cout << endl;
    
}

void Logic()
{
	char Another = ' ';
	Another = 'y';
    while(Another == 'y')
    {
    			cout << endl;
                  cout << "What would you like to order?: ";
                  getline(Read, Item);
                  Read >> Price;
                  cin >> Item;
                  cout << "Please enter the amount of the Item you intend to purchase: ";
                  cin >> Amount;
                  cout << "Do you want to order another Item (y/n)? ";
                  cin >> Another;
                  
                  Total = Amount * Price;
                  Tip = Total * 0.15;
                  GrandTotal = Total + Tip;
    }
	
}
int main()
{
    Welcome_Message() ;
    LoadMenu();    
    Menu();
    Logic();
    Receipt();

}
Last edited on
When I copy youvr program and store.txt, it displays the menu fine. Make sure that you're storing the menu in the same directory as where the program runs. If necessary, give a fill path to store.

You aren't using the global Read variable. Remove it (line 9) and everywhere it's used (lines 69, 91 & 92)

Line 93: You should read the item as a number, not a string. So change Item to an int or (better yet) an unsigned. You don't want a string because...

Line 99: You need to compute the price of the item by looking it up in the menu vector. Since you're now reading Item as an integer, menu[Item] contains the info for the item that they ordered. Use that to get the price.

Make those changes and it that much working.

Once you have this working, consider the receipt. Right now it just gives the total + tip. Shouldn't it be itemized, showing the name, price, quantity and subtotal for each item?
Sorry i'm new to c++. Would it be possible for you to correct my code and then show me the source code please?
Last edited on
Topic archived. No new replies allowed.