2017-09-26 8 views
-2

プログラミングの世界に新しい人たちがいます。文字列としてすべての入力を読むべきですが、これは単純なプログラムであり、私は思っていますが、なぜこの無限ループを持っているのですか

は、これまでのところ、私は終了し、Qを押したときに、私は無限ループを取得していますし、どのように私はここに総 に見えるので、しばらくループ凝縮なぜ私の質問は......私が持っているものである
#include "stdafx.h" 

#include <iostream> 
using namespace std; 
int main() 
{ 

    int grade; 
    char quit = 'a'; 

    cout << "Input your grade (0-100): "; 
    cout << endl; 
    cin >> grade; 
    while (grade < 0) { 
     cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl; 
     cin >> grade; 
    } 
    while (quit != 'q') { 
     while (grade < 0) { 
      cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl; 
      cin >> grade; 
     } 

     if (grade == 100) { 
      cout << "You got a perfect grade!" << endl; 
      cout << "Letter grade: A" << endl; 
     } 
     else if (grade >= 90 && grade <= 100) { 
      cout << "Letter grade: A" << endl << endl; 
     } 
     else if (grade >= 80 && grade <= 89) { 
      cout << "Letter grade: B" << endl << endl; 
     } 
     else if (grade >= 70 && grade <= 79) { 
      cout << "Letter grade: C" << endl << endl; 
     } 
     else if (grade >= 60 && grade <= 69) { 
      cout << "Letter grade: D" << endl << endl; 
     } 
     else if (grade < 60) { 
      cout << "Letter grade: F" << endl << endl; 
     } 
     else { 
      cout << "Invalid grade!" << endl; 
     } 
     cout << " would you like to enter another grade? or press q to quit" << endl; 
     cin >> grade; 
    } 
    system("pause"); 
    return 0;`enter code here` 
} 
+2

変数 'quit'を実際に変更するようなことはありません。 –

+2

あなたは 'quit'が文字 'q'と等しくないことをテストしますが、変数を一番上に明示的に 'a'に設定し、それを更新しません。私はあなたの2番目の 'while'条件を' while(quit!= grade) 'に変更し、' quit'を 'q'に初期化することをお勧めします。それは最も美しいアプローチから遠いですが、うまくいくはずです! –

答えて

1

ミニマル確認可能な例:

#include "stdafx.h" 

#include <iostream> 
using namespace std; 
int main() 
{ 

    int grade; 
    char quit = 'a'; 

    cout << "Input your grade (0-100): "; 
    cout << endl; 
    cin >> grade; 
    while (quit != 'q') { 
     cout << " would you like to enter another grade? or press q to quit" << endl; 
     cin >> grade; 
    } 
    system("pause"); 
    return 0;`enter code here` 
} 

問題をquitで確認してください?

編集
私がやったことはquitまたはループとは何の関係もありません(ほとんどの)行を削除しています。

この時点で、ループは決して変化しないことに気づくはずです

プログラムに問題がある場合は、何が間違っているのかを判断する最善の方法の1つは、エラーとは関係のないすべてのものを取り除くことです。時間内に、あなたはあなたの心だけを使用してを行うことができます。 Duuude!

私はそれ
でよながら、ユーザの入力を処理するための正しい方法は、あなたが望むものに変換し、文字列としてそれを取得することです。例えば

#include <iostream> 
#include <sstream> 
#include <stdexcept> 
#include <string> 

template <typename T> 
T string_to(const std::string& s) 
{ 
    T value; 
    std::istringstream ss(s); 
    ss >> value >> std::ws; 
    if (!ss.eof()) throw std::invalid_argument("T string_to()"); 
    return value; 
} 

int main() 
{ 
    std::cout << "Enter a number or 'q': "; 
    std::string s; 
    getline(std::cin, s); 

    if (s == 'q') 
    { 
    std::cout << "Good job! You entered 'q'.\n"; 
    } 
    else 
    { 
    try 
    { 
     double x = string_to <double> (s); 
     std::cout << "Good job! You entered '" << x << "'.\n"; 
    } 
    catch (const std::exception& e) 
    { 
     std::cout << "Foo, you didn't obey instructions and made me " << e.what() << ".\n"; 
    } 
    } 
} 
+0

要約版は役に立ちますが、おそらく問題がどういうものなのか説明する必要があると思います。一見すると、これは多くの答えのようには見えませんが、OPを表示して問題をより小さな問題に分割して解決を助けるならば、その問題が何であるかを示すと、それは長い道のりになります。 – Tas

+0

よく置いてください。一定。 –

1

gradeのため、VAR。 gradeintと宣言しました。 正しいintを入力すると、それがうまく動作しますが、あなたは別のchar EX :) qまたはfを入力すると、機能cinintタイプとしてqまたはfを認識することはできません。

charが入力された場合、cinは自分のプロセスに合格します。

あなたはcharint入力の両方を認識するchargradeタイプを変更する必要があります。

入力フローを1つだけ使用する場合は、この実装コードを参考にしてください。

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    char c_input[32] = {0}; 
    cin>>c_input; 

    while(atoi(c_input) < 0) 
    { 
     cout<<"If you have a negative grade....drop out! otherwise enter another grade" << endl; 
     cin>>c_input; 
    } 
    while(c_input[0] != 'q') 
    { 
     while(atoi(c_input) < 0) 
     { 
      cout<<"If you have a negative grade....drop out! otherwise enter another grade" << endl; 
      cin>>c_input; 
     } 
     cout<<c_input; 
     cout<<"Would you like to enter another grade? or press q to quit" << endl; 
     cin>>c_input; 
    } 
    return 0; 
} 
関連する問題