Cash register

Pages: 12
WHy doesnt it go into the if statement block of code?

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

int main()
{
	string Ham = "Ham - $4";
	string Veggie = " Veggie - $3";
	string Rat = " Rat - $2";
	string Bacon = " Bacon - $.50";
	string Hair = " Hair - $.10";
	cout << "What type of sandwhich do you want?\n";
	cin >> Ham;
	
	if (cin >> Ham)
	{
		cout << "Would you like bacon with that?Y/N\n";
		char response;
		cin >> response;
		if (response == 'Y')
		{
			cout << "$4.50";
		}
		else {
			cout << "$4";
		}
		}
	}

First ask the user what kind of sandwich they want: Ham, Veggie, or Rat.
Depending on what kind of sandwich they want, ask what toppings they want:
Only ham and rat sandwiches can have bacon. Only rat and veggie can have hair.
Display the final price of the sandwich they made.
Why doesn't it go into the if statement block of code?

Which if block are you referring to?

You won't enter the if block at line 16 until you have entered two values for ham.

Lines 13,15: Both lines wipe out the literal value you established at line 7.

Lines 7-11 should be const

Line 15: Did you mean?
 
    if (ham == "HAM")


If you meant the if block at line 21, you will enter it only if you enter response in upper case.

Last edited on

You won't enter the if block at line 16 until you have entered two values for ham.

Yes I noticed that but why?
Line 13 and line 15 are both input statements requiring a value be entered.
Notice the >> on both lines?
How do I make it so I only have to enter a single value such as "Ham" and then jump into line 15
Hello Notanormie,

Give this a look and some thought. Reworked the code, but have not tested it yet.
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string Ham = "Ham - $4";  // <--- Is there a point to this? I do not see its use yet.
    string Veggie = " Veggie - $3";
    string Rat = " Rat - $2";
    string Bacon = " Bacon - $.50";
    string Hair = " Hair - $.10";
    std::string choice;  // <--- Added.

    cout << "What type of sandwich do you want?\n";  // <--- Corrected spelling.
    cin >> choice;

    if (choice == "Ham")
    {
        cout << "Would you like bacon with that?Y/N\n";

        char response;
        cin >> response;

        if (response == 'Y')  // <--- What about 'y'??
        {
            cout << "$4.50";
        }
        else
        {
            cout << "$4";
        }
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

That is a clean up of your cede, but not the best way to go about it for the whole program.

Andy

Edit:
Last edited on
I added string choice and changed choice to == ham; when I ran it , the first character I entered closed the window. Back to my own code, I just need it to read in one value and then jump to line 15
Nvm I got it too work Ill post more later if I have more troubles.
Hello Notanormie,

Here is another option for your program. This has been tested and runs.
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
#include <iostream>
#include <string>
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.

using namespace std;  // <--- Best not to use.

int main()
{
    std::string sandwiches[]
    {
        "",  // <--- Place holder not used.
        "Ham    - $4",
        "Veggie - $3",
        "Rat    - $2"
    };

    string Bacon = " Bacon - $.50";
    string Hair = " Hair - $.10";
    unsigned int choice{};  // <--- Added.

    cout << "What type of sandwich do you want?\n";

    for (unsigned idx = 1; idx < sizeof(sandwiches) / sizeof(sandwiches[0]); idx++)
    {
        std::cout << ' ' << idx << ". " << sandwiches[idx] << '\n';
    }

    std::cout << "  Enter choice: ";
    cin >> choice;

    if (choice == 1)
    {
        cout << "\n Would you like bacon with that? (Y/N): ";

        char response;
        cin >> response;


        if (static_cast<char>(std::toupper(static_cast<unsigned char>(response))) == 'Y')  // <--- What about 'y'??
        {
            cout << "\n Your sandwich cost is Ham with bacon    $4.50";
        }
        else
        {
            cout << "\n Your sandwich cost is Ham    $4.00";
        }
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}


Andy
L23. As sandwiches is an array and not a degraded pointer, you can use std::size() to obtain the number of elements.

PS What's a 'hair sandwich' ?
Something you have with a Rat sandwich.
Simply, perhaps consider:

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

int main()
{
	std::cout << "Ham - $4\n"
		<< "Veggie - $3\n"
		<< "Rat - $2\n\n"
		<< "Bacon - $.50\n"
		<< "Hair - $.10\n\n";

	std::string sand;
	double cost {};

	std::cout << "What type of sandwich do you want (ham/veg/rat): ";
	std::cin >> sand;

	if (sand == "ham")
		cost = 4;
	else if (sand == "veg")
		cost = 3;
	else
		if (sand == "rat")
			cost = 2;
		else
			return (std::cout << "Invalid sandwich\n"), 1;

	if (sand == "ham" || sand == "rat") {
		std::cout << "Would you like bacon with that (Y/N): ";

		char response;
		std::cin >> response;

		if (response == 'Y' || response == 'y')
			cost += .5;
	}

	if (sand == "rat" || sand == "veg") {
		std::cout << "Would you like hair with that (Y/N): ";

		char response;
		std::cin >> response;

		if (response == 'Y' || response == 'y')
			cost += .1;
	}

	std::cout << "The cost is $" << cost << '\n';
}

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
  

#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";
	cin >> Ham, Veggie, Rat;

	if (Ham == "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 (Rat == "Rat")
		cout << " Would you like bacon or hair with that? y/n";
	cin >> response;
			if (response == 'y')
			{
				cout << "bacon or hair";
				string choice;
				cin >> choice;

			}
			else {
				cout << "$2";
			}
}



How come I have to enter two values in line 16
And how how come it then prints two prices
Last edited on
Line 15: The comma operator doesn't do what you think it does here.
https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator

BTW, it's poor practice to also use string literals (lines 8-11) as input variables.
What if you want to add a loop to your program so it can handle more than one transaction?
Your string literals will have been wiped out.
As I suggested before, lines 9-13 should be const.
Hello Notanormie,

Your first code that you posted is bad, but this is worse:

You need to pay attention to what AbstractionAnon has said.

You start by defining variables then ask what sandwich they would like, but any given user will have no idea what is available.

You want something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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? ";  // <--- Changed. Removed the (\n).
    cin >> choice; // Ham, Veggie, Rat; 


This will produce:

Sandwichws available:
---------------------
 1) Ham    - $4.00
 2) Veggie - $3.00
 3) Rat    - $2.00
      Extras
 -----------------
 Bacon     - $0.50
 Hair      - $0.10
