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