2

What do I have to use to store months as a variable? When I run the code it says "warning character constant too long for its type".

I have created a program to check how many days are in the given month so I take a value from the user and put it into m. I have also tried taking the value of m as (character) but it's not working. I check if the month is Feb or another month. If it is Feb, then it will ask to enter the year, y, and if it is not Feb then it will match the value of m within all conditions and output the month, m.

#include<iostream>
    using namespace std;
    int main(){
    char m;
    int y;
    cout<<"Plase Enter The month:- ";
    cin>>m;
    if (m=='january'||m=='march'||m=='april'||m=='may'||m=='june'||m=='july'||m=='august'||m=='september'||m=='december'||m=='october'||m=='november'||m=='jan'||m=='mar'||m=='apr'||m=='may'||m=='jun'||m=='jul'||m=='aug'||m=='sep'||m=='dec'||m=='oct'||m=='nov'||m=='JANUARY'||m=='MARCH'||m=='APRIL'||m=='MAY'||m=='JUNE'||m=='JULY'||m=='AUGUST'||m=='SEPTEMBER'||m=='OCTOBER'||m=='NOVEMBER'||m=='DECEMBER')
    {
        if (m=='jan'||m=='january'||m=='JANUARY')
        {
            cout<<"There Are 31";
        }
        else if (m=='march'||m=='mar'||m=='MARCH')
        {
            cout<<"There Are 31";
        }
        else if (m=='apr'||m=='april'||m=='APRIL')
        {
            cout<<"There Are 30";
        }
        else if (m=='may'||m=='MAY')
        {
            cout<<"There Are 31";
        }
        else if (m=='june'||m=='jun'||m=='JUNE')
        {
            cout<<"There Are 30";
        }
        else if (m=='jul'||m=='july'||m=='JULY')
        {
            cout<<"There Are 31";
        }
        else if (m=='aug'||m=='august'||m=='AUGUST')
        {
            cout<<"There Are 31";
        }
        else if (m=='sep'||m=='september'||m=='SEPTEMBER')
        {
            cout<<"There Are 30";
        }
        else if (m=='oct'||m=='october'||m=='OCTOBER')
        {
            cout<<"There Are 31";
        }
        else if (m=='nov'||m=='november'||m=='NOVEMBER')
        {
            cout<<"There Are 30";
        }
        else if (m=='dec'||m=='december'||m=='december')
        {
            cout<<"There Are 31";
        }
    }
    else if(m=='feburary'||m=='feb'||m=='FEBURARY'){
        cout<<"You Entered ";
        cout<<m;
        cout<<" which have more days in Leap year's ";
        cout<<"So Please Enter The year:- ";
        
        cin>>y;
        if (y>=1752)
    {
        
    if ((y-1752)%4==0)
    {
        cout<<"There are 29 days";
    }
    else if((y-1752)%4!=0){
        cout<<"There is 28 days";
    }}
    else{
        cout<<"The First leap is started from 1752 so Please enter Number Greater than it";
    }
    
    }
   else cout<<"------------------Please Enter A valid Month or Check Your Spelling--------------------";
        return 0;
    }
sainupangad
  • 115
  • 9

3 Answers3

5

Try this:

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

