2014-01-12 3 views
1

라인 작동하지 닫으려면 하면서 무한 루프 - 숯불 C++

cin >> cRestart; 

는 숯을 캡처되어 있지만 while 루프가 종료되지 않는다. 계속 나아갈 코드가 필요해. 제발 도와 줄 수있어? 는

bool startInputValidation() 
    { 
     char cRestart = 'b';   //set initial value at b to run while loop 

     conRGB(COLOUR_WHITE);   //text colour white 

     conXY(0, 19);     //cursor position in console 
     drawLine();      //draws a horizontal line across screen 
     conXY(0, 23); 
     drawLine(); 

     while (cRestart != 'y' || cRestart != 'Y' || cRestart != 'n' || cRestart != 'N') //check for correct input (Y/N) 
     { 
      conXY(21, 21); 
      for(int iCount = 0; iCount < 139; iCount++)          //blank lines 
      { 
       cout << " "; 
      } 
      conXY(21, 21); 
      cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N? ";         //ask question 
      cin >> cRestart;                //get input from user 
     } 

     if (cRestart == 'y' || cRestart == 'Y')            //if yes return true 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
+0

다음과 같은 방법을 작성합니다 타트 '는 항상 적어도 한 번에 그 캐릭터의 * 3 *에 불평등합니다. 현재 루프는 적어도 하나 이상은 불투명합니다. – cHao

답변

3

한숨, 드 모르 강의 법칙을 잊은 다른 프로그래머 (while 루프는, Y, N 및 N은 경우에 당신은 모든 것을 볼 수 없습니다. y를 찾습니다). 그것은 같아야

while (cRestart != 'y' && cRestart != 'Y' && cRestart != 'n' && cRestart != 'N') //check for correct input (Y/N) 
+0

고맙습니다. 아주 늦었고 프로그래밍을 처음 접했습니다. 내일 아침에 드 모간의 법을 조사 할거야. – Mitcy

+0

Cliff notes 버전 :'! (A || B) == (! A &&! B)'와'! (A && B) == (! A || B)'. – cHao

0

구조

char cRestart = 'b';   //set initial value at b to run while loop 
    // ... 

    while (cRestart != 'y' || cRestart != 'Y' || cRestart != 'n' || cRestart != 'N') //check for correct input (Y/N) 
    { 
     conXY(21, 21); 
     for(int iCount = 0; iCount < 139; iCount++)          //blank lines 
     { 
      cout << " "; 
     } 
     conXY(21, 21); 
     cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N? ";         //ask question 
     cin >> cRestart;                //get input from user 
    } 

1)에 잘못이 컨트롤은 (는) while 루프 2에서 유효 조건을 가지고 나쁘게 보인다 (그 활용도 다른 제어 구조)

내가 기능을 난 당신이`CRES을 보장 할 수

bool startInputValidation() 
{ 
    char cRestart;     //set initial value at b to run while loop 

    conRGB(COLOUR_WHITE);   //text colour white 

    conXY(0, 19);     //cursor position in console 
    drawLine();      //draws a horizontal line across screen 
    conXY(0, 23); 
    drawLine(); 


    do 
    { 
     conXY(21, 21); 
     for (int iCount = 0; iCount < 139; iCount++) 
     { 
      cout << ' '; 
     } 

     conXY(21, 21); 
     cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N? "; 

     cin >> cRestart; 
     cRestart = toupper(cRestart); 
    } while (cRestart != 'N' && cRestart != 'Y') 

    return cRestart == 'Y'; 
}