Cash register

Pages: 12
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
 #include <iostream>
using namespace std;
#include <string>

int main()
{
	string Ham = "Ham";
	string Veggie = " Veggie";
	string Rat = " Rat";
	string Bacon = " Bacon";
	string Hair = " Hair";
	cout << "What type of sandwhich do you want?\n";
	string choice = "0";
	cin >> choice;

	if (choice == "Ham")

		cout << "Would you like bacon with that? y/n \n";
	char response;
	cin >> response;

	if (response == 'y')

	{
		cout << "$4.50";
	}
	else {
		cout << "$4";
	}

	
	
	if (choice == "Rat")
		cout << " Would you like bacon or hair with that? y/n";
	cin >> response;
			if (response == 'y')
			{
				cout << "bacon or hair";
				string option;
				cin >> option;
				if (option == "bacon") {
					cout << "$2.50";
				}
				if (option == "hair")
				{
					cout << "$2.10";
				}
			}
			else {
				cout << "$2";
			}

			if (choice == "Veggie")
			{
				cout << "Would you like hair with that? y/n ";
				cin >> response;
				if (response == 'y') {

					cout << "$3.10";
				}
				else {
					cout << "$3";
				}

			}
} 
Hello Notanormie,

you wrote:
I feel my question wasnt answered.

The answer is 42. https://www.youtube.com/watch?v=aboZctrHfK8

Your question has been answered. The problem is that either you are not listing or there is something that you do not understand correctly.

Given your code:
1
2
3
4
5
6
7
8
9
10
string Ham = "Ham";
string Veggie = " Veggie";
string Rat = " Rat";
string Bacon = " Bacon";
string Hair = " Hair";

cout << "What type of sandwich do you want?\n";  // <--- "sandwhich" is misspelled. "sandwich" is correct. I expect it is a language problem. No worries.
cin >> Ham, Veggie, Rat;

if (Ham == "Ham")

Exactly what do you expect line 8 to do?

AbstractionAnon has already commented on the use of the (,)s. When I tested your code only "Ham" received a new value from the "cin" Which means that the following if statement will never be true unless you actually did enter "Ham". A 1 in 3 chance there.

Since the value of the variable "Rat" has not changed that if statement will be true, but that may not be what is wanted.

When you present a menu to choose from the variable that you need to use would be "choice" or "menuChoice" anything but a variable that is initialized to a value and should be a constant variable.

If you had put "const" in front of the variables,
1
2
3
4
5
6
7
const string Ham    = " 1) Ham    - $4.00\n";
const string Veggie = " 2) Veggie - $3.00\n";
const string Rat    = " 3) Rat    - $2.00\n";
const string Bacon  = " Bacon     - $0.50\n";
const string Hair   = " Hair      - $0.10\n";
const std::string Quit{ " 4) Quit\n\n" };
const std::string PROMPT{ "What type of sandwich do you want? " };
, The compiler would have given you an error when you tried to change a constant variable that can not be changed. That is usually a good indication that you need a different variable for input.

If you ignore the advice given or do not ask questions about what you do not understand we can not read your mind to know what you do not understand.

With all the code you have been shown and seeplus's code that has most of your program written far enough that you could finish what he started. Something seems to be amiss.

When you get your program set up right it is very easy to add a "do/while" or "while" loop to allow for ordering more sandwiches.

You could add a menu choice for say "Total" to print a receipt of that order then "Quit" to leave the program. This may not be something that you need now, but may be the subject of the next assignment. If you try to prepare for it now it will be much easier later to modify the program.

Andy
I posted my new code above
Hello Notanormie,

I posted my new code above


Just changing:
1
2
3
string choice = "0";

cin >> choice;

The string is empty when defined initializing it to "0" is just a waste of time and key strokes.

The if statement that follows is only checking for "Ham", but what about "HAM" or "ham"or any other combination of upper case and lower case letters. you are counting on the user to enter the correct name and the correct spelling and that is asking a lot of some people. "choice" work better as an "int" or even a "char" because it is 1 letter and easier to check case of a single "char".

Starting at line 16 none of that will work as is. Even if you get the if statements right there is no guarantee that "choice" will match anything.

Andy
Hello Notanormie,

Before I lost everything I wrote the first time I forgot to mention see the bottom of http://www.cplusplus.com/forum/beginner/278780/#msg1203997

Andy
Mmmm. I could use a Rat sandwich with some hair on it. This feels like a school assignment. When I had taken my C++ class, I had to code a vending machine, so i did. I created a vending machine that vends panties. Lol. Im a dude. My teacher was amused. I got an A+ in that class.
Last edited on
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>
using namespace std;
#include <string>

int main()
{
    const string Ham = " 1) Ham    - $4.00\n";
    const string Veggie = " 2) Veggie - $3.00\n";
    const string Rat = " 3) Rat    - $2.00\n";
    const string Bacon = " Bacon     - $0.50\n";
    const string Hair = " Hair      - $0.10\n";
    int choice;
    char response;

    cout
        <<
        "\n"
        "Sandwichws available:\n"
        "---------------------\n"
        << Ham
        << Veggie
        << Rat
        << "      Extras\n"
        << " -----------------\n"
        << Bacon
        << Hair
        << "What type of sandwhich do you want? ";
    cin >> choice;
    if (choice == 1)
    {
        cout << "Would you like bacon with that? y/n \n";
        cin >> response;
        if (response == 'y')
            cout << "4.50";
        else cout << "$4";
    }
    if (choice == 2)
        cout << "Would you like hair with that? y/n\n";
    cin >> response;
    if (response == 'y')
        cout << "$3.10";
    else cout << "$3";

    if (choice == 3) 
        cout << "Bacon with that? y/n \n";
    if (response == 'y')
        cout << "$2.50";
    else cout << "$2";
    cout << "Would you like hair with that";
    if (response == 'y')
        cout << "$2.10";
    else cout << "$2";
    


}


Im having trouble when entering '3'. The rat sandwich can have bacon and hair
You have no cin >> response; after line 45 or 49.
Last edited on
Got it! finally! Thank you all so much!
Topic archived. No new replies allowed.
Pages: 12