Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I'm stuck: "Write a program that takes a date as input and outputs the date's season."

+7 votes
asked Sep 18, 2020 by Evie Sarthias (190 points)

I am extremely new to C++ and am completely at a loss for how to fix this issue. I keep getting the following error: main.cpp:12:19: error: no match for ‘operator==’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘int’)

only the error is a LOT longer because it's saying this error for every == >= and <=. I have spent hours trying to figure out to fix this, but I just can't seem to figure it out.

The full problem is: 

Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day.

Ex: If the input is: April 11

the output is: Spring

In addition, check if the string and int are valid (an actual month and day).

Ex: If the input is: Blue 63

the output is: Invalid

My code is:

#include <iostream>
#include <string>
using namespace std;

int main() {
   string inputMonth;
   int inputDay;
   
   cin >> inputMonth;
   cin >> inputDay;
   
   if (inputMonth == 12 && inputDay >= 21) {
      cout << "Winter" << endl;
   }
   else if (inputMonth <= 3 && inputDay <= 19) {
      cout << "Winter" << endl;
   }
   else if (inputMonth >= 3 || inputMonth <= 6 && inputDay >= 20) {
      cout << "Spring" << endl;
   }
   else if (inputMonth >= 6 || inputMonth <= 9 && inputDay >= 21) {
      cout << "Autumn" << endl;
   }
   else if (inputMonth >= 9 && inputDay >= 22 || inputMonth == 12 && inputDay <= 20) {
      cout << "Winter" << endl;
   }
   else {
      cout << "Invalid" << endl;
}
   return 0;
}

1 Answer

+1 vote
answered Sep 19, 2020 by xDELLx (10,500 points)
edited Sep 20, 2020 by xDELLx


First , if the compiler throws an error , it doesnt mean you have to check each & every line of the errors.
The compiler is just showing you what all options it evaluated before flagging the code as error.Compiler tries its best to make your code work by typecasting the types implicitly if we have not given a explicit method to do so.

The input suggested in the problem statement doesnt match the logic in your code,so I modifed it a bit.If both the user inputs are ints , just changing "string inputMonth;" to "int inputMonth;" will make your code working .

The problem with your code is you're trying to comparing equaltiy of strings(inputMonth) & int (12,9,3,6 from ur code)

no match for ‘operator==’ (operand types are ‘std::string’ {aka‘std::__cxx11::basic_string<char>’} and ‘int’)

So, now the error by compiler hopefully makes sense

   string inputMonth;
   ..... // other lines of ur code
   if (inputMonth == 12 && .... // other of ur code

Compiler tried to implicitly typecast string to int ,which is shown in the error messages.When it cant find a suitable candidate, your code is flagged as error.

//FIX:

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
   string inputMonth;
   std::map<string,int > string2int_month_map = {
    {"January",1},
    {"Feb",2},
    {"Mar",3},
    //... other months .. too lazy to populate the map, bailing out
    {"Dec",12} 
   } ;
   int inputDay;

   cin >> inputMonth;
   cin >> inputDay;

    if (string2int_month_map[inputMonth] == 12 && inputDay >= 21) {
      cout << "Winter" << endl;
   }
   if (string2int_month_map[inputMonth] == 12 && inputDay >= 21) {
      cout << "Winter" << endl;
   }
   else if (string2int_month_map[inputMonth] <= 3 && inputDay <= 19) {
      cout << "Winter" << endl;
   }
#if 0   // block commented ,to stop compile error
   else if (inputMonth >= 3 || inputMonth <= 6 && inputDay >= 20) {
      cout << "Spring" << endl;
   }
   else if (inputMonth >= 6 || inputMonth <= 9 && inputDay >= 21) {
      cout << "Autumn" << endl;
   }
   else if (inputMonth >= 9 && inputDay >= 22 || inputMonth == 12 && inputDay <= 20) {
      cout << "Winter" << endl;
   }
   else {
      cout << "Invalid" << endl;
}
#endif
   return 0;
}
/*
Output
Mar                                                                                                                          
10                                                                                                                           
Winter

*/
commented Nov 22, 2020 by xDELLx (10,500 points)
.../*
Output
Mar                                                                                                                          
10                                                                                                                           
Winter

*/
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...