Thread: Problem with simple program

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Problem with simple program

    I am very new to C++, Ive only been learning it for the last week. Anyway Ive written this simple program that should create a number guessing game with player 1 and 2. The use can input the number of rounds they wish to play then at the end of each guess the winner of that round (the one with the the number closest to the random number) should be displayed. At the end the program should display the winner, how many rounds they won, and how many points they beat the other player by.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    typedef unsigned short int USHORT;
    
    USHORT Game(USHORT, USHORT);
    
    int main()
    {
      USHORT rnd_amount;
      USHORT P1_guess;
      USHORT P2_guess;
      USHORT result;
      USHORT wincnt_P1;
      USHORT wincnt_P2;
      USHORT winner;
    
      std::cout << "Welcome to the guess the number game.\n\n";
      std::cout << "You can input the number of rounds you wish to play, whoever wins the most rounds wins the game.\n\n";
      std::cout << "A random number will be generated by the computer. Each player has attempt to guess the number.\n\n";
      std::cout << "The winner of the round is the person with the guess closest to the generated number\n\n";
      std::cout << "\n\n\n\nPlease enter the number of rounds you wish to play";
      std::cin >> rnd_amount;
      std::cout << "\n\nTHE GAME HAS BEGUN";
    
      USHORT rnd_counter;
      wincnt_P1 = 0;
      wincnt_P2 = 0;
      for (rnd_counter = 0; rnd_counter < rnd_amount; rnd_counter++)
      {
        std::cout << "\n\nThis is round: " << rnd_counter + 1;
        std::cout << "\n\nPlease enter your guess Player 1:";
        std::cin >> P1_guess;
        std::cout << "\n\nPlease enter your guess Player 2:";
        std::cin >> P2_guess;
        result = Game(P1_guess, P2_guess);
        switch (result)
        {
        case 1:
          std::cout << "\n\nThe winner of this round is Player 1";
          wincnt_P1++;
          break;
        case 2:
          std::cout << "The winner of this round is Player 2";
          wincnt_P2++;
          break;
        case 3:
          std::cout << "This round was a draw";
          break;
        }
    
      }
    
      std::cout << "\n\nIt is the end of the game!";
      std::cout << "\n\nPlayer 1, you won " << wincnt_P1 << "games!";
      std::cout << "\n\nPlayer 2, you won " << wincnt_P2 << "games!";
      winner = 0;
      if (wincnt_P2 < wincnt_P1)
      { winner = 1; }
      if (wincnt_P1 < wincnt_P2)
      { winner = 2;  }
      else
      { winner = 3;}
    
      switch (winner)
      {
        case 1:
          std::cout << "\n\nPlayer 1, you have won this game with " << wincnt_P1 << " points. You beat player 2 by " << (wincnt_P1 - wincnt_P2) << " points";
          break;
        case 2:
          std::cout << "\n\nPlayer 2, you have won this game with " << wincnt_P2 << " points. You beat player 1 by " << (wincnt_P2 - wincnt_P1) << " points";
          break;
        case 3:
          std::cout << "\n\nNeither player won the game, it was a draw.";
          break;
      }
    
      std::cout << "\n\nThank you for playing the number guessing game! Please play again";
      std::cout << "\n\n\n\n\n\nThe Number Guessing Game v1.0. Built by Andrew";
    
      int nil;
      std::cout << ".  ";
      std::cin >> nil;
    
      return 0;
    }
    
    USHORT Game(USHORT P1_guess, USHORT P2_guess)
    {
      srand( (unsigned)time( NULL ) );
      USHORT random = (rand() % 10) + 1;
      USHORT P1_result;
      USHORT P2_result;
      USHORT z;
      P1_result = 0;
      P2_result = 0;
    
      if (P1_guess < random)
      {    P1_result = random - P1_guess;  }
      else
      {    P1_result = P1_guess - random;  };
    
      if (P2_guess < random)
      {    P2_result = random - P2_guess;  }
      else
      {    P2_result = P2_guess - random;  };
    
      z = 0;
      if (P2_result < P1_result)
      { z = 2; }
      if (P1_result < P2_result)
      { z = 1; }
      else (P2_result == P1_result);
      { z = 3; };
    
    return z;
    }
    It will compile but returns some warnings about variables in the Game function being assigned values and never used.

    When I run the program it simply says the round is a draw whatever number is input.

    I've been over the code but my skills with C++ are pretty limited and I can't work out where I have gone wrong.

    Any help would be much appreciated.

    Thanks in advance.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    winner = 0;
    if (wincnt_P2 < wincnt_P1)
    { winner = 1; }
    if (wincnt_P1 < wincnt_P2)
    { winner = 2;  }
    else
    { winner = 3;}
    This code does not do what you think it does. It will end up setting winner to either 2 or 3 and never 1. You probably meant for it to be like this:

    Code:
    winner = 0;
    if (wincnt_P2 < wincnt_P1)
    { winner = 1; }
    else if (wincnt_P1 < wincnt_P2)
    { winner = 2;  }
    else
    { winner = 3;}

    Code:
    USHORT Game(USHORT P1_guess, USHORT P2_guess)
    {
      srand( (unsigned)time( NULL ) );
      USHORT random = (rand() % 10) + 1;
      USHORT P1_result;
      USHORT P2_result;
      USHORT z;
      P1_result = 0;
      P2_result = 0;
    
      if (P1_guess < random)
      {    P1_result = random - P1_guess;  }
      else
      {    P1_result = P1_guess - random;  };
    
      if (P2_guess < random)
      {    P2_result = random - P2_guess;  }
      else
      {    P2_result = P2_guess - random;  };
    
      z = 0;
      if (P2_result < P1_result)
      { z = 2; }
      if (P1_result < P2_result)
      { z = 1; }
      else (P2_result == P1_result);
      { z = 3; };
    
    return z;
    }
    You have some unnecessary semicolons in the above code. The one after the else (P2_result == P1_result) line in particular results in the variable z always getting the value of 3 which means that your program always reports the particular round as being a draw. Also, you should only call srand once within your program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
     if (wincnt_P2 < wincnt_P1)
      { winner = 1; }
      if (wincnt_P1 < wincnt_P2)
      { winner = 2;  }
      else
      { winner = 3;}
    If there are multiple if-else statements.The last else is combined with the If just above it.So even if your first condition is true and winner becomes 1.it will be changed to winner=3
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
      if (P2_result < P1_result)
      { z = 2; }
      if (P1_result < P2_result)
      { z = 1; }
      else (P2_result == P1_result);
      { z = 3; };
    should be:
    Code:
      if (P2_result < P1_result)
        z = 2;
      else if (P1_result < P2_result)
        z = 1;
      else if (P2_result == P1_result)
        z = 3;
    If you are checking for more situations then you need it to be 'else if', and if you use 'if' then 'if' and 'else', then the first if may get processed, and then the next 'else' too, so make it 'if' 'else if' 'else if' (and 'else' too if you want, only one of those statements will get processed). The semicolon on the last 'else' wasn't necessary, and after the curly bracer, and probably caused an error. I also took out the curly bracers because they were annoying.. if the if/for/while loop only has 1 line then you can omit the bracers.

    That may not be the only problem, if they continue you might want to post the EXACT error (copy and paste).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Great minds think alike.

    Code:
      int nil;
      std::cout << ".  ";
      std::cin >> nil;
    You don't need to read in to a dummy variable. You can use
    Code:
    cin.ignore();
    cin.get();
    or something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Thanks for the help everyone, ill re-write bits of the program with the advice you have given me and see what helps. Ive looked throught the book I'm using and it doesnt actually mention else if statements at all, it just asumes all if statements will have one if and an else, therefor i assumed multiple if's would be fine.

    As for hk_mp5kpdw comment about the overuse of semicolons, I thought that they were wrong to, but for some reason the program wouldnt even compile unless they were all there, which i found rather strange.

    Thanks, and ill post again if there are still problems.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Yep, works perfectly now. Cheers for the help and advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM