using logical operators like and or

I wrote this program using an online compiler i am getting a lot of errors please help

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
string birthmonth;
string birthday;

std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

std::cin >> birthmonth, birthday;

getline(cin, birthmonth,birthday);

if (birthmonth == "October" || "November"
&& birthday <= 23);

std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl;



return 0;
}
Please use code tags when posting code:
http://www.cplusplus.com/articles/jEywvCM9/

First: you are trying to read birthmonth and birthday twice?
For std::string's you should use std::getline() and remember that it can read just one string at a time:

1
2
3
4
// good
std::getline(std::cin, birthmonth);
std::getline(std::cin, birthday);
// bad
std::cin >> birthmonth, birthday;

getline(cin, birthmonth,birthday);


Second: you can't use the logical or operator to "shortcut" the comparison like that.
By doing what you did, the string literal "November" becomes a standalone true condition.

Also you must remove the semicolon after the if(), which counts as an empty instruction (meaning that the std::cout is outside the if() and will always print).

1
2
3
4
// good
if (birthmonth == "October" || birthmonth == "November" && birthday <= 23)
    std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl;


1
2
3
4
5
// bad
if (birthmonth == "October" || "November"
&& birthday <= 23);

std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl;


Edit: also, I just noticed that you're comparing birthday against a number. You can't do that without first converting one to another.
Last edited on
This code contain many errors both compile time and logic error. Also the program forces the user to type a correct choice with may introduce.
Here are a few ways to correct your code
1
2
3
4
5
6
7
8
9
10
11
string birthmonth;
 int birthday;

 std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

 std::cin >> birthmonth >> birthday;

if (birthmonth == "October" || "November" 
 && birthday <= 23);

 std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl; 

or
1
2
3
4
5
6
7
8
9
10
11
string birthmonth;
 string birthday;

 std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

 std::cin >> birthmonth >> birthday;

if (birthmonth == "October" || "November" 
 && birthday <= "23");

 std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl; 

or
1
2
3
4
5
6
7
8
9
10
11
12
string birthmonth;
int birthday;

 std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

getline(cin, birthmonth);
std::cin >> birthday;

if (birthmonth == "October" || "November" 
 && birthday <= 23);

 std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl; 

or
1
2
3
4
5
6
7
8
9
10
11
12
string birthmonth;
string birthday;

 std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

getline(cin, birthmonth);
getline(cin, birthday);

if (birthmonth == "October" || "November" 
 && birthday <= "23");

 std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl; 
Here are a few ways to correct your code


... none of those are correct. Each of them has a misplaced semi-colon on line 10 and improper usage of logical or on the preceding line.
I appreciate the quick response from everybody and the great participation. i have taken everyone's corrections and i am still compiling errors please continue the help


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
string birthmonth;
int birthday;

std::cout << "Please enter your month of birth and day of birth, thank-you" << endl << endl;

std::getline(std::cin, birthmonth);

std::getline(std::cin, birthday);

if (birthmonth == "October" || birthmonth == "November" && birthday <= 23);

std::cout << "Your astrological sign is in the origin of Scorpio" << endl << endl;

return 0;
}
getline only works on strings, not ints. Since, a month may not consist of more than one word, using operator>> would probably be simpler. Btw, the misplaced semi-colon is still there.

std::cin >> birthmonth >> birthday

You have not used code tags.
And you have not told us what the errors are, making us search for them.
You are making it harder for us to help you.

Anyway, the error is caused birthday <= 23 which is a comparison between an std::string and an int.

Since you already have sstream included, I assume that your are expected to extract an int from the birthday string.

1
2
3
4
5
6
std::stringstream temp(birthday);
int n_birthday;

temp >> n_birthday;

if (birthmonth == "October" || birthmonth == "November" && n_birthday <= 23) 


And you still haven't removed the extra semicolon from after the if().

Edit: wait, birthday is int now? Make up your mind!
Last edited on
Topic archived. No new replies allowed.