Sign in to follow this  
Naughty_Ottsel

Really Bad coding needs sorting.

Recommended Posts

Naughty_Ottsel    122
Hello all. I am new to C++ and still learning it, when you see this code you will understand. I am learning how to code using Beginning C++ Game Programming and have reached Chapter 3. At the end of Chapter 2 a "Guess my number" game is created with the exercise to create a version where the computer guesses the players number. I have since then done a very crude one and have added it to the original program. But I would like to add a play again feature at 3 different points. 1 for each type of game and one to get back to the menu. What is in the code is basically what I know so far. (Random numbers are so cool) Heres the code: #include <cstdlib> #include <iostream> #include <ctime> using namespace std; int diff; int theNumber; // The random number for the guess the computer game int tries = 0, guess, comptries = 0; //players tries = tries, guess = players guess, comptries= computers tries int choice, game; //choice = N/A, game = game selection char again = 'y', n, answer, y; // again = play again, y/n etc = answers to questions, answer game selection answer int compguess; // computers guess int main() { srand(time(0)); //seed random number generator cout << "Hello and welcome to Guess My Number version 1.5" <<endl; cout << "\nNew to version 1.5 is the option of games" << endl; cout << "To have the computer guess your number press 1" <<endl; cout << "To guess the computers number press 2 "; cin >> game; cin.ignore(); switch (game) // First menu, Game Selection { case 1: //If option 1 is selected cout << "\n\nYou have selected for the computer to guess your number" <<endl; cout << "Please think of a number between 1 and 100, then press enter" <<endl; cin.get(); do { compguess = rand() % 100 +1; comptries ++; cout << "\nIs you number " << compguess << "? Y/N "; cin >> answer; cin.ignore(); if (answer == 'n') cout << "Darn it! :(" <<endl; } while (answer != 'y'); cout << "\nHaha I guessed you number in " << comptries << " tries"; cout << "\n\nPress enter to close, keep an eye out for 1.6" <<endl; break; case 2: // If option 2 is selected cout << "\nYou have selected to guess the computers number" <<endl; do { cout << "\nBefore we begin please choose a difficulty." << endl; cout << "1 = Easy" << endl; cout << "2 = Medium" << endl; cout << "3 = Hard" << endl; cout << "Choice "; cin >> choice; cin.ignore(); switch (choice) // dificulty settings for guessing the computers number { case 1: cout << "My number is between 1 and 100" <<endl; theNumber = rand() % 100 + 1; break; case 2: cout << "My number is between 1 and 1000" <<endl; theNumber = rand() % 1000 + 1; break; case 3: cout << "My Number is between 1 and 10000" << endl; theNumber = rand() % 10000 + 1; break; default: cout << "Please pick a number" << endl; } do // Guessing the computers number { cout << "Enter a guess: "; cin >> guess; cin.ignore(); ++ tries; if (guess > theNumber) cout << "\nYour guess is too high!\n" << endl; if (guess < theNumber) cout << "\nYour guess is too low!\n" <<endl; } while (guess != theNumber); cout << "\n\n\tWell done you have guessed my number! " << endl; cout << "\n\tTries: " << tries; cout << "\n\nDo you want to try again? (y/n) "; cout << "\n\nNote! this will only replay the 2nd option" <<endl; //Play again option with a note. cout << "Hopefully this will be sorted in 1.6, please leave feedback" << endl; cin >> again; cin.ignore(); tries = 0; }while (again == 'y'); cout << "\nGuess again some other time!" << endl; } cin.get(); return 0; }

Share this post


Link to post
Share on other sites
Molle85    172
My suggestion would be to keep reading the book and keep practicing, lend/buy more books to advance in knowledge.
You will soon enough know how to do everything that you have in mind right now.

Share this post


Link to post
Share on other sites
Gage64    1235
A couple of things:

1) Declare your variables just before you are about to use them, not at the top. Read this (the section titled "Defining variables on the fly") to understand why.

2) Some of your comments state the obvious and don't really add any information, so they only clutter the code.

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