While Loop statement help (not hacking related AT ALL.)
Posts 1–4 of 4 · Page 1 of 1
While Loop statement help (not hacking related AT ALL.)
So, I've decided to create a program. The program will say for start "would you like to buy a bread for 1 coin? <y/n>" and if I select yes, then I buy 1 bread for 1 coin (i have 100 coin and 1 bread for default).
I made this program within 1 minute. It was WAY too easy, so I decided to crank up the heat a little by adding some while loops.
This time, I wanted to add that at the end of purchase, the program will ask, "would you like to buy another bread? <y/n>", then, if I say "yes", then I will buy ANOTHER bread for 1 coin, thus, resulting in 98 coins and 2 breads now. (Hope you understand this). If I select no, then the program will just say "awww that's too bad" then terminate by the "return 0;" statement.
This is my coding.
Code:
#include <iostream>
using namespace std;
int main()
{
int c=100, b=1;
char choice='y';
char AnotherChoice='x';
cout << "Welcome to our store! Would you like to buy a bread for 1 coin? <y/n> ";
cin >> choice;
while(choice=='y')
if(AnotherChoice=='y')
{
c--;
b++;
cout << "Here you go! " << endl;
}
else
{
c=c;
b=b;
cout << "That's too bad. " << endl;
}
cout << "COINS: " << c << endl;
cout << "BREAD: " << b << endl;
cout << endl;
cout << "Would you like to buy another bread for 1 coin? <y/n> ";
system("pause");
return 0;
}
DEBUGGING SCREENSHOTS to prove something is DEFINITELY wrong.
#1
#2 (after clicking "y")
infinite loop? lol
Could someone please tell me what part of coding I'm screwing up at?
In this case @giniyat101
you use two variables, which are choice and AnotherChoice..
you put user input to choice and then test AnotherChoice value (which will remain 'x' ofc)
so this is how to fix:
Code:
int main()
{
int c=100, b=1;
char choice='y';
//char AnotherChoice='x'; //this line is useless.. commented
cout << "Welcome to our store! Would you like to buy a bread for 1 coin? <y/n> ";
cin >> choice;
while(choice=='y' && c >= 1) //something you forgot.. u must have enough money!
{ // you forgot it.. thats why it was outputting the bad message without asking because it loops through the if statement only
//another useless while, it will never step here if it is not y : )
//if(AnotherChoice=='y')
//{
c--;
c++;
cout << "Here you go! " << endl;
//}
cout << "COINS: " << c << endl;
cout << "BREAD: " << b << endl;
cout << endl;
cout << "Would you like to buy another bread for 1 coin? <y/n> ";
//you forgot to ask again too..
cin >> choice;
}
//when user presses something but y the loop will exit and come here
//more useless stuff
//c=c;
//b=b;
cout << "That's too bad. " << endl;
system("pause");
return 0;
}
but i see you are getting better in coding... keep it up @Infractured
[B @giniyat101 omg thanks so much bro!! Exactly what I was looking for! Perfectly detailed and everything!!
I AM really kind of confused about while statements and stuff. The only reason I put "anotherchoice" was because in my program there are 2 statements that ask me yes or no question and they both involve "char".
I fixed my coding with yours and now it works as I wanted
And thanks...yeah I just got into arrays. Kind of confusing, but I hope I will improve [/B]
---------- Post added at 06:03 PM ---------- Previous post was at 05:59 PM ----------
@Hero
close~
Originally Posted by Infractured
[B @giniyat101 omg thanks so much bro!! Exactly what I was looking for! Perfectly detailed and everything!!
I AM really kind of confused about while statements and stuff. The only reason I put "anotherchoice" was because in my program there are 2 statements that ask me yes or no question and they both involve "char".
I fixed my coding with yours and now it works as I wanted
And thanks...yeah I just got into arrays. Kind of confusing, but I hope I will improve [/B]