使用函数时从整数转换为字符串
Convert to string from integer while using functions
谢谢大家对我上一篇文章的帮助!从函数中提取信息时,我仍然无法从整数转换为字符串。我正在尝试将月份的实际名称显示在输出中,而不是与之关联的数字。我不确定如何成功转换为字符串。它在下面工作,数字在它的位置,但我只是无法让它在屏幕上显示字符串。
例如,我被要求有这样的句子:">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;简单就这样:
#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;
}
使用简单索引
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;
无需添加任何新代码。
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数