C++ 编写一个输入数字1-12并输出相应月份的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19200301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:35:51  来源:igfitidea点击:

writting a program that inputs a number 1-12 and outputs the corresponding month

c++

提问by Stacy Doyle

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?

我的程序假设提示用户输入数字 1-12 并输出相应的月份。好的,我知道我错过了这个程序的一个非常重要的部分,但我知道我正在努力弄清楚要使用什么。我是否需要一个包含所有月份名称的字符串?我也知道我需要在 cout<<“月份是”<< 一些东西必须放在这里,所以答案会打印出来,但我不确定现在是什么。我也认为我需要 at have int month= something 但不确定它是否应该是 1-12 或月名。这是我编辑过的程序,它正在运行,但现在它有一个调试错误,变量“month”在没有初始化的情况下被使用。这意味着什么?

#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;

}

}

回答by Timur Aykut YILDIRIM

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.

您应该cin>>month;在 if 语句之前使用。因为如果您不使用它,您从键盘输入的内容将永远不会分配给您的整数。

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

我还建议使用 switch-case 并尽可能避免使用 if 语句。

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

在您的情况下,字符串数组也适用,但 switch-case 更方便

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

您可以检查以下代码。我建议删除 break 语句然后运行代码删除默认语句并输入无效的输入然后运行代码。它将帮助您了解 switch-case 的工作原理

#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);
}

回答by m01

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 cinto read from the console input:

不是字符串,而是字符串数组可用于使您的代码更加紧凑,正如 Timur 所指出的,您可以使用cin从控制台输入中读取:

#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 1from the number entered by the user. You'll probably also want to add an ifstatement to make sure that the number is between 1 and 12, inclusive, but I'll leave that as an exercise for the reader.

在 C/C++ 中,数组索引从 0 开始,这就是您需要1从用户输入的数字中减去的原因。您可能还想添加一条if语句以确保数字介于 1 和 12 之间(包括 1 和 12),但我将把它留给读者作为练习。

回答by Jerry Coffin

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.

...在执行上面的代码之前,用户输入,比如说5,,你会得到德语(“Juni”)的结果。大多数情况下,您想使用: setlocale(LC_ALL, "");,它检索用户设置操作系统的区域设置,并使用它,因此根本不修改您的代码,它会为我打印“June”,为使用的人打印“Juni”德语,“Junio”用于使用西班牙语等的人。

回答by Tu Bui

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:

然后在 if else 嵌套中的每个 cout 命令之后,添加以下内容:

monthName.assign("YOUR_MONTH_HERE");

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

例如,如果月=1,上面的行应该是 monthName.assign("January");

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

在程序结束时,只需将 monthName 放入 cout 中:

cout<< "The month is "<<monthName;

回答by JFakult

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).

有几种方法可以做到这一点。首先,你的方法是正确的,但是我会在 if 块中分配一个字符串来表示月份值,并在最后输出字符串。另一种更简单的方法,如果你知道数组,是用月份创建一个数组,并计算出数组[monthnumber] <-(你能找到那个语句中的错误吗)。

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

此外,最好将其放入循环中,直到用户输入 1 到 12 之间的数字