int main() {
    std::string m;
    int y;
    cout << "Plase Enter The month:- ";
    cin >> m;
    if (m == "january" || m == "march" || m == "april" || m == "may" || m == "june" || m == "july" || m == "august" || m == "september" || m == "december" || m == "october" || m == "november" || m == "jan" || m == "mar" || m == "apr" || m == "may" || m == "jun" || m == "jul" || m == "aug" || m == "sep" || m == "dec" || m == "oct" || m == "nov" || m == "JANUARY" || m == "MARCH" || m == "APRIL" || m == "MAY" || m == "JUNE" || m == "JULY" || m == "AUGUST" || m == "SEPTEMBER" || m == "OCTOBER" || m == "NOVEMBER" || m == "DECEMBER")
    {
        if (m == "jan" || m == "january" || m == "JANUARY")
        {
            cout << "There Are 31";
        }
        else if (m == "march" || m == "mar" || m == "MARCH")
        {
            cout << "There Are 31";
        }
        else if (m == "apr" || m == "april" || m == "APRIL")
        {
            cout << "There Are 30";
        }
        else if (m == "may" || m == "MAY")
        {
            cout << "There Are 31";
        }
        else if (m == "june" || m == "jun" || m == "JUNE")
        {
            cout << "There Are 30";
        }
        else if (m == "jul" || m == "july" || m == "JULY")
        {
            cout << "There Are 31";
        }
        else if (m == "aug" || m == "august" || m == "AUGUST")
        {
            cout << "There Are 31";
        }
        else if (m == "sep" || m == "september" || m == "SEPTEMBER")
        {
            cout << "There Are 30";
        }
        else if (m == "oct" || m == "october" || m == "OCTOBER")
        {
            cout << "There Are 31";
        }
        else if (m == "nov" || m == "november" || m == "NOVEMBER")
        {
            cout << "There Are 30";
        }
        else if (m == "dec" || m == "december" || m == "december")
        {
            cout << "There Are 31";
        }
    }
    else if (m == "feburary" || m == "feb" || m == "FEBURARY") {
        cout << "You Entered ";
        cout << m;
        cout << " which have more days in Leap year\"s ";
            cout << "So Please Enter The year:- ";

        cin >> y;
        if (y >= 1752)
        {

            if ((y - 1752) % 4 == 0)
            {
                cout << "There are 29 days";
            }
            else if ((y - 1752) % 4 != 0) {
                cout << "There is 28 days";
            }
        }
        else {
            cout << "The First leap is started from 1752 so Please enter Number Greater than it";
        }

    }
    else cout << "------------------Please Enter A valid Month or Check Your Spelling--------------------";
    return 0;
}

In c++, the single quote ' can only be used for characters, for example 'e', but not for strings, like 'hello'. For strings, you use the double quote, like this ".

You should also use std::strings if you wish to compare input with something else.

3
  1. You declared m as character and use it as string.
  2. You use '' to denote strings but it's "" what you should use.
@@ -1,70 +1,35 @@
 #include<iostream>
+#include<string>
 
 using namespace std;
 
 int main()
 {
-    char m;
+    string m;
     int y;
     cout<<"Plase Enter The month:- ";
     cin>>m;
-    if (m=='january'||m=='march' ...
+    if (m=="january"||m=="march" ...
     {
-        if (m=='jan'||m=='january'||m=='JANUARY')
+        if (m=="jan"||m=="january"||m=="JANUARY")
         {
             cout<<"There Are 31";
         }

tansy
  • 486
  • 3
  • 8
  • This is the output of a program called `diff` one could use `patch` to get the source code of the update, related: [https://opensource.com/article/18/8/diffs-patches](https://opensource.com/article/18/8/diffs-patches) – drescherjm May 24 '21 at 17:37
  • I did it to show what is to be changed to make it work, and in answer is said why. – tansy May 24 '21 at 18:26
  • No problem, I just wanted to explain to people who have never seen this type of output. – drescherjm May 24 '21 at 18:33
  • The tests will always fail. Also, I'm pretty sure this won't compile, as there isn't a == operator for chars and const char*s. –  May 25 '21 at 14:20
  • if one does correct syntax it will compile and the tests will not always fail. Since then it's OP's job to do something with his program. We helped him to point his mistakes, now it's his job to learn from it. – tansy May 25 '21 at 15:54
1

Actually m is a character. The size of a character is 1 byte. That means you can assign char m = 'q'; But you can't say char m = 'qq'; Here you have to use a character array, i.e., char m[] = "qq";

Rohan
  • 52
  • 1
  • 4