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

Posted

技术标签:

【中文标题】使用函数时从整数转换为字符串【英文标题】:Convert to string from integer while using functions 【发布时间】:2018-04-23 07:32:43 【问题描述】:

感谢大家对我之前帖子的帮助!从函数中提取信息时,我仍然无法从整数转换为字符串。我正在尝试将月份的实际名称显示到输出中,而不是与其关联的数字。我不确定如何成功转换为字符串。它在下面使用数字,但我无法让它在屏幕上显示字符串。

例如,我需要有以下句子:“1 月 月份的最大降雨量为 12 英寸”

edit:总而言之,我无法让函数带入字符串,而不是整数值。我的代码顶部声明的函数是我的教授让我们使用的,所以我不能对它们的声明方式做任何不同的事情。

// 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;

【问题讨论】:

Alternative to itoa() for converting integer to string C++?的可能重复 你已经在第一个循环中成功地做到了。再做同样的事情。 【参考方案1】:

请注意,我还在函数中添加了下标 = 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;

【讨论】:

【参考方案2】:

使用简单的索引

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;

无需添加任何新代码。

【讨论】:

在这个作业中,我必须使用我的代码中包含的函数。 @Denise ,将函数 IntToString(int id) 添加到您的代码中并使用它。或者只使用索引months[id-1] 如果您想从 1 开始月份,则只有 id 必须是 -1。或者我​​没有在评论中得到问题? 哦,抱歉。我想我误解了你的回答。我会按照你的建议工作。谢谢! @Denise ,请在帖子末尾查看我的编辑更新。我写了解释

以上是关于使用函数时从整数转换为字符串的主要内容,如果未能解决你的问题,请参考以下文章

[C/C++笔面试]不使用库函数将整数转换为字符串

[C/C++笔面试]不使用库函数将字符串转换为整数

编程:将一个数字字符串转换成一个整数(不得调用C语言提供的将字符串转换为整数的函数)。

转换输入时间时从字符串转换日期和/或时间时转换失败 | SQL, SQLSRV

自定义一个函数itos,将一个整数转换为字符串的功能,并编写主函数调用该函数。

怎样用c语言编写将字符串转换为整数的函数