2016-08-03 2 views
-5

그래서 사용자에게 5가 아닌 다른 숫자를 입력하도록 요청하는 기본 프로그램을 작성하려고합니다. 그리고 숫자 5를 입력하지 않은 사용자의 10 회 반복 이후에 프로그램에서 인쇄를 원합니다. 화면에. 여기에 지금까지 내 코드입니다 : 난 그냥 화면에 10 개 반복에서 루프 출력을 정지하기 위해 컴퓨터를 이야기하는 방법을 모른다C++ : 반복 루핑 시간 중

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

int main(){ 

    int num; 

    cout << "Please enter a number other than 5." << endl; 
    cin >> num; 

    while (num != 5){ 
     cout << "Please enter a number other than 5." << endl; 
     cin >> num; 
    } 

    return 0; 
} 

은.

+3

카운터를 추적하십시오 .. – Li357

+0

사용자가 'while'루프 중에 5를 입력하면 어떻게됩니까? –

+0

안녕 루이! ** Stackoverflow에 오신 것을 환영합니다 !!! ** ... 귀하의 질문에, 나는 당신에게 [** this **] (http://stackoverflow.com/questions/388242/the-definitive-c- book-guide-and-list)을 읽고 적어도 두 가지 선택을하고, 읽은 다음, 여기로 돌아와서 질문 할 수 있습니다. 우리는 당신을 도울 것입니다 :-) – WhiZTiM

답변

1

이의 상태를 평가 한 후 어떤 조건을 평가하지 않고 그것이 블록 내에서 문을 실행할되어 작동

do while 

루프

방법을 활용하기 위해 적절한 시간이다 루프를 다시 실행해야하는지 확인하십시오.

이것은 프로그램의 모양입니다.

#include <iostream> 
using namespace std; 

int main(void) 
{ 
int counter = 0, num; 
do 
{ 
if (counter > 10) // or >=10 if you want to display if it is 10 
{ 
cout << "exiting the loop!" << endl; 
break; // the break statement will just break out of the loop 
} 
cout << "please enter a number not equal to 5" << endl; 
cin >> num; 
counter++; // or ++counter doesn't matter in this context 

} 
while (num != 5); 
return 0; 
} 
-2
#include <iostream> 
#include <string> 
using namespace std; 

int main(){ 

    int num; 
    int counter=1; 

    cin >> num; 
    cout <<num; 
    if(num==5) 
    cout << "Please enter a number other than 5." << endl; 



    while (num != 5&&counter<=10){ 
     cin >> num; 
     cout <<num; 
     if(num==5) 
     cout << "Please enter a number other than 5." << endl; 
     counter=counter+1; 
    } 

    return 0; 
} 
+0

사용자가 처음 5 번을 입력하면, 프로그램이 작동하지 않습니다. –

+0

그가 묻는 것입니다 .... 사용자가 5를 입력하면 루프를 종료해야합니다. – rUCHit31