Sign in to follow this  
skippydahobo

Need Some C++ help

Recommended Posts

Hey all *waves* , well i'm new to C++ and i've been dabbling in it from time to time but now i'm trying to get more into it. I've been working on a simple guess my number game , but i've run into a problem while revising it so the player can pick the number and the computer guesses it. Ill link my code here and my errors. #include <iostream> #include <cstdlib> #include <ctime> #include <windows.h> using namespace std; int main() { system("TITLE Guess My Number v1.1"); system("color 9"); int gametype; cout << "\tWelcome to Guess My Number v1.1\n"<<endl; cout << "Please choose a game type:\n\n"; cout << "1.Computer picks number and you guess.\n2.You pick a number and the computer guesses.\n\n"; cin >> gametype; switch(gametype) { case 1: cout << "Alright the computer will pick a number and you have to guess it.\n\n"; Sleep(1000); srand(time(0)); //seed random number generator int theNumber = rand() %100 +1; // random number between 1 and 100 int tries = 0, guess; int score = 100; //The Guessing Loop do { cout << "Enter a guess between 1 and 100: "; cin >> guess; ++tries; --score; if (guess < 1 || guess > 100) { cout << "Please enter a number between 1 and 100.\n\n"; --tries; ++score; continue; } if (guess > theNumber) cout << "Too high!\n\n"; if (guess < theNumber) cout << "Too low!\n\n"; } while (guess != theNumber); cout << "\nThat's it! You got it in " << tries<< " guesses! Your score is " << score << "!\n\n"; break; case 2: cout << "Ok, You pick a number and the Computer will try and guess it.\n\n"; Sleep(2000); srand(time(0)); int playerNumber; int computerTries = 0; int computerGuess = rand() %100 +1; cout << "Enter a number between 1 and 100: "; cin >> playerNumber; do { cout << "The computers guess is: " << computerGuess <<endl; } while (computerGuess != playerNumber); cout << "The computer guessed your number in " << computerTries << " guesses.\n\n"; break; } system("pause"); return 0; } and my errors are c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\GuessMyNumber1.1\main.cpp(67): error C2360: initialization of 'tries' is skipped by 'case' label c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\GuessMyNumber1.1\main.cpp(34) : see declaration of 'tries' and i got that same error for my other to variables that are listed in case 1 =/ thx for looking at this and helping if you do =)

Share this post


Link to post
Share on other sites
Any time you initialize a variable within a switch case, you need to surround it with curly braces:


switch(gametype){
case 2:{
cout << "Ok, You pick a number and the Computer will try and guess it.\n\n";
Sleep(2000);
srand(time(0));

int playerNumber;
int computerTries = 0;
int computerGuess = rand() %100 +1;

cout << "Enter a number between 1 and 100: ";
cin >> playerNumber;

do {
cout << "The computers guess is: " << computerGuess <<endl;
} while (computerGuess != playerNumber);
cout << "The computer guessed your number in " << computerTries << " guesses.\n\n";
break;
}
}

Share this post


Link to post
Share on other sites
any time you want to see the gamedev markup for something, just click "edit" on the post you want to view, and it will show you the markup (of course you can't save the edit without the original poster's password).

in this case the answer is put the source tag in square brackets [ s o r c e ] without the spaces. end it with [ / s o r c e ]

basically just like xml only use [] instead of <>.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
any time you want to see the gamedev markup for something, just click "edit" on the post you want to view, and it will show you the markup (of course you can't save the edit without the original poster's password).

in this case the answer is put the source tag in square brackets [ s o r c e ] without the spaces. end it with [ / s o r c e ]

basically just like xml only use [] instead of <>.


ahh thank you =)

Share this post


Link to post
Share on other sites
Except it's spelled "source," not "sorce." ^_^

Unless I suck at reading, which is entirely possible considering I'm not wearing any corrective lenses at the moment. :(

Share this post


Link to post
Share on other sites
Quote:
Original post by violentcrayon
Except it's spelled "source," not "sorce." ^_^

Unless I suck at reading, which is entirely possible considering I'm not wearing any corrective lenses at the moment. :(


whoops! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this