2 + 2 = 5

me and my friend just read the novel 1984 in english and i want to prank him; in this novel 2 + 2 = 5. heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 2 and 2 is 5

#include<iostream>
using namespace std;

int main()
{
        int a,b,c;
        cout <<"Enter a number: ";
        cin >> a;
        cout <<"Enter a number: ";
        cin >> b;
        if (a=2, b=2){
                        c = 5;
                        cout << a << " + " << b << " = " << c;
                        cout << endl;}
        else {
        c = a + b;
        cout << a << " + " << b << " = " << c;}
        cout << endl;
        system("PAUSE");
        return 0;
}


the problem is that no matter that i type 2 and 2, 5 and 5, watever and whater it always couts "2 + 2 = 5" even though i put the if else in there
Last edited on
what this is supposed to do is act as a simple addition calculator, and is completely normal, until someone enters 2 + 2, then it would say it equals 5, but then go back to being a normal addition calculator, unless of course they add 2 and 2 again.
Last edited on
wouldn't you want line 13 to be:

if (a == 2 && b == 2)

???
Exactly what satchmo05 says. You are assigning values to 'a' and 'b', which will ALWAYS result in true (unless your are replacing it with 0). Change the code to what satchmo05 suggested.
Not only that, but the comma is not an "and" thingy.

No commas there. That should work.
oh and use cin.get(); not system("pause")' because it leaves your code unportable.
when i use cin.get(), the console still closes, idk why, it shouldn't but it does.
Last edited on
It has something to do with a '\n' being left in the input buffer after you use cin>>

If you change your code to:

1
2
3
4
5
6
7
        int a,b,c;
        cout <<"Enter a number: ";
        cin >> a;
        cin.ignore(); // <-- new code here
        cout <<"Enter a number: ";
        cin >> b;
        cin.ignore(); // <-- new code here 


Your program will run as expected.
Last edited on
so, if i use the cin.ignore() with the cin.get() it wont close?
Last edited on
Indeed it won't.
why do i have to use cin.ignore(), what does it do, and why is it necessary for cin.get()
It isn't directly necessary for cin.get(). For example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namesapce std;

int main()
{
   cout << "Hello um0123!";
   
   cin.get();
   return 0;
}


This program uses cin.get() and will pause the program until its gets a character(most often enter). However, it doesn't use cin.ignore() does it.

As i said, its has soemthing to do with a newline, or '\n', being left in the input buffer when you use cin >> a. cin.ignore() takes up to 3 arguments but by default, will just ignore '\n' when it finds it. That's why you have to use it when you ask for both numbers.

Unfortunately I can't tell you why it does this but I would be interested to know why the program does close if you don't use it, if anyone can explain?
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namesapce std;

int main()
{
   int a = 0;
   cout << "Hello world!" << endl << "Enter a number: ";
   cin >> a; //here there is a \n left in the buffer
   cout << "You input: "<<a<<endl;
   cin.get(); //without a cin.ignore(), cin.get() will see the \n and "get" it
   //then the program will end
   return 0;
}
How the pros get input*: http://www.cplusplus.com/forum/articles/6046/



*The real pros avoid getting input at all, and instead use the command line, config files, or a UI of some kind that takes of that.
@firedraco or helios

could you explain what happens here please:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
   int a = 0, b = 0;
   cout << "Hello world!" << endl << "Enter a number: ";
   cin >> a; //here there is a \n left in the buffer
   cout << "You input: "<<a<<endl;
   cout << "\n Enter another number: ";
   cin >> b;
   cout << "You input " <<b<<endl;
   cin.get(); //without a cin.ignore(), cin.get() will see the \n and "get" it
   //then the program will end
   return 0;
}


The program doesn't ignore the '\n' so does it get carried into the b variable? :S
No. This is because cin >> will read everything until the \n and put it into the variable, so in this case, you are putting nothing into the variable (which means it will probably become some garbage value). IIRC how it works anyway.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main()
{
do{
int a, b, c;
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
if(firstNum == 2 && secondNum ==2)
{
 c = 5;
cout<<a<<"+"<<b<<"="<<c<<endl;
}
else 
{
c = a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
}
char rep;
cout<<"Again?(y/n)"<<endl;
cin>>rep;
}while(rep == 'y');

system("pause");
return 0;
}
Topic archived. No new replies allowed.