并行数组如何将int转换为字符串
Parallel Arrays how to turn int into string
我的作业有问题,我的教授没有回应。我如何让我的数组返回getHighest
和getLowest
函数的月份的字符串名称?
它一直告诉我我必须添加更多的细节,我从中删除了代码,所以我现在只剩下主要和getLowest
和getHighest
函数。它说我还需要更多的细节,所以我输入这个来添加更多的细节,这样我就可以提交我的问题了。
提前感谢!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Function prototypes
void getRainfall(double[], int); //To retrieve the user input.
double getTotal(double[], int); //To total the rainfall amounts.
double getAverage(double[], int); //To get the average rainfall.
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 highest value, provides the index of the highest value in the last parameter.
//Global Variable
const int NUM_MONTHS = 12;
//Array names
double rainfall[NUM_MONTHS];
string month[NUM_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; //Month array to hold names of months
int main()
{
//Declare variables
double total, average;
int low, high;
string lowMonth, highMonth;
//Call Functions
getRainfall(rainfall, NUM_MONTHS); //To retrieve the user input.
total = getTotal(rainfall, NUM_MONTHS); //To total the rainfall amounts.
average = getAverage(getTotal, NUM_MONTHS); //To get the average rainfall.
lowMonth = getLowest(rainfall, NUM_MONTHS, low); //Returns the lowest value, provides the index of the lowest value in the last parameter.
highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter.
//Display the following:
cout << "The total rainfall for the year is: " << total << endl;
cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl;
cout << "Least amount of rainfall fell in: " << highMonth << endl;
cout << "Most amount of rainfall fell in: " << lowMonth << endl;
return 0;
}
//*******************************************************************************************
// double getLowest(double amount[], int size) *
// Returns the lowest value, provides the index of the lowest value in the last parameter. *
//*******************************************************************************************
double getLowest(double amount[], int NUM_MONTHS, int &low)
{
low = amount[0]; //Variable to hold lowest value.
int lowMonth = 0; //Set low value to intial rainfall value. //Variable to return month element location.
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] < low)
{
low = amount[index];
lowMonth = index;
}
}
return lowMonth;
}
//*********************************************************************************************
// double getHighest(double amount[], int size) *
// Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************
double getHighest(double amount[], int NUM_MONTHS, int &highMonth)
{
//high = amount[0]; //Variable to hold highest value
months = amount[0];
double highMonth = 0; //Variable to hold highest value
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
}
}
return highMonth;
}
所以你的问题是非常不清楚,你的代码是一个完全混乱,但我假设你想函数getHighest
也返回一个字符串与月份的名称?
好吧,一个函数只能返回一种类型的变量,但您可以使用引用:
double getHighest(double amount[], int NUM_MONTHS, int &highMonth, string &name)
{
//high = amount[0]; //Variable to hold highest value
months = amount[0];
double highMonth = 0; //Variable to hold highest value
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
name = month[index];
}
}
return highMonth;
}
但通常你的代码有相当多的缺陷:例如覆盖highMonth
可能会导致问题,为什么要传递NUM_MONTHS
,即使它是全局定义的?我不太明白为什么这段代码如此复杂。我也认为getHighest
和getLowest
功能甚至不起作用。作为一种更好的方法,我会尝试这样的函数:
double getHighest(double amount[], string &name)
{
double highest = 0;
double highPos = 0;
for(unsigned int i = 0; i < NUM_MONTHS; i++)
{
if(highest <= amount[i])
{
highest = amount[i];
highPos = i;
}
}
name = month[highPos];
return highest;
}
编辑:或者,如果你只想返回字符串,你可以这样做:
字符串,名称){//high = amount[0];//保存最高值的变量Months = amount[0];double highMonth = 0;//保存最高值的变量
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
name = month[index];
}
}
return highMonth;
}
但通常你的代码有相当多的缺陷:例如覆盖highMonth
可能会导致问题,为什么要传递NUM_MONTHS
,即使它是全局定义的?我不太明白为什么这段代码如此复杂。我也认为getHighest
和getLowest
功能甚至不起作用。作为一种更好的方法,我会尝试这样的函数:
string getHighest(double amount[], string &name)
{
double highest = 0;
double highPos = 0;
for(unsigned int i = 0; i < NUM_MONTHS; i++)
{
if(highest <= amount[i])
{
highest = amount[i];
highPos = i;
}
}
name = month[highPos];
return name;
}
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数