General

Chat

C Quicky

(Title was supposed to say C++, only says C)

#include<iostream>
using namespace std;

int main()
{ int a;
int b;
int c;
cout << "Please enter an integer value for a,b, and c." << endl;
cin >> a >> b >> c;
cout << "You have entered " << a << ", " << b << ", and " << c << endl;
if (a != 7) {
if (b == 5) {
cout << "Case 1" << endl;
} else if (b != 5) {
if (c == 4) {
cout << "Case 2" << endl;
}
} else if (c != 4){
cout << "Case 3" << endl;
} else if (a == 7){
cout << "Case 4" <<endl;
}
}
return 0;
}

Trying to get it to print Case 1, 2, 3, or 4 for "If a is not equal to 7 and b is equal to 5 then print case 1, but if b is not equal to 5 then if c is equal to 4 print case 2, but if c is not equal to 4 print case 3. If a is equal to 7 then print case 4." The cin is working and it takes your input and prints it back out, but it won't go into the if/else statements and print out the case numbers. Anyone know how to fix this? I'm a bit short on time and this is frustrating me. Thanks.

September 25, 2013

4 Comments • Newest first

NoNsensical

[quote=voyance]Okay, I fired up Visual Studio just now. Wrote this code in like 5mins.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{ int a, b, c;

cout << "Enter 3 numbers: ";
cin >> a >> b >> c;

cout << endl << "Your inputs are: " << a << ", " << b << ", " << c << endl;

if(a !=7 && b == 5)
cout << "Case 1" << endl;
else if(b != 5 && c == 4)
cout << "Case 2" << endl;
else if(c != 4)
cout << "Case 3" << endl;
else
cout << "Case 4" << endl;
cout << "Press any key to continue.n";
_getch();
return 0;
}

Enjoy...[/quote]
Wow thanks! You saved my life just now. Locking this thread now.

Reply September 25, 2013
voyance

Okay, I fired up Visual Studio just now. Wrote this code in like 5mins.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{ int a, b, c;

cout << "Enter 3 numbers: ";
cin >> a >> b >> c;

cout << endl << "Your inputs are: " << a << ", " << b << ", " << c << endl;

if(a !=7 && b == 5)
cout << "Case 1" << endl;
else if(b != 5 && c == 4)
cout << "Case 2" << endl;
else if(c != 4)
cout << "Case 3" << endl;
else
cout << "Case 4" << endl;
cout << "Press any key to continue.n";
_getch();
return 0;
}

Enjoy...

Reply September 25, 2013
Suryoyo

i only work with python unfortunate,
but maybe [url=http://www.reddit.com/r/programming]this[/url] site can help you,
there are more links in the right description

Reply September 25, 2013
voyance

if(a != 7 && b == 5)
cout << "Case 1";
else if(b != 5 && c == 4)
cout << "Case 2";
else if(c != 4)
cout << "Case 3";
else if(a == 7)
cout << "Case 4";...

What is supposed to happen is none of the conditions are met?
Also, your input is taken in and displayed back out because you have a cout statement just after the cin. So, that's totally normal based on your code.

Don't take my word on anything I wrote here too seriously. I haven't fired up Visual Studio or Notepad++ for like a year now!

Reply September 25, 2013 - edited