您如何将字符串传递给函数并将 return 传递给整数?
How would you pass a string to a function and return an integer?
我发现我需要开始使用 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;
}
将您的一个循环提取到另一个函数并参数化您的 "question text":
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;
}
}
你需要一个函数。类似于:
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 传入字符串并利用 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);
}
您要做的是将您复制的代码提取到它自己的函数中,如下所示:
...
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;
}
};
...
我发现我需要开始使用 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;
}
将您的一个循环提取到另一个函数并参数化您的 "question text":
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;
}
}
你需要一个函数。类似于:
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 传入字符串并利用 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);
}
您要做的是将您复制的代码提取到它自己的函数中,如下所示:
...
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;
}
};
...