Need Some C++ help

Started by
5 comments, last by Xai 16 years, 8 months ago
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 =)
Advertisement
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;    }}
Oooh, makes sense. Thx alot =)

oh and how do u make the special box to add code in XD
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 without the spaces. end it with [ / s o r c e ]<br><br>basically just like xml &#111;nly use [] instead of &lt;&gt;.
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 without the spaces. end it with [ / s o r c e ]<br><br>basically just like xml &#111;nly use [] instead of &lt;&gt;.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>ahh thank you =)<br><br>
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. :(
http://neolithic.exofire.net
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! :)

This topic is closed to new replies.

Advertisement