2017-10-01 3 views
0

私はたくさんのことを試みてこのコードを変更し続けましたが、 'スコア'の値を受け入れるには2つの入力が必要です。また、「スコア」と「答え」からcinを止める方法を見つけるのが難しかった ユーザーは値を入力せずにエンターキーを押すことができません。私が見た唯一の方法は、両方を文字列として受け入れることですが、私はそれを避けることを望んでいました。値を受け取る前に2つ以上の入力を必要とするC++ cin

#include<iostream> 
 
#include<iomanip> 
 
#include<limits>**strong text** 
 

 
using namespace std; 
 

 
//void enterdata(string*, int*); 
 

 
int main(){ 
 
// \t string names[]; 
 
// \t int testscores[]; 
 
\t string name; 
 
\t int score; 
 
\t char answer; 
 

 
\t cout<<"This program asks the user to enter a list of names and test scores,"<<endl; 
 
\t cout<<"then sorts the list into ascending order according to the scores"<<endl; 
 
\t cout<<"and calculates the average test score."<<endl; 
 

 
// \t enterdata(names, testscores); 
 

 
\t do{ 
 
\t \t \t cout<<"Please enter a name: "; 
 
\t \t \t getline(cin, name); 
 
\t \t \t while(name.empty()){ 
 
\t \t \t \t cout<<"No entry detected. Please enter a name: "; 
 
\t \t \t \t getline(cin, name); 
 
\t \t \t } 
 
\t \t \t cout<<name<<endl; 
 
\t \t \t cout<<"Please enter a test score: "; 
 
\t \t \t cin>>score; 
 
\t \t \t while(!(cin>>score) || score<0){ 
 
\t \t \t \t cin.clear(); 
 
\t \t \t  cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
 
\t \t \t  cout<<"Input not valid. Only positive integers will be accpeted."<<endl; 
 
\t \t \t  cout<<"Please enter a test score: "; 
 
\t \t \t } 
 
\t \t \t cout<<score<<endl; 
 
\t \t \t cin.sync(); 
 
\t \t \t cout<<"Would you like to enter another student name and test score? [y/n] "; 
 
\t \t \t cin>>answer; 
 
\t \t \t while(answer!='y' && answer!='n'){ 
 
\t \t \t  cout<<"Input not valid. Only y or n will be accepted."<<endl; 
 
\t \t \t  cout<<"Would you like to enter another student name and test score? [y/n] "; 
 
\t \t \t  cin>>answer; 
 
\t \t \t } 
 
\t \t \t cout<<answer<<endl; 
 
\t \t \t cin.sync(); 
 
\t \t } while(answer == 'y'); 
 

 
\t return 0; 
 
}

+0

フォーマット抽出後にstd :: getline()が入力をスキップするのはなぜですか?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted -extraction) – user0042

+0

私は分かりません。それはその質問とは違った働きをしているようです。入力をスキップしません。プログラムは、値を受け入れずに次のステップに移りません。私が正の数を入力してEnterを押す限り、2回目に数字を入力してenterを押すと、cinは値を受け入れてプログラムの次のステップに進みます。 – BigB63

+0

これらの2行で整数を2回読み込んでいます - cin >> score; while(!(cin >> score)|| score <0){ また、cinとreadlineを切り替えるときには注意してください - cinを使って整数を読み込んだ場合、改行は読み込まれないので、次のreadline()。 – Harmeet

答えて

1

エラーはここにある:

while(!(cin>>score) || score<0){ //------ Here! 
    cin.clear(); 
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
    cout<<"Input not valid. Only positive integers will be accpeted."<<endl; 
    cout<<"Please enter a test score: "; 
} 

あなたがcin>>scoreを言うときには、再度入力ストリームを使用しているので、プログラムは複数の入力を要求します!関数呼び出しのように考えてください(ヒント:演算子のオーバーロード!)。ストリームのエラーをテストする必要があるため、これを書いたとします(つまり、!(cin>>score)。このような小さなプログラムでは、私はそれを(学校の運動?)

あなたの質問の第二部については、妨害については、影響を与えることから入力すると、迷惑な改行これを簡単に行うことはできません。

#include <iostream> 
#include <iomanip> 
#include <limits> 
#include <string> 

using std::cout; 
using std::cin; 
using std::string; 
using std::endl; 

//void enterdata(string*, int*); 

int main() { 
    //string names[]; 
    //int testscores[]; 
    string name; 
    int score; 
    char answer = 'y'; 

    cout << "This program asks the user to enter a list of names and test scores," << endl; 
    cout << "then sorts the list into ascending order according to the scores" << endl; 
    cout << "and calculates the average test score." << endl; 

    //enterdata(names, testscores); 

    while(answer == 'y') { 
    cout << "Please enter a name: "; 
    std::cin >> name; 
    while (name.empty()) { 
     cout << "No entry detected. Please enter a name: "; 
     std::cin >> name; 
    } 
    cout << name << endl; 
    cout << "Please enter a test score: "; 
    cin >> score; 
    while (score<0) { 
     cin >> score; 
     cin.clear(); 
     cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     cout << "Input not valid. Only positive integers will be accpeted." << endl; 
     cout << "Please enter a test score: "; 
    } 
    cout << score << endl; 
    cin.sync(); 
    cout << "Would you like to enter another student name and test score? [y/n] "; 
    cin >> answer; 
    while (answer != 'y' && answer != 'n') { 
     cout << "Input not valid. Only y or n will be accepted." << endl; 
     cout << "Would you like to enter another student name and test score? [y/n] "; 
     cin >> answer; 
    } 
    cout << answer << endl; 
    cin.sync(); 
} 
return 0; 
} 
:私は丁寧に掃除してコードを修正しているここであなたは本当にこのかかわらずに時間を過ごしたい(と、それは学校の運動であれば、誰もが同じ問題を持っているとしている)かどうかを

をお願いしたいです

+0

ええ、これは学校のプロジェクトです。私は残りの部分を行う前にメインのdo-whileループを取得しようとしていました。私はおそらく私はそれをプログラムの残りの部分に入れて宣言のいくつかを変更する必要があります。プロジェクトはポインタ上にあり、名前とテストスコアを含む配列/配列を動的に割り当てる必要があります。ポインターを使用して、配列/配列をそれぞれのテストスコアに関連付けられた名前を維持しながらテストスコアに従って昇順に並べ替えます。名前とスコアのソートされたリストを出して平均テストスコアを計算しなければなりません。 – BigB63

+0

とにかく、プロジェクトの一部として、入力の失敗チェックと、プログラムがcinエラーで終了するのを許可するのではなく、有効なデータの再入力を行う必要があります。 – BigB63

+0

また、私は手伝ってくれてうれしいですが、あなたが私に与えたコードはどちらも動作していません。Eclipse上では、私の袖口を印刷するのではなく、整数値を求めているだけです.C++ Shellでは、テストスコアを入力するときに無効な応答を与えると、名前の最初の値と0のテストスコアが無限に再現されます。 – BigB63

関連する問題