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

C++ 如何将字符串传递给函数并返回整数?,c++,string,int,stringstream,C++,String,Int,Stringstream,我发现我需要开始使用getline(cin,input);用于我的用户输入。我了解了如何使用stringstream将用户的字符串转换为int,以便在数学函数中存储和使用数字 例如,假设您需要向用户请求一个学生ID,您可以轻松地将其存储为字符串,因为您很少需要使用它执行任何类型的数学方程。然而,如果你要问成绩,你需要平均并转换成GPA,那就另当别论了 基本上,我想让用户通过getline输入一个数字,然后将输入转换成int,但作为一个函数,我不需要在每次需要转换某些内容时都输入相同的交易 例如:

我发现我需要开始使用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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班级学生{
int s1、s2、s3;
字符串名称、id、输入;
公众:
void getData(){

cout将一个循环提取到另一个函数,并将“问题文本”参数化:

int get_输入(const char*what)
{
while(true)
{
cout>temp)返回温度;

你需要一个函数,比如:

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");

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

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;
    }
}
intgetgrade(const std::string&s){
试一试{
int结果=标准::stoi(s);
返回结果;
}
catch(std::无效的_参数){

std::cout您要做的是将复制到其自身函数中的代码提取出来,如下所示:

...

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

...
。。。
int readGrade(常量字符*主题){
while(true){

无法确定您要问的是什么-是获取int并返回字符串的函数的签名吗?有几种方法可以做到这一点。
std::istringstream
std::stoi()
,等等。我明白了!将主题作为字符串传递完全逃过了我的眼睛。非常感谢!我只是在自己的时间里练习,所以这只是我为自己做的一个小个人项目,以改进我的编码。在这种情况下,你可以将其作为
常量字符*
传递-我只是倾向于默认为
std::string
除非另有充分理由。
int main() {
    int x;
    std::string s1;
    std::cout << "Enter grade: ";
    std::getline(std::cin, s1)        
    x = getGrade(s1);
}
...

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

...