感谢大家在上一篇文章中提供的帮助!从函数中提取信息时,我仍然遇到从整数转换为字符串的问题。我试图将月份的实际名称显示到输出而不是与其关联的数字。我不确定如何成功转换为字符串。它在下面用数字代替它,但是我无法让它在屏幕上显示字符串。
例如,我需要有一句话:“ 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&);
double getHighest(double[], int, int&);
int main()
{
int subscript;
for (int months = 0; months < TOTALMONTHS; months++)
{
// Get this month's rainfall.
cout << "Enter the rainfall (in inches) for ";
cout << monthNames[months] << ": ";
cin >> rainFall[months];
}
// 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;
}
double getLowest(double rainFall[], int TOTALMONTHS, int &subscript)
{
double smallest;
smallest = rainFall[0];
for (int months = 0; months < TOTALMONTHS; months++)
{
if (rainFall[months] < smallest)
{
smallest = rainFall[months];
subscript = months;
}
}
return smallest;
}
double getHighest(double rainFall[], int TOTALMONTHS, int &subscript)
{
double largest;
largest = rainFall[0];
for (int months = 0; months < TOTALMONTHS; months++)
{
if (rainFall[months] > largest)
{
largest = rainFall[months];
subscript = months;
}
}
return largest;
}
答案 0 :(得分:0)
使用简单的索引
std::string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
std::string IntToString(int id)
{
if ( id < 1 || id > 12 )
{
return "BadMonth";
}
return months[id-1];
}
int main()
{
std::cout << IntToString(1) << std::endl;
std::cout << IntToString(2) << std::endl;
std::cout << IntToString(3) << std::endl;
std::cout << IntToString(0) << std::endl;
}
输出是:
January
February
March
BadMonth
在您的上下文中,我认为您应该替换
cout << (subscript + 1) << "." << endl << endl;
与
cout << IntToString(subscript + 1) << "." << endl << endl;
添加我的方法后。 或者只是
cout << months[subscript] << "." << endl << endl;
不添加任何新代码。
答案 1 :(得分:0)
注意我还在函数中添加了subscript = 0; 这很简单:
#include <string>
#include <iostream>
#include <limits>
using namespace std;
// Declare const variables
const int TOTALMONTHS = 12;
// Declare array for months and rainfall
string monthNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainFall[TOTALMONTHS];
// Declare functions
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);
int main()
{
int subscript;
for (int months = 0; months < TOTALMONTHS; months++)
{
// Get this month's rainfall.
cout << "Enter the rainfall (in inches) for ";
cout << monthNames[months] << ": ";
cin >> rainFall[months];
}
// Display the largest amount of rainfall.
cout << "The largest amount of rainfall was ";
cout << getHighest(rainFall, TOTALMONTHS, subscript)
<< " inches in month ";
cout << monthNames[subscript] << "." << endl;
// Display the smallest amount of rainfall.
cout << "The smallest amount of rainfall was ";
cout << getLowest(rainFall, TOTALMONTHS, subscript)
<< " inches in month ";
cout << monthNames[subscript] << "." << endl << endl;
// End of program
//system("pause");
return 0;
}
double getLowest(double rainFall[], int TOTALMONTHS, int &subscript)
{
double smallest;
smallest = rainFall[0];
subscript = 0;
for (int months = 0; months < TOTALMONTHS; months++)
{
if (rainFall[months] < smallest)
{
smallest = rainFall[months];
subscript = months;
}
}
return smallest;
}
double getHighest(double rainFall[], int TOTALMONTHS, int &subscript)
{
double largest;
largest = rainFall[0];
subscript = 0;
for (int months = 0; months < TOTALMONTHS; months++)
{
if (rainFall[months] > largest)
{
largest = rainFall[months];
subscript = months;
}
}
return largest;
}