Thread: my game wont continue

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7

    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;
    }}

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7
    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

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    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.)

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7
    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

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7
    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;
    }

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7
    got it to work thanks for all the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My game wont clear screen
    By dreadfear in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2008, 03:15 AM
  2. Continue
    By Livijn in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2007, 07:25 AM
  3. Please any key to continue...
    By clement28 in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2003, 04:19 AM
  4. continue
    By lithium in forum C Programming
    Replies: 4
    Last Post: 01-25-2003, 10:34 PM