尝试从整数转换为字符串的月份
Trying to convert from integer to string for month
我试图让月份的实际名称显示在输出中,而不是与之关联的数字。基本上,我必须将月份显示为字符串而不是数字。我不确定如何成功转换为字符串。它在下面工作,数字在它的位置,但我只是无法让它在屏幕上显示字符串。
我需要有一句话:"输入一月份的降雨量(以英寸为单位("以及:">1月份的最大降雨量为12英寸">
这是我到目前为止的代码(更新帖子以删除不相关的代码(:
// Declare const variables
const int TOTALMONTHS = 12;
// Declare array for months and rainfall
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainFall[TOTALMONTHS];
// Declare functions
double getLowest(double[], int, int&); // returns the lowest value, provides the index of the lowest value in the last parameter.
double getHighest(double[], int, int&); // returns the higest value, provides the index of the highest value in the last parameter.
int main()
{
int subscript;
// Get the rainfall for each month.
for (int months = 0; months < TOTALMONTHS; months++)
{
// Get this month's rainfall.
cout << "Enter the rainfall (in inches) for month #";
cout << months[months] << ": ";
cin >> rainFall[months];
// Validate the value entered.
}
// The subscript variable will be passed by reference to the getHighest and getLowest functions.
// Display the largest amount of rainfall.
cout << "The largest amount of rainfall was ";
cout << getHighest(rainFall, TOTALMONTHS, subscript)
<< " inches in month ";
cout << (subscript + 1) << "." << endl;
// Display the smallest amount of rainfall.
cout << "The smallest amount of rainfall was ";
cout << getLowest(rainFall, TOTALMONTHS, subscript)
<< " inches in month ";
cout << (subscript + 1) << "." << endl << endl;
// End of program
system("pause");
return 0;
}
你用一个也叫months
的循环变量隐藏了带有月份名称(months
(的数组 - 在给定的作用域中,你只能有一个名为months
的变量。最好将它们都重命名,然后您只需将数组的值打印在您现在使用的索引处作为数字(在 + 1
之前(:
string monthNames[TOTALMONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int main()
{
int subScript;
//...
for (int month = 0; month < TOTALMONTHS; month++)
{
// Get this month's rainfall.
cout << "Enter the rainfall (in inches) for month ";
cout << monthNames[month] << ": ";
cin >> rainFall[month];
//...
}
//...
cout << "The largest amount of rainfall was ";
cout << getHighest(rainFall, TOTALMONTHS, subScript)
<< " inches in month ";
cout << monthNames[subScript] << "." << endl;
//...
system("pause");
return 0;
}
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如何将这个std::字符串转换为std::基本字符串
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 如何在C++中将十六进制字符串转换为文本数据
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何从字符串转换为双精度*
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 如何在C++中将 Python 字符串转换为其转义版本?
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 如何将整数字符串转换为整数的二维向量?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 将文件复制到自定义位置,存在字符串转换问题
- 将ANSI C字符串转换为UNICODE
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将字符串转换为无符号字符数组/字节数组