如何将字符串传递给函数并返回整数?

时间:2017-10-24 08:35:28

标签: c++ string int stringstream

我发现我需要开始使用getline(cin,input);为我的用户输入。我想出了如何使用stringstream将用户的字符串转换为int,这样我就可以在数学函数中存储和使用数字。

例如,假设您需要向用户询问学生ID,您可以轻松地将其存储为字符串,因为您很少需要使用它来执行任何类型的数学方程式。但是,如果您要求成绩,那么您需要平均并转换为GPA,这是另一个故事。

我本质上想要让用户通过getline输入一个数字,然后将输入转换为int,但作为一个函数,这样我每次需要转换时都不需要输入相同的交易。

示例:

#include<iostream>
#include<conio.h>
#include<string>
#include<sstream>

using namespace std;

class students{

    int s1, s2, s3;
    string name, id, input;

public:
    void getData(){
            cout << "Enter ID: ";
            getline(cin, id);

            cout << "Enter Name: ";
            getline(cin, name);

            while(true){
                cout << "Enter grades for Math: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s1)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
            while(true){
                cout << "Enter grades for Science: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s2)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
            while(true){
                cout << "Enter grades for English: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s3)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
    }

    void showData(){
        cout << "\n" << id << "\t" << name << "\tMath: " << s1 << "\tScience: " << s2 << "\tEnglish: " << s3;
    }
};

int main(){
    students s[20];
    int i, numOfStudents;
    string input;

    while(true){
        cout << "\nNumber of Students? ";
        getline(cin, input);

        stringstream convert(input);
        if(convert >> numOfStudents)
            break;
        cout << "Invalid Grade; Please Try Again! " << endl;
    }

    for(i = 0; i < numOfStudents; i++){
        s[i].getData();
    }

    for(i = 0; i < numOfStudents; i++){
        s[i].showData();
    }

    _getch(); //Only there to keep the command line window open.
    return 0;
}

4 个答案:

答案 0 :(得分:4)

你需要一个功能。类似的东西:

int getGrade(const std::string& subject)
{
   while(true){
        std::cout << "Enter grades for " << subject << ": " << std::flush;
        std::string input;
        std::getline(std::cin, input);

        std::stringstream convert(input);
        int result;
        if(convert >> result)
             return result;
        std::cout << "Invalid Grade; Please Try Again! " << std::endl;
    }
}

用法如下:

s1 = getGrade("Math");

答案 1 :(得分:2)

您可以通过引用传入字符串const并使用std::stoi函数:

int getGrade(const std::string& s) {
    try {
        int result = std::stoi(s);
        return result;
    }
    catch (std::invalid_argument) {
        std::cout << "Could not convert to integer.";
        return -1;
    }
}

并使用它如下:

int main() {
    int x;
    std::string s1;
    std::cout << "Enter grade: ";
    std::getline(std::cin, s1)        
    x = getGrade(s1);
}

答案 2 :(得分:0)

将您的一个循环提取到另一个函数并参数化您的问题文本&#34;:

int get_input(const char* what)
{
     while(true)
     {
         cout << what;
         string input;
         getline(cin, input);

         int temp;
         stringstream convert(input);
         if(convert >> temp) return temp;

         cout << "Invalid input; Please Try Again! " << endl;
    }
}

答案 3 :(得分:0)

您要做的是将您复制的代码提取到自己的函数中,如下所示:

...

int readGrade(const char* subject) {
    while(true) {
        cout << "Enter grade for " << subject << ": ";
        string input;
        getline(cin, input);

        stringstream convert(input);
        int n;
        if(convert >> n)
            return n;
        cout << "Invalid grade, please try again." << endl;
    }
}

class students{

    int s1, s2, s3;
    string name, id, input;

public:
    void getData(){
        cout << "Enter ID: ";
        getline(cin, id);

        cout << "Enter Name: ";
        getline(cin, name);

        s1 = readGrade("Math");
        s2 = readGrade("Science");
        s3 = readGrade("English");
    }

    void showData(){
        cout << "\n" << id << "\t" << name << "\tMath: " << s1 << "\tScience: " << s2 << "\tEnglish: " << s3;
    }
};

...
相关问题