-3

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. Here is my edited program it was working but now it has a debug error the variable "month" is being used without being initialized. What does that mean?

#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{

int month;
cout<<"Enter a number from 1-12.";
if (month ==1)
    cout<<"January";
else if (month==2)
    cout<< "February";
else if (month==3)
    cout<<"March";
else if (month==4)
    cout<<"April";
else if (month==5)
    cout<<"May";
else if (month==6)
    cout<<"June";
else if (month==7)
    cout<<"July";
else if (month==8)
    cout<<"August";
else if (month==9)
    cout<<"September";
else if (month==10)
    cout<<"October";
else if (month==11)
    cout<<"November";
else if (month==12)
    cout<<"December";
else if (month>12)
    cout<<"Sorry I need a number from 1-12."<<endl;          
else if(month<=12) 
    cout<< "The month is "<<month;
cin>>chr;
return 0;

}

Stacy Doyle
  • 63
  • 2
  • 2
  • 9
  • 2
    Is your question how to actually set `month` to whatever the user typed in, or something else? – Useless Oct 05 '13 at 16:59
  • I have set the month so it displays I am now trying to get the cout<<" the month is"" to display with the months name. My program was working but now there is an error that says the variable "month" is being used without being initialized. what does that mean and how do I fix it? – Stacy Doyle Oct 05 '13 at 17:52
  • This question appears to be off-topic because it is about such a low-level and unique problem that it will be of no use to anyone but the asker. SO is not a "fix my codez" site. – Kuba hasn't forgotten Monica Oct 05 '13 at 20:27

5 Answers5

8

you should use cin>>month; before if statement. Because if you don't use this, your input from keyboard will never be assigned to your integer.

I also recommend to use switch-case and avoid using if statement as long as you can.

In your case, array of strings is also applicable but switch-case is more convenient

You can examine below code. I recommend delete break statement then run the code or delete default statement and enter invalid inpu then run the code. It will help you to see how switch-case works

#include <iostream>

using namespace std;

int main (){
    int month;
    cout<<"Enter month: ";
    cin>>month;

    switch(month){
    case 1:
        cout<<"Jan"<<endl;
        break;
    case 2:
        cout<<"Feb"<<endl;
        break;
    case 3:
        cout<<"Mar"<<endl;
        break;
    case 4:
        cout<<"Apr"<<endl;
        break;
    case 5:
        cout<<"May"<<endl;
        break;
    case 6:
        cout<<"Jun"<<endl;
        break;
    case 7:
        cout<<"Jul"<<endl;
        break;
    case 8:
        cout<<"Aug"<<endl;
        break;
    case 9:
        cout<<"Sep"<<endl;
        break;
    case 10:
        cout<<"Oct"<<endl;
        break;
    case 11:
        cout<<"Nov"<<endl;
        break;
    case 12:
        cout<<"Dec"<<endl;
        break;

    default: // default is for when you enter a number out of 1-12 range. for instance, 13
        cout<<"invalid input!"<<endl;
    }

    return (0);
}
t1w
  • 1,408
  • 5
  • 25
  • 55
  • 1
    `I also recommend to use switch-case and avoid using if statement as long as you can` -- Why? – Robert Harvey Oct 05 '13 at 18:35
  • @RobertHarvey http://stackoverflow.com/questions/449273/why-the-switch-statement-and-not-if-else also: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx and also: http://www.codeproject.com/Questions/108561/Why-we-use-switch-in-c-language-in-case-of-if-else – t1w Oct 05 '13 at 18:41
  • Hmm, OK. The CodeProject article pretty much says it's a matter of style, and speed of execution is almost certainly inconsequential here. Just sayin'. – Robert Harvey Oct 05 '13 at 18:45
  • @RobertHarvey it may not make any different for this case. But i asked this problem to myself some time ago and, for dense situations, case values compiler generates jump table, for sparse situations - binary search or series of if/else, so in worst case switch is as fast as if/else, but typically faster. Although some says some compilers can similarly optimise if/else – t1w Oct 05 '13 at 18:54
4

Do I need to have a string that includes all the names of the months?

Not a string, but an array of strings can be used to make your code far more compact, and as Timur pointed out, you can use cin to read from the console input:

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

int main()
{
    int m;
    string months[] = {"Jan", "Feb", "Mar", "Apr", "May",
                       "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    cout << "Enter a month number: ";
    cin >> m;
    cout << "You selected " << months[m-1] << endl;
    return 0;
}

Array indices start at 0 in C/C++, which is why you need to subtract 1 from the number entered by the user. You'll probably also want to add an if statement to make sure that the number is between 1 and 12, inclusive, but I'll leave that as an exercise for the reader.

m01
  • 9,033
  • 6
  • 32
  • 58
  • we have not learned arrays yet. I changed this: int month; cout<<" Enter a number from 1-12."<>month; also I changed this else if (month>12) cout<<"Sorry I need a number from 1-12."< – Stacy Doyle Oct 05 '13 at 17:15
  • 1
    If you insert an empty string as the first month then you don't have to subtract 1 from the index. – Thomas Matthews Oct 05 '13 at 17:20
2

Instead of doing this on your own, I think I'd use some of the functions built into the standard library:

struct tm t = { 0 };

std::cin >> t.tm_mon;

char buffer[32];

strftime(buffer, sizeof(buffer), "%B", &t);
std::cout << buffer;

Among other things, this has the advantage that it's locale-aware, so if (for example) you do something like:

setlocale(LC_ALL, "de-DE");

...before executing the code above, and the user enters, say, 5, you'll get the result in German ("Juni"). Most often you want to use: setlocale(LC_ALL, "");, which retrieves the locale for which the user has set up the operating system, and uses that, so without modifying your code at all, it would print "June" for me and "Juni" for somebody using German, "Junio" for somebody using Spanish, etc.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Firstly, define a variable to hold the name of the month:

string monthName;

Then after every cout command in the if else nest, add the following:

monthName.assign("YOUR_MONTH_HERE");

For example, if month=1, the above line should be monthName.assign("January");

At the end of the program, just put monthName into cout:

cout<< "The month is "<<monthName;
Tu Bui
  • 1,660
  • 5
  • 26
  • 39
0

There are a few ways that you can do this. Firstly, your way is correct, however I would assign a string the month value in the if blocks and cout the string at the end instead. Another, easier way, if you know arrays, is to make an array with the months, and cout the array[monthnumber] <- (can you find the error in that statement).

Also, it would be best to put this in a loop until the user inputs a number between 1 and 12

JFakult
  • 327
  • 1
  • 2
  • 16