2017-09-26 1 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'로 설정하고 결코 업데이트하지 마십시오. 두 번째 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 또는 루프와는 아무 상관이 없다 (대부분의) 라인을 제거합니다.

이 시점에서 루프 은 절대로 변경되지 않습니다..

프로그램에 문제가있는 경우 오류를 파악하는 가장 좋은 방법 중 하나는 오류와 관련이없는 모든 것을 제거하는 것입니다. 시간이 지나면 마음을 사용하여 을 할 수 있습니다. 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 전 :)를 입력하면 q 또는 f, 기능 cinint 유형으로 q 또는 f을 인식 할 수 없습니다.

char 입력시 cin은 자신의 과정을 통과합니다.

당신은 charint 입력 모두를 인식 할 수 char으로 grade 유형을 변경해야합니다.

입력 흐름을 하나만 사용하려면이 구현 코드를 사용하는 것이 좋습니다.

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