#include %26lt;iostream%26gt;
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
using namespace std;
int main()
{
int guess, actual;
srand(rand());
actual = rand() % 10 + 1;
cout %26lt;%26lt; "Guess a number between 1 and 10: ";
cin %26gt;%26gt; guess;
if (guess = actual) cout %26lt;%26lt; "Congrats!!";
else if (guess /= actual) cout %26lt;%26lt; "Incorrect. The correct number is " %26lt;%26lt; actual %26lt;%26lt; ".";
return 0;
}
The program is supposed to generate a random number and determine whether your guess is right or not but every guess I enter is determined to be correct. What is wrong?
What is wrong with my c++ program?
You are using only one '=' in your condition statement rather than '=='. Essentially what is happening is you are assigning the value of actual to guess rather than comparing the values. That assignment operation returns a non 0 value, so the condition is evaluated as true
Change the line
if(guess = actual) ....
to
if(guess == actual) ...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment