# Problem with simple program

• 10-29-2005
ajdspud
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.

• 10-29-2005
hk_mp5kpdw
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.
• 10-29-2005
cbastard
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
• 10-29-2005
Dae
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).
• 10-29-2005
dwks
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.
• 10-30-2005
ajdspud
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.
• 10-30-2005
ajdspud
Yep, works perfectly now. Cheers for the help and advice