-
Look what i did!!
I made a really simple program, but i did it without cheating! :D
Code:
#include <iostream>
using namespace std;
int main()
{
int nunb;
cout << "Number:";
cin>> nunb;
cin.ignore();
if ( nunb = 20){
cout << "You read my mind!";
}
else {
cout << "Way off";
}
cin.get();
}
-
This is actually starting to make a little sense
-
Actually, there is an error. In the if() statement, you need to change = to ==. In C++, = is an assignment operator therefore you will assign variable values with that operator. However, == is a comparative operator, it will tell you if both sides are equals.
Code:
int var = 0; // Assign var the value 0
// check if var is equal to
if(var == 0)
{
/* ... */
}
-
-
Try inputting anything else than 20 and you will see.
-
Let me guess you typed in 20 and it said you read my mind. Try typing in any number, I guarantee you it will still say that you read my mind.
The way the original program is written the if statement is checking the return from the assignment which will always be true. Thus whether you type in 2 or 20 or 200 or 2000 you will always get 'you read my mind'.
-
The only case where it will not is if you type = 0 (false == 0).
-
-
-
Oh, I get what you are saying. You mean if the assignment statement you set the value to 0. Well in that case you are right. My bad, I thought you meant typing in 0 for a guess.
-
Sorry, I'm not a native englophone (capital 'e' ?) so I often make mistakes that change the meaning of my words.
-
-
-
-
Wait it didn't work, i hit enter and it just adds a line
Code:
#include <iostream>
using namespace std;
int main()
{
int nunb;
cout << "Number:";
cin>> nunb;
cin.ignore();
if ( nunb == 20 ){
cout << "You read my mind!";
}
if (false) {
cout << "Way off";
}
cin.get();
}