if statements not working?

I'm trying to figure out how I'm supposed to include the string into the if statement. In this, my latest, attempt the random generation works but no matter what number I input after that the result is always "Right!". How am I supposed to write the if statement so it functions properly?


#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main()
{
string p[7];
int num;
srand((unsigned)time(0));

p[1] = "George Washington";
p[2] = "John Adams";
p[3] = "Thomas Jefferson";
p[4] = "James Madison";
p[5] = "James Monroe";
p[6] = "John Quincy Adams";

cout << "\nEnter the order number for each of the following presidents:";

cout << endl << p[1 + rand() % 6] << ": ";
cin >> num;

if(("George Washington" && num == 1) || ("John Adams" && num == 2) || ("Thomas Jefferson" && num == 3) || ("James Madison" && num == 4)
|| ("James Monroe" && num == 5) || ("John Quincy Adams" && num == 6))
{
cout << "Right!";
}

else if(("George Washington" && num != 1) || ("John Adams" && num !=2) || ("Thomas Jefferson" && num != 3) ||
("James Madison" && num != 4) || ("James Monroe" && num != 5) || ("John Quincy Adams" && num != 6))
{
cout << "Wrong!";
}


cout << endl;


system("pause");
return 0;
}
You're not comparing those string literals to anything, so they're considered non-zero and therefore always true.
Last edited on
How would I do that?
Here's the pseudo-code:

1.) Accept user input number
2.) Does the user's number match the index of the randomly selected string array index?
3.) If yes, print "success"
4.) Otherwise, print "Wrong"


You're over-complicating things.
Topic archived. No new replies allowed.