Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Trying to compile a text game with little success

Currently I trying to make a simple lemonade stand game that encompasses five days, Although I would be happy to get just the first day to work. The program compiles past the the supermarket portion and then goes into a repitious loop asking how many bags of sugar do you want. My outlook is that I think I first need to redefine the 'for' statement mentioned prior to the supermarket and then do something with the 'while' statements associated with shopping. I am in a C++ College class, and this was just something extra I decided to do as a challenge. The actual asignment we were given was to give an example of functional decomposition of a problem. I decided not only to give the example but write a program. Also for only being in the class for 10 weeks so far and no other experience I think I did an alright job with typing this up from scratch, minus my errors. Currently getting no errors/warnings just the program stopping at the line asking how many bags of sugar you would like. Anyways apoligize for posting the whole code, but thought it might help grab the entire gist of what I am trying to accomplish in the end.

Thanks for any help

Jon

<pre>

#include <iostream>

using namespace std;

int main()

{
int d1;
int samt, lamt, camt, pamt;
int sugst, lemst, cupst, pitst;
int pitchers;
float bank, stot, c1, s1, bankst;
char mat, prod;

cout << "Welcome to the game of Lemonade" << endl;
cout << "" << endl;
cout << "Directions:" << endl;
cout << "1. The game can be played in five day increments." << endl;
cout << "2. You currently have $10 dollars in start up money for your stand." << endl;
cout << "3. See how much money you can make and good luck." << endl;
cout << "" << endl;
cout << "How many days would you like to sell lemonade for (pick 1 thru 5)? ";
cin >> d1;

while (d1 <= 5);
do
{
switch (d1)
{
case 1:
cout << "You have chosen to play for 1 day " << endl;
break;

case 2:
cout << "You have chosen to play for 2 day's " << endl;
break;

case 3:
cout << "You have chosen to play for 3 day's " << endl;
break;

case 4:
cout << "You have chosen to play for 4 day's " << endl;
break;

case 5:
cout << "You have chosen to play for 5 day's " << endl;
break;

default:
cout << "The number " << d1 << "is not valid. " << endl;
cout << "Please choose again. " << endl;
break;
}
}

cout << "Would you like to shop for materials? (Enter y or n) ";
cin >> mat;

if ((mat == 'N') || (mat == 'n')) cout << "You have no supplies to make lemonade." << endl;
break;
if ((mat == 'Y') || (mat == 'y'))

for (mat=1;mat<=1;mat++)
{
cout << "" << endl;
cout << "Welcome to the supermarket." << endl;
cout << "" << endl;
cout << "1. Here is the list of items you may buy." << endl;
cout << "2. Select a number and then select the" << endl;
cout << " amount you want to buy." << endl;
cout << "" << endl;
cout << "(1) Sugar @ 20 Cents per bag. " << endl;
cout << " 1 bag will make 1 pitcher " << endl;
cout << "" << endl;
cout << "(2) Lemon @ 1 Dollar per bag. " << endl;
cout << " 1 bag will make 1 pitcher " << endl;
cout << "" << endl;
cout << "(3) Cups @ 10 Cents per package. " << endl;
cout << " 1 Package contains 20 cups and" << endl;
cout << " will be enough for 1 pitcher" << endl;
cout << "" << endl;
cout << "(4) Pitcher @ 1 Dollar per pitcher. " << endl;
cout << " The more pitchers you have, the " << endl;
cout << " more money you can make" << endl;
cout << "" << endl;
cout << "Select a product by entering 1, 2, 3, or 4: ";
cin >> prod;
}

bank = 10;

while (prod = 1)
{
cout << "" << endl;
cout << "How many bags of SUGAR would you like? ";
cin >> samt;

sugst = samt * .20;
{
if (sugst > bank)
cout << "You dont have enough money. Please select another amount." << endl;
cout << "How many bags would you like? ";
cin >> samt;

if (sugst < bank)
cout << "Would you like to purchase another product? (Enter y or n)";
cin >> mat;
}

}

while (prod = 2)
{
cout << "" << endl;
cout << "How many bags of LEMONS would you like? ";
cin >> lamt;

lemst = lamt * 1;
{
if (lemst > bank)
cout << "You dont have enough money. Please select another amount." << endl;
cout << "How many bags of LEMONS would you like? ";
cin >> lamt;

if (lemst < bank)
cout << "Would you like to purchase another product? (Enter y or n)";
cin >> mat;
}
}

while (prod = 3)
{
cout << "" << endl;
cout << "How many packages of CUPS would you like? ";
cin >> camt;

cupst = camt * .10;
{
if (cupst > bank)
cout << "You dont have enough money. Please select another amount." << endl;
cout << "How many packages of CUPS would you like? ";
cin >> camt;

if (cupst < bank)
cout << "Would you like to purchase another product? (Enter y or n)";
cin >> mat;
}
}

while (prod = 4)
{
cout << "" << endl;
cout << "How many PITCHERS would you like? ";
cin >> pamt;

pitst = pamt * 1;
{
if (pitst > bank)
cout << "You dont have enough money. Please select another amount." << endl;
cout << "How many bags would you like? ";
cin >> pamt;

if (pitst < bank)
cout << "Would you like to purchase another product? (Enter y or n)";
cin >> mat;
}
}

bankst = sugst+lemst+cupst+pitst;
bank = bank - bankst;

cout << "You can proceed to your Lemonade Stand.";
cout << "";
cout << "You have purchased the following supplies ";
cout << samt << "bag(s) of sugar ";
cout << lamt << "bag(s) of lemons ";
cout << camt << "package(s) of cups ";
cout << pamt << "pitcher(s) ";

stot = (samt >= 1, lamt >= 1, camt >= 1, pamt >= 1);
cout << "Lets go make some lemonade. ";
cout << "";
cout << "Welcome to day 1 ";
cout << "";
cout << "How many pitchers would you like to make? (enter 1 thru 5) ";
cin >> pitchers;

while (pitchers == 1)
{
if (samt >= 1 && lamt >= 1 && camt >= 1 && pamt >= 1)
cout << "You have made one pitcher of lemonade. ";

}

while (pitchers == 2)
{
if (samt >= 2 && lamt >= 2 && camt >= 2 && pamt >= 2)
cout << "You have made two pitchers of lemonade. ";
if (samt <= 2 && lamt <= 2 && camt <= 2 && pamt <= 2)
cout << "You must pick another amount. ";
}

while (pitchers == 3)
{
if (samt >= 3 && lamt >= 3 && camt >= 3 && pamt >= 3)
cout << "You have made three pitchers of lemonade. ";
if (samt <= 3 && lamt <= 3 && camt <= 3 && pamt <= 3)
cout << "You must pick another amount. ";
}

while (pitchers == 4)
{
if (samt >= 4 && lamt >= 4 && camt >= 4 && pamt >= 4)
cout << "You have made four pitchers of lemonade. ";
if (samt <= 4 && lamt <= 4 && camt <= 4 && pamt <= 4)
cout << "You must pick another amount. ";
}

while (pitchers == 5)
{
if (samt >= 5 && lamt >= 5 && camt >= 5 && pamt >= 5)
cout << "You have made five pitchers of lemonade. ";
if (samt <= 5 && lamt <= 5 && camt <= 5 && pamt <= 5)
cout << "You must pick another amount. ";
}

c1 = (pitchers*20);
s1 = (pitchers*5);
bank = (bank+s1);

cout << "Day 1 is now finished. ";
cout << "Your amounts made for the day where: ";
cout << "";
cout << "Pitchers made = " << pitchers;
cout << "Cups sold = " << c1;
cout << "Total sales = $" << s1;
cout << "You now have $", bank , "in your bank account" ;

return 0;
}
</pre>
[9588 byte] By [jarienks] at [2007-11-11 7:17:50]
# 1 Re: Trying to compile a text game with little success
First, if you can run it, it compiled.
Second, you have a while loop over "prod" that does not modify "prod".
Prod MUST change to escape this loop!
I think you messed with "mat" when you meant to mess with "prod" ??
jonnin at 2007-11-11 21:02:28 >
# 2 Re: Trying to compile a text game with little success
Thanks for the reply on this, I have actually started this out from scratch again and now basically going thru this line by line, compiling it, and ensuring that it runs. I have split the first section of cout statements into two seperate string statements, so that I may call these up at any time.

