# Thread: Why doesn't it work?

1. ## Why doesn't it work?

Why doesn't the following code work?

#include <iostream.h>
int main()
{
int favnum;
int nxt;
cout<<"enter a number from 1-100";
cin>>favnum;
cout<<"you entered: "<<favnum
cout<<"now type either a 1 or 2";
cin>>nxt;
if (nxt==1, nxt==2)
{
if (nxt==1)
{
cout<<"your number is 1";
}
else if (nxt==2)
{
cout<<"your number is 2";
}
else
{
cout<<"hey, you didn't type 1 or 2! meanie!!!!";
}
}

2. It just needed a little tweeking...
Code:
```#include <iostream>
using namespace std;

int main()
{
int favnum;
int nxt;

cout << "enter a number from 1-100: ";
cin >> favnum;
cout << "you entered: " << favnum << endl;

cout << "now type either a 1 or 2";
cin >> nxt;

if ( nxt == 1 )
{
cout << "your number is 1" << endl;
}
else if (nxt == 2 )
{
cout << "your number is 2" << endl;
}
else
{
cout << "hey, you didn't type 1 or 2! meanie!!!!" << endl;
}

return 0;
}```
I'll let you find the changes it will be a good exercise.

3. One more:

#include <iostream.h>
int main()
{
int favnum;
int nxt;
cout<<"enter a number from 1-100";
cin>>favnum;
cout<<"you entered: "<< favnum;
cout<<"now type either a 1 \or 2";
cin>>nxt;
if (nxt==1, nxt==2)
{
if (nxt==1)
{
cout<<"your number is 1";
}
else if (nxt==2)
{
cout<<"your number is 2";
}
else
{
cout<<"hey, you didn't type 1 or 2! meanie!!!!";
}
}

Take a look at this

Popular pages Recent additions