So just trying to create a simple menu as part of another program, It says there are no errors when I build it however when I try execute the code and type in an option It comes up that there's an error.
#include <iostream>
int main()
{
int itemNum;
int numItems = 0;
double price = 0.0;
do
{
std::cout << "Press 1 for mars bar - 2.50\n"
<< "press 2 for crisps - 1.20\n"
<< "press 3 for coke - 1.00\n"
<< "press 4 for ice cream - 4.10\n"
<< "press 5 for pizza - 4.30\n"
<< "press 6 for chocolate\n"
<< "press 0 to check out\n: ";
std::cin >> itemNum;
switch (itemNum)
{
case 1:
std::cout << "Buying a Mars Bar.\n";
price += 2.50;
numItems++;
break;
case 2:
std::cout << "Buying Crisps.\n";
price += 1.20;
numItems++;
break;
case 3:
std::cout << "Buying Coke.\n";
price += 1.00;
numItems++;
break;
case 4:
std::cout << "Buying Ice Cream.\n";
price += 4.10;
numItems++;
break;
case 5:
std::cout << "Buying Pizza.\n";
price += 4.30;
numItems++;
break;
case 6:
std::cout << "Buying Chocolate.\n";
price += 1.30;
numItems++;
break;
case 0: // exiting
break;
default:
std::cout << "This is not a valid option please try again...\n";
}
} while (itemNum != 0); // originally had wrong test condition, would exit on neg nums, ooops!
std::cout << "Number of items = " << numItems << std::endl;
std::cout << "Price = " << price << std::endl;
}
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 7
This is not a valid option please try again...
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 1
Buying a Mars Bar.
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 3
Buying Coke.
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 0
Number of items = 2
Price = 3.5
"closed account", did you bother to actually compile your code before you post? If you did you'd find your "solutions" don't work.
Please note your and kemort's while test condition is correct while (x != 0). My original one, while(x > 0), didn't work properly for negative numbers.
Why is my solution not working? while(itemNum < 0 || itemNum > 6);
This loops whatever itemNum is lower than zero or itemNum is greater than 6 (invalid itemNum)
Perhaps itemNum is a std::size_t?
And :
1 2
default:
if(itemNum != 0) cout << "This is not a valid option please try again" << endl;
Because it should loop until the user enters zero.
This code is perfectly legal.
Perhaps but it would be better to have a case statement for all legal entries, including zero. The only time the default statement should execute is when an illegal entry is made.
#include <iostream>
int main()
{
int itemNum;
int numItems = 0;
double price = 0.0;
do
{
std::cout << "Press 1 for mars bar - 2.50\n"
<< "press 2 for crisps - 1.20\n"
<< "press 3 for coke - 1.00\n"
<< "press 4 for ice cream - 4.10\n"
<< "press 5 for pizza - 4.30\n"
<< "press 6 for chocolate\n"
<< "press 0 to check out\n: ";
std::cin >> itemNum;
switch (itemNum)
{
case 1:
std::cout << "Buying a Mars Bar.\n";
price += 2.50;
numItems++;
break;
case 2:
std::cout << "Buying Crisps.\n";
price += 1.20;
numItems++;
break;
case 3:
std::cout << "Buying Coke.\n";
price += 1.00;
numItems++;
break;
case 4:
std::cout << "Buying Ice Cream.\n";
price += 4.10;
numItems++;
break;
case 5:
std::cout << "Buying Pizza.\n";
price += 4.30;
numItems++;
break;
case 6:
std::cout << "Buying Chocolate.\n";
price += 1.30;
numItems++;
break;
case 0: // exiting
break;
default:
std::cout << "This is not a valid option please try again...\n";
}
} while(itemNum < 0 || itemNum > 6);
std::cout << "Number of items = " << numItems << std::endl;
std::cout << "Price = " << price << std::endl;
}
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 1
Buying a Mars Bar.
Number of items = 1
Price = 2.5
THAT'S why it doesn't work, a user can only purchase ONE item.
This code is perfectly legal.
Yes it is, but it still is bad code that doesn't work properly.