What type of sandwhich do you want?


By giving the sandwiches a number this makes it easier to get your input anf with your if statements.

Your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (Ham == "Ham")  // <--- The variable "Ham" will have its value changed. Has a 1 in 3 chance of being true.
    cout << "Would you like bacon with that? y/n \n";  // <--- Only line that is part of the if statement.
                                                 //^ Needs a (: ) or something else. The (:) is your choice. Could be something else.

// <--- These next lines happen if the if statement is true or false. Not what you want.
char response;  // <--- These next lines are not part of the if statement. In the end should not be defined here.
cin >> response;

if (response == 'y')

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


What follows line 24 in the 1st code does not work.

Part of it even though I made some changes it still does not work properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//if (choice == 1)
if (choice == "Ham" || choice == "HAM" || choice == "ham" || ...)
    cout << "Would you like bacon with that? y/n: ";  // <--- Only line that is part of the if statement.


    cin >> response;  // <--- Next lines not part of the if statement.

    if (response == 'y')
    {
        cout << "$4.50";
    }
}
else if (choice == Ham)
{
    cout << "$4";
}

Line 1 is what you should be doing and line 2 is what you need to do if you use a string. Even with every combination of upper and lower case letters thought of you still could miss something.

And think of how long that if statement would be for "veggie".

You are better off defining "choice" as an "int" and input a number. But by doing this it creates another problem that needs to be dealt with. So you should do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const std::string PROMPT{ "What type of sandwich do you want? " };  // <--- Added with the other variables

// after the menu prints.

while (std::cout << PROMPT && !(std::cin >> choice) || choice < 1 || choice > 3)
{
    if (!std::cin)
    {
        std::cerr << "\n     Invalid Input! Must be a number.\n\n";

        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    }
    else if (choice < 1 || choice > 3)
    {
        std::cerr << "\n     Invalid Choice! Choose 1, 2 or 3\n\n";
    }
}

That is a good start for your program if you like the way it comes out.

The if statements that follow need to be completely reworked to work correctly.
1
2
3
4
5
6
7
8
9
10
11
12
if choice == 1)
{
    // <--- All the code needed to process this choice.
}
else if ( choice == 2)
{
    // <--- All the code needed to process this choice.
}
else if ( choice == 3)
{
    // <--- All the code needed to process this choice.
}

Although for what is there a switch would be a good choice if you have studied that.

Andy
See mine previous. Doesn't use number entry but does check valid input. Also a rat sandwich can have optional both bacon and hair extras. That's why I used a running total for the sandwich. Using a number is good idea. So consider (without much input checking):

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

int main()
{
	std::cout << "1) Ham - $4\n"
		<< "2) Veggie - $3\n"
		<< "3) Rat - $2\n\n"
		<< "Bacon - $.50\n"
		<< "Hair - $.10\n\n";

	unsigned sand {};
	double cost {};

	std::cout << "What type of sandwich do you want (1 - 3): ";
	std::cin >> sand;

	if (sand == 1)
		cost = 4;
	else if (sand == 2)
		cost = 3;
	else
		if (sand == 3)
			cost = 2;
		else
			return (std::cout << "Invalid sandwich\n"), 1;

	if (sand == 1 || sand == 3) {
		std::cout << "Would you like bacon with that (Y/N): ";

		char response;
		std::cin >> response;

		if (response == 'Y' || response == 'y')
			cost += .5;
	}

	if (sand == 3 || sand == 2) {
		std::cout << "Would you like hair with that (Y/N): ";

		char response;
		std::cin >> response;

		if (response == 'Y' || response == 'y')
			cost += .1;
	}

	std::cout << "The cost is $" << cost << '\n';
}



I feel my question wasnt answered. Why does it do the first if loop if I enter "Ham" but if I enter Rat it will not work and instead must take in a second value such as "Ham" for it to launch into the following if statement
I answered that question in my first post in this thread.
http://www.cplusplus.com/forum/beginner/278780/#msg1203817
See also:
http://www.cplusplus.com/forum/beginner/278780/#msg1203820

Unsure what your code looks like now, so can only answer the question with respect to your original post.

Last edited on
Pages: 12