It's okay. It's a very easy mistake for a person new to C++ to make.
Printable View
It's okay. It's a very easy mistake for a person new to C++ to make.
This code will never execute. You might want to put the else back in there like you originally had it.Code:if (false) {
cout << "Way off";
}
Code:else {
cout << "Way off";
}
OK i got it, thanks all!!!!!!
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();
}
Or you could put:
You might want to put a loop in there to give the user multiple guesses:Code:if(nunb!=20)
{
cout<<"Way off!";
}
Code:#include <iostream>
using namespace std;
int main()
{
int nunb;
bool guess = false;
while(guess!=true){
cout << "Number:";
cin>> nunb;
cin.ignore();
if ( nunb == 20 ){
cout << "You read my mind!";
guess = true;
}
if (nunb!=20) {
cout << "Way off"<<endl;
}
}
cin.get();
}
No offense, but some of this doesn't make any sense.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();
}
This statement will NEVER be evaluated. "false" will NEVER evaluate to true and therefore the contents of the if statement will never be executed.Code:if (false) {
cout << "Way off";
}
Try this:
else {
cout << "Way off";
}
-Edit-
Sorry about that, I was slow to respond! Old news - move along, nothing to see here :)