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.

Would someone please help me figure out how to get C++ to accept my input

0 votes
asked Feb 18, 2020 by WillPope (190 points)

// Would someone please help me figure out how to get C++ to accept my inputed month value as a decimal, by in-turn change it to an integer?

#include <iostream>
using namespace std;

int main()
{
    int month, day, year;
    cout << "Enter a month (use 1 for january, ect.): ";
    cin  >> month;
    
    cout << "Enter a day of the month: ";
    cin  >>  day;
    
    cout << month << "/" << day << endl;
    

    if ( day == (int) day )
    {                        // Day must be an integer quantity.
    }
        else
        {
            cout << "day is invalid" << endl;   
        }
            if ( (month < 1 ) || ( month > 12) )
            {
                cout << "Month value is invalid." << endl;
            }
                if ( (day < 1 ) || ( day > 31) )  
                {
                    cout << "Day value is invalid." << endl;
                }
    
    switch (month)
    {
        case 9:     // September
        case 4:     // April
        case 6:     // June
        case 11:    // November
        {
            if ( day > 30 )
            {
                cout << "Day value is invalid." << endl;
            }
        }
        break;
        
        case 2:     // February
        {
            if ( day > 28 )
            {
                cout << "Day value is invalid." << endl;
            }
        }
        break;
    }
    
   return 0;
}

1 Answer

0 votes
answered Feb 19, 2020 by Theodore Friedrich (290 points)
If your just trying to get a decimal input, you can just change the your initialization of month to "double month" instead of "int month."

If you want an int input and then have it become a decimal you can just create a new variable for your decimal month and set it equal to the int month:

double dMonth = month; //c++ and most other languages will convert an int to a floating-point number if need be
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.
...