My program is suppose to prompt user to enter a number 1-12 and output the corresponding month.
Ok I know I am missing a very important part of this program but right know I am struggling to figure out what to use. Do I need to have a string that includes all the names of the months? Also I know that I need to have something to put after the cout<<"the month is"<< something has to go here so the answer will print but I not sure what right now. I also think I need at have int month= something but not sure if it should be 1-12 or monthname.
constchar *array_months[]={"January","February","March","4","5","6","7","8","9","10","11","12"}; // I'm too lazy to fill the names
int month;
cout<<" Enter a number from 1-12."<<endl;
cin >>month;
// make sure that user entered a correct number. If user entered an invalid number and you do not chheck it, the program might crash
if(month<1 || month>12)
cout <<"Month must be between 1 and 12\n";
else cout <<array_months[month-1]; //month-1 because the first item in the array is array_mothns[0],
We have not learned arrays yet. I changed this part and add string months. It prints the month name but how do I get it to print the cout with the month at the end?[code
cout<<"Sorry I need a number from 1-12."<<endl;
else if(month<=12)
cout<< "The month is "<<months;
cin>>chr;
return 0;
}
my program was working but now I am getting an error message saying the variable "month" is being used without being initialized. What does that mean and how do I fix it?
Well, this means that you have not input the value of month as "cin>>month". well i am also a beginner, and i don't understand the term string, but i had a program similar to this for my initial lessons.
there are two things, you use if, elseif or switch. i prefer switch, but if u prefer, if elseif, u can use the second code.
in your code, first you output the name of the month, then you otput the sentence!, it is not required.
#include<iostream.h>
#include<conio.h>
void main()
{
int month;
cout<<" Enter a number from 1-12.";
cin>>month;
switch (month)
{
case 1:
cout<< "The month is January";
break;
case 2:
cout<< "The month is February";
break;
case 3:
cout<<"The month is March";
break;
case 4:
cout<<"The month is April";
break;
case 5:
cout<<"The month is May";
break;
case 6:
cout<<"The month is June";
break;
case 7:
cout<<"The month is July";
break;
case 8:
cout<<"The month is August";
break;
case 9:
cout<<"The month is September";
break;
case 10:
cout<<"The month is October";
break;
case 11:
cout<<"The month is November";
break;
case 12:
cout<<"The month is December";
break;
default:
cout<<"Sorry I need a number from 1-12.";
break;
}
getch();
#include<iostream.h>
#include<conio.h>
void main()
{
int month;
cout<<" Enter a number from 1-12.";
cin>>month;
if (month ==1)
cout<< "The month is January";
elseif (month==2)
cout<< "The month is February";
elseif (month==3)
cout<<"The month is March";
elseif (month==4)
cout<<"The month is April";
elseif (month==5)
cout<<"The month is May";
elseif (month==6)
cout<<"The month is June";
elseif (month==7)
cout<<"The month is July";
elseif (month==8)
cout<<"The month is August";
elseif (month==9)
cout<<"The month is September";
elseif (month==10)
cout<<"The month is October";
elseif (month==11)
cout<<"The month is November";
elseif (month==12)
cout<<"The month is December";
else
cout<<"Sorry I need a number from 1-12.";
getch();