I am trying to get my program to repeat itself only if the user wants it to
this is what i have come up with so far but it doesnt work it gets to the first input for total then just says press any key to continue then it closes
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int main(){
int total, again, n1, y, n;
cout << "Welcome to Hell. Wanna play?" << endl;
cout << " Pick a number any number I can handle it." << endl;
cin >> total;
while (total < 1) {
cout << " need a number greater then 0" << endl;
cin >> total;
}
while ( again == y){
while(total > 0) {
if ((total % 3) == 2){
total = total - 2;
cout << "I'll take 2 whether you like it or not. =P" << endl;
} else {
total--;
cout << " I'm taking one mo fucka." << endl;
}
cout << " Only " << total << " left. Can you even count that high?" << endl;
if (total == 0) {
cout << " HAHAHA I WIN I WIN I WIN !!!!!!!!!!!!!!!!" << endl;
break;
}
cout << "Pick a 1 or 2 I promise it doesn't matter though I'll still win ";
cin >> n1;
while (n1 < 1 || n1 > 2){
cout << "Hey I thought I told you 1 or 2 try again numskull " << endl;
cin >> n1;
}
total = total - n1;
cout << " Okay we are at " << total << " Good job you made me do the math for you now lets move on" << endl;
if (total == 0) {
cout << " Wow whats a shocker you won. Good job you beat something that can't even think on its own" << endl;
break;
}
}
cout << " wanna go again? y/n" <<endl;
cin >> again;
}
system ("PAUSE");
return 0;
}
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int main(){
int total, n1, n;
char again = 'y';
while ( again == 'y'){
cout << "Welcome to Hell. Wanna play?" << endl;
cout << " Pick a number any number I can handle it." << endl;
cin >> total;
while (total < 1) {
cout << " need a number greater then 0" << endl;
cin >> total;
}
while(total > 0) {
if ((total % 3) == 2){
total = total - 2;
cout << "I'll take 2 whether you like it or not. =P" << endl;
} else {
total--;
cout << " I'm taking one mo fucka." << endl;
}
cout << " Only " << total << " left. Can you even count that high?" << endl;
if (total == 0) {
cout << " HAHAHA I WIN I WIN I WIN !!!!!!!!!!!!!!!!" << endl;
break;
}
cout << "Pick a 1 or 2 I promise it doesn't matter though I'll still win ";
cin >> n1;
while (n1 < 1 || n1 > 2){
cout << "Hey I thought I told you 1 or 2 try again numskull " << endl;
cin >> n1;
}
total = total - n1;
cout << " Okay we are at " << total << " Good job you made me do the math for you now lets move on" << endl;
if (total == 0) {
cout << " Wow whats a shocker you won. Good job you beat something that can't even think on its own" << endl;
break;
}
}
cout << " wanna go again? y/n" <<endl;
cin >> again;
}
system ("PAUSE");
return 0;
}
You declared them, but did not assign them an initial value, where you wrote, while (again==y), the compiler was checking if the values held by the variables were equal. You had not assigned either a value, until at the end cin >> again. But you asked for a y or n, to be stored in a variable which can only hold numbers.
By initialize I just mean.
1 2 3 4 5 6
int y;
y=1;
or int y=1;
or int y=1,x=2;
The values of again, and y, were probably 0 by default.
wow umm some of that stuff seems a bit out of my reach atm im just starting do u think my book will go over something like that later or should it have done it when i first started working on this project?