2017-10-24 11 views
-3

getline(cin、input)を使う必要があることがわかりました。私のユーザー入力のために。私は文字列をユーザーからintに変換するためにstringstreamを使用する方法を考え出しました。そのため、数学関数で数値を格納して使用することができます。文字列を関数に渡して整数を返しますか?

例として、ユーザーにStudent IDを尋ねる必要があるとします。その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; 
} 
+0

ないあなたが求めているものを確認してください - 関数のシグネチャint型を取って、文字列を返すためにそれをあります? – Steve

+3

これを行うにはいくつかの方法があります。 'std :: istringstream'、' std :: stoi() 'など – user0042

答えて

4

あなたは関数が必要です。ような何か:CONSTとstd::stoi機能を利用するためにあなたが参照することにより、文字列を渡すことができます

s1 = getGrade("Math"); 
+0

私は見る!被験者を文字列として渡すと、私は完全に逃げました。どうもありがとうございます!私はちょうど自分の時間のために練習しているので、これは自分のコーディングを改善するために自分でやっている少し個人的なプロジェクトでした。 – JRuxDev

+0

この場合、実際には 'const char *'として渡すことができます。そうでないと合理的な理由がない限り、 'std :: string'のデフォルトになりがちです。 –

0

異なる機能にあなたのループの1つを抽出して、 "質問文" をパラメータ化:

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

入力が無効な場合は何も返しません... – Sean

2

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

使用法のようなものになるでしょう以下:

int main() { 
    int x; 
    std::string s1; 
    std::cout << "Enter grade: "; 
    std::getline(std::cin, s1)   
    x = getGrade(s1); 
} 
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; 
    } 
}; 

... 
+1

スナップ! (偉大な心は似ていると思う)。 –

関連する問題