Cannot get correct output from separate file.

Please if possible can anyone improve my code. This project is due in two days time. So basically what I need is:
1. for users to input the item they want and then calculate correct item from menu (seems like it only uses the last item shown, in this case the fanta)
2.Show up in the receipt the items chosen and their corresponding prices
3.If possible is there a way to make this code more user friendly?
4.Also enter users name if possible then print out in receipt

The text file is Store.txt

CheeseBurger#5
DeluxeBurger#12
SmallPizza#7
MediumPizza#12
LargePizza#18
FamilyPizza#27
Coke#3
Sprite#3
Fanta#3

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

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


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 Menu()
{
	Read.open("Store.txt", ios::in);
	
	cout << "\t~PIZZA MENU~\t" << endl;
	cout << "########################" << endl;
    for(double x = 0; x < 9; x++)
    {
             getline(Read, Item, '#');
             Read >> Price;
             cout << Item << " $" << Price;
    }
    cout << endl;
	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()
{
	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 = Total + (Amount * Price);
                  Tip = Total * 0.15;
                  GrandTotal = Total + Tip;
    }
	
}

int main()
{
	Welcome_Message() ;
	Menu();
	Logic();
	Receipt();
	return 0;
}
Last edited on
void Foo();
¿ever heard of parameters?

1
2
3
4
5
6
    for(double x = 0; x < 9; x++)
    {
             getline(Read, Item, '#');
             Read >> Price;
             cout << Item << " $" << Price;
    }
¿why double?
¿do you realise why each item is presented in its own line when you didn't send and end-of-line to cout?


1
2
3
4
cout << "What would you like to order?: ";
getline(Read, Item); //¿?
Read >> Price;
cin >> Item;
¿what do you think you are doing there?
¿how are you getting the price of, say, "Coke"?
There are some good ideas there, but it also seems muddled at times.
When the user is prompted for what they would like to order, the reading of the input file seems out-of sequence and unrelated in any way to what the user wants.

There are a lot of global variables. I would cut those down to the absolute minimum. For example there's no need for char Another = ' '; to be global. Make it a local variable declared inside the function Logic() which is the only place where it is used.


A suggestion.

Read from the file into an array (preferably a vector), to store the name and price of each item.

Use that array to later print out the menu, lookup the price of the item. Also use it to generate the receipt, you could print the name and quantity of each item as well as the cost.

It might be a good idea if the user could enter the number rather than the name of the item they want to order. It would make it possible to directly access the price from the array, whereas if they enter the name, it means having to search through the items one by one, hoping to find a match - assuming the user did not make any spelling mistakes and there's also the need to do a compare while ignoring the capitalisation which is extra effort.


An idea:
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

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};
    
    // insert an empty item, so that numbering can start from 1, not zero.
    menu.push_back(item);
            
    while (getline(read, item.name, '#') && read >> item.price)
    {
        read.ignore(100, '\n'); // ignore till end of current line
        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;    
}

int main()
{
    Welcome_Message() ;
    LoadMenu();    
    Menu();
//    Logic();
//    Receipt();

}

Output:
                HELLO AND WELCOME TO TI.LAUME PIZZERIA!
                ========================================

                THE PLACE WHERE ALL YOUR PIZZA DESIRES
                ARE QUENCHED BY OUR DELICIOUS PIZZAS!
                ========================================


        ~PIZZA MENU~
########################
  1  CheeseBurger   $5
  2  DeluxeBurger   $12
  3  SmallPizza     $7
  4  MediumPizza    $12
  5  LargePizza     $18
  6  FamilyPizza    $27
  7  Coke           $3
  8  Sprite         $3
  9  Fanta          $3
########################


Now, when the user chooses an item, say "DeluxeBurger", they could simply type the number '2' and the program can find the price as menu[2].price and the name as menu[2].name.

Hi thank you so much for your help but i'm having trouble in the receipt section! When the user inputs its choice of item the receipt just displays a total cost of 0. Why? Sorry i'm very new to c++.

Do I have to change something in my Store.txt
> When the user inputs its choice of item the receipt just displays a total
> cost of 0. Why?
>> ¿how are you getting the price of, say, "Coke"?
See that's the problem I know for sure there is a problem within my code but I really need help to identify it and correct it! Please could you help me, i really don't know what to do.
Topic archived. No new replies allowed.