[C언어/C++] 숫자 맞추기 게임 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdlib.h>
#include <time.h> 
using namespace std;
int main(int argc, char** argv)
{
    while (1)
    {
        int m_num, m_rand;
        int cnt = 0;
        char yn = 0;
        srand((unsigned)time(NULL));
 
        m_rand = rand() % 100 + 1;
 
        while (1)
        {
            cout << “숫자를 입력하시오? “;
            cin >> m_num;
            if (m_num == m_rand || m_num == 0break;
            else if (m_num > m_rand) cout << m_num << “보다 작습니다.” << endl;
            else cout << m_num << “보다 큽니다.” << endl;
            cnt++;
        }
        if (m_num == 0) {
            cout << “포기하셨군요. 다음 기회에”;
            break;
        }
        else
        {
            cout << cnt << “번째만에 정답을 맞추셨습니다.” << endl;
            cout << “다시 도전하시겠습니까? (y or n) : “;
            while (1)
            {
                cin >> yn;
                if (yn == ‘y’ || yn == ‘n’break;
                else cout << “only ‘y’ or ‘n’” << endl;
            }
        }
        if (yn == ‘n’break;
    }
    return 0;
}
cs

무한 반복문인 while(1)과 if문으로 구현을 해줍니다.

관련글

in-coder 커뮤니티 HOT게시물

제목 작성자 작성일