使用函数时从整数转换为字符串

Convert to string from integer while using functions

本文关键字:转换 字符串 整数 函数      更新时间:2023-10-16

谢谢大家对我上一篇文章的帮助!从函数中提取信息时,我仍然无法从整数转换为字符串。我正在尝试将月份的实际名称显示在输出中,而不是与之关联的数字。我不确定如何成功转换为字符串。它在下面工作,数字在它的位置,但我只是无法让它在屏幕上显示字符串。

例如,我被要求有这样的句子:">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;

无需添加任何新代码。