-
my game wont continue
im new to C++ and this is a practice i found in a book and im having an issue
the goal of the game is for the computer to hit 0 before the user by subtracting either 1 or 2 at a time
what it is supposed to do is take a number the user inputs and subtract either 1 or 2 from it
then allow the user to input a number and have the computer take that number from the total post it and then subtract another number
BUT it isnt accepting the users input for n to subtract from total what am i doing wrong
im really starting to doubt this book too lol
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int total, n;
cout << "Welcome to Hell. Wanna play?" << endl;
cout << " Pick a number any number I can handle it." << endl;
cin >> total;
while(true) {
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 ........a." << 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 >> n;
while (n < 1 || n > 2)
cout << "Hey I thought I told you 1 or 2 try again numskull " << endl;
cin >> n;
total = total -n;
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 what a shocker you won. Good job you beat something that can't even think on its own" << endl;
break;
}
system("PAUSE");
return 0;
}}
-
Fix your indentation and you will spot the problem. More specifically, it is not right to end two scopes on the same line.
Perfect indentation is not a luxury, it is essential; a top priority when writing code. It's not something you fixup just when you feel like it. A real programmer has it right always.
-
well i haven't been taught anything about indentation and this book doesn't mention it im starting from scratch with 0 prior knowledge im just trying to do things that kinda make it easyer for me to read
-
But BURST, Malc just gave you a good advice. Read up on indentation. It's a little bit like when you write text, you follow certain rules to make the text readable. (Then on top of that you've got grammar, spelling etc.)
-
-
Thank you i worked on the indentation and found it >.<
but now im also trying to make this same program start itself over if the user wants it too
something like
Code:
int again
while (again == y){
cout << "do you want to play again (y/n)" << endl
cin >> again;
}
but im not sure exactly how to put this into the code
-
this is what i have come up with but it doesnt work it asks for the first input to total then it just says press any key to continue what am i doing wrong?
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int total, again, n1, y = 1, n = 2;
cout << "Welcome to Hell. Wanna play?" << endl;
cout << " Pick a number any number I can handle it." << endl;
cin >> total;
while ( again == 1){
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 ........a." << 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;
}
-
got it to work thanks for all the help