[html]<pre> string days "How many days would you like to sell lemonade for (pick 1 thru 5)? ";
cout << days;
cin >> d1;

if (d1 == 1) {cout << "sell for 1 day";}
else if (d1 == 2) {cout << "sell for 2 days";}
else if...
else (d1 > 5 ) {cout << "select another number";}

<\pre>[\HTML]

I have turned this part into a string that asks how many days and then my cin statement references d1. Then I have a changed the switch statement to an if statement, not that this matters because both would work the same, Its just that my limited knowledge makes it easier if I use an if statement. However when I type in a number other than 1 thru 5 it should loop back to the original question which it does not. So what function would I put in place that would allow for it to go back to that question.

I think once I figure that out I can then I can move on to the shopping list portion of the program.
jarienks at 2007-11-11 21:03:33 >
# 3 Re: Trying to compile a text game with little success
int d1 = 100; //invalid to start, so we enter our loop --

string days "How many days would you like to sell lemonade for (pick 1 thru 5)? ";

while(d1 > 5)
{
cout << days;
cin >> d1;

if (d1 == 1)
{cout << "sell for 1 day";}
else if (d1 == 2) {cout << "sell for 2 days";}
else if...
else (d1 > 5 ) {cout << "select another number";}
} //end while
jonnin at 2007-11-11 21:04:32 >