not sure if im right about this but I think its because "return 0" exits the program and "break" exits the loop [in short, even if you choose 1 or 2 they'd still exit the program]. just a suggestion, why not turn exitProgram into an int and change
while (customerMoney >= 59.99)
to
while (customerMoney >= 59.99 && exitProgram == 1)
(note: you'd have to initialize exitProgram to 1 [exitProgram = 1] for it to enter the loop).
Also, I though of some changes that you might like to put in your code. its your choice if you want to do it though.
since your choices are constant (i.e input 1 for product1, 2 for product2, etc...). why not try to use "switch" instead? (just a suggestion though:
http://www.cplusplus.com/doc/tutorial/control/ ). don't know if you know this but if you read the link I gave, its just the same as the if and ifelse statement except the values given are constant (i.e "a == b" only. not "a < b", not "a>b" etc...).
2nd, i've noticed that in evrey "if" there's a "would you like to exit this program?" and so forth in you code. i'd suggest you turn this into a function to lessen the lines.
i've edited your code so that you can understand what "switch" and "functions" mean (just in case if you don't know)
for switch:
http://www.cplusplus.com/doc/tutorial/control/
for functions:
http://www.cplusplus.com/doc/tutorial/functions/
p.s: you can also use switch for your products1,2,3 etc... too
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 <stdlib.h>
using namespace std;
///this is a function
int exitP(int x){
cout<<"\nWould you like to exit the program? [1 -> yes :: 2 -> no]: " ;
cin>>x;
///this is a switch
switch(x){
case 1: cout<<"Goodbye!";
exit(EXIT_SUCCESS);
break;
case 2: return x;
break;
default: cout<<"Pls enter numbers 1-2 only. ";
exitP(x);
break;
}
}
int main(){
double product1 = 59.99;
double product2 = 59.99;
double product3 = 59.99;
double product4 = 59.99;
double customerMoney = 0.00;
int exitProgram = 1;
cout << "How much money do you have? ";
cin >> customerMoney;
while (customerMoney >= 59.99 && exitProgram == 1)
{
int customerChoice = 0;
cout << "Which product would you like to buy?\n Enter '1' for Killzone: Shadow Fall ($59.99)\n Enter '2' for Deadrising 3 ($59.99)\n Enter '3' for Call of Duty Ghosts ($59.99)\n Enter '4' for Madden NFL 25 ($59.99)"
<<"\n\nChoice: ";
cin >> customerChoice;
if (customerChoice == 1)
{
cout << "You have selected Killzone: Shadow Fall\n";
customerMoney = customerMoney - product1;
cout << "You have $" << customerMoney << " left\n";
///this is what a function is used for
exitP(exitProgram);
}
else if (customerChoice == 2)
{
cout << "You have selected Deadrising 3\n";
customerMoney = customerMoney - product2;
cout << "You have $" << customerMoney << " left\n";
///this is what a function is used for
exitP(exitProgram);
}
else if (customerChoice == 3)
{
cout << "You have selected Call of Duty Ghosts\n";
customerMoney = customerMoney - product3;
cout << "You have $" << customerMoney << " left\n";
///this is what a function is used for
exitP(exitProgram);
}
else if (customerChoice == 4)
{
cout << "You have selected Madden NFL 25\n";
customerMoney = customerMoney - product4;
cout << "You have $" << customerMoney << " left\n";
///this is what a function is used for
exitP(exitProgram);
}
}
cout << "You don't have enough money to purchase anything else. Thank you for shopping!";
cin.ignore();
cin.get();
return 0;
}
|