2017-11-14 1 views
-4

브레이크 문이 전혀 루프를 벗어나지 않는 것 같아서 choice while 루프가 무한합니다. 따라서 프로그램이 answer 루프를 읽지 않습니다. 또한, "잘못된 항목"표시 다른 모든 잘못 입력하는 대신 매번 잘못된 문자가 표시가 입력왜 루프가 붙어 있습니까

#include <iostream> 
#include <cctype> 
using namespace std; 

int getAges(int age, const int SIZE); 
char getChoice(); 
void displayInOrder(int numbers[], const int SIZE, char choice); 
void displayInReverse(int numbers[], const int SIZE, char choice); 

int main() 
{ 
const int SIZE = 5; 
int numbers[SIZE] = { 1, 2 ,3 ,4, 5 }; 
char answer = 0; 
int age = 0; 
char choice = 0; 

while (choice = getChoice()) 
{ 
    if (toupper(choice) == 'O') 
    { 
     displayInOrder(numbers, SIZE, choice); 
     break; 
    } 
    else if (toupper(choice) == 'R') 
    { 
     displayInReverse(numbers, SIZE, choice); 
     break; 
    } 
    else 
    { 
     cout << "Invalid entry! - Must be O or R\n\n"; 
     break; 
    } 
} 

while (toupper(answer) == 'Y') 
{ 
    system("cls"); 

    age = getAges(age, SIZE); 
    choice = getChoice(); 
    displayInOrder(numbers, SIZE, choice); 
    displayInReverse(numbers, SIZE, choice); 

    cout << "Run program again (Y or N)? "; 
    cin >> answer; 

    if (toupper(answer) == 'N') 
    { 
     exit(); 
    } 
} 
return 0; 
} 

int getAges(int age, const int SIZE) 
{ 
cout << "Enter " << SIZE << " ages: \n\n"; 
cin >> age; 
cout << endl; 
cin >> age; 
cout << endl; 
cin >> age; 
cout << endl; 
cin >> age; 
cout << endl; 
cin >> age; 
cout << endl; 
return age; 
} 

char getChoice() 
{ 
char choice; 
cout << "How do you want to see the ages displayed? \n\n Enter O for In Order, or R for In Reverse.\n\n"; 
cin >> choice; 

return choice; 

} 

void displayInOrder(int numbers[], const int SIZE, char answer) 
{ 
    cout << "Here are the ages in order: \n\n"; 
    for (int i = 0; i < SIZE; i++) 
    { 
     cout << numbers[i] << endl; 
    } 
} 

void displayInReverse(int numbers[], const int SIZE, char answer) 
{ 
    cout << "Here are the ages in reverse order: \n\n"; 
    for (int i = SIZE - 1; i >= 0; i--) 
    { 
     cout << numbers[i] << endl; 
    } 
} 
+0

'getAges = (numbers, SIZE);'Wut ?? – user0042

+0

모든 문제를 해결할 수는 없지만 'O'와 'R'주변에 작은 따옴표가 없습니다. – George

+3

[The Definitive C++ Book Guide and List]의 가능한 복제본 (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user0042

답변

0

1. "잘못된 항목은"다른 모든 잘못된 입력을 표시하면 while 루프 내부 getChoice() 호출을 여기()가

while (choice = getChoice()) 

따라서 이전에 getChoice getChoice() 호출 뒤에

else 
{ 
    cout << "Invalid entry! - Must be O or R\n\n"; 
    choice = getChoice(); 
} 

처리되지 않는다.

2. 당신은 당신이 이제까지 두 번째에 도달 할 수 있는지에 대해 생각해야한다 "나는 사용자가 대답에 대한 N을 입력하면 프로그램을 종료하는 방법을 잘 모르겠어요"while 루프와 위치를 설정하는 answer 변수 나 기본적으로 프로그램을 끝내기 위해 사용자 입력을받는 위치는 어디입니까? 처음 while 루프에서 처리 할 수 ​​있는지 확인해야합니다.

+0

쉽게 지금까지 나를 돕는 데 가장 많은 노력을 기울였습니다. 그리고 그것에 대해 감사드립니다. else 문에서'choice = get Choice();'를 제거해도 아무 것도 바뀌지 않으며 두 번째 부분에서는 main 내부에서 while 루프를 함께 사용하거나 if 문을'answer' 루프에 넣으려고합니까? –

+0

우선, else 문에서 choice = get Choice();를 제거하면 실제로 잘못된 유형의 메시지가 대체됩니다. 확인 : https://ideone.com/wt4Z86 – tinus91

+0

두 번째 부분에서는 while 루프가 작동하는 방식을 확인해야합니다. 첫 번째 루프는 결코 끝나지 않는 루프입니다. 최소한 루프에서 빠져 나가는 방법에 대해 생각해야합니다. – tinus91

관련 문제