Critic my program -

I wrote a guess my number program but I was curious to if I did a good job organizing my program or not? I'm trying to improve my organization.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <stdlib.h>
#include <time.h>

void initializeSecretNumber(int& secret, int& level);
void getGuess(const int& secret, bool& winner, int& numGuesses, int& highscore, const int& difficultyScore);



int main()
{
    bool gameOver = false;
    int highscore = 10000;
    int difficulty;
    int numOfGuesses = 1;
    int secretNumber;

    initializeSecretNumber(secretNumber,difficulty);

    while ( !gameOver )
    {
        getGuess(secretNumber, gameOver, numOfGuesses, highscore, difficulty);
    }
    return 0;
}





void initializeSecretNumber(int& secret, int& level)
{
    std::cout << "Welcome to Guess My Number programmed by Max Daniels!\n\n" << std::endl;
    std::cout << "Choose a difficulty by typing the corresponding letter choice!" << std::endl;
    std::cout << "A) Easy (0 - 100)\nB) Medium (0 - 1000)\nC) Hard (0 - 10000)\nD) Insane (0 - 100000)\nE) Impossible (0 - 1000000)" << std::endl;
    char userInput;
    bool choiceMade = false;

    while ( !choiceMade )
    {
        std::cin >> userInput;
        userInput = toupper(userInput);

        switch ( userInput )
        {
            case 'A': level = 100; choiceMade = true;
                    break;
            case 'B': level = 1000; choiceMade = true;
                    break;
            case 'C': level = 10000; choiceMade = true;
                    break;
            case 'D': level = 100000; choiceMade = true;
                    break;
            case 'E': level = 1000000; choiceMade = true;
                    break;
            default: std::cout << "Invalid, try again." << std::endl;
        }
    }
        std::srand(std::time(NULL));
        secret = rand()%level;
}

void getGuess(const int& secret, bool& winner, int& numGuesses, int& highscore, const int& difficultyScore)
{
    int guess;
    std::cout << std::endl <<  "Guess a number: ";
    std::cin >> guess;
    if ( guess > secret )
    {
        std::cout << "You guessed to high!" << std::endl;
        numGuesses++;
    }
    else if ( guess < secret )
    {
        std::cout << "You guessed to low!" << std::endl;
        numGuesses++;
    }
    else
    {
        std::cout << "\n\nYou guessed it correctly! The secret number was " << secret << "!" << std::endl;
        highscore = (highscore - (numGuesses * 200));
        std::cout << "It took you " << numGuesses << " attempts!" << std::endl;
        std::cout << "You achieved a high score of " << highscore << std::endl;
        if ( highscore <= 5000 )
        {
            std::cout << "Did you just bang your head against your keyboard? That was terrible!" << std::endl;
        }
        else if ( highscore <= 6000)
        {
            std::cout << "You did slightly better than a button mashing 5 year old." << std::endl;
        }
        else if ( highscore <= 7000 )
        {
            std::cout << "You could have done better, you could have done worse too, I guess you're average." << std::endl;
        }
        else if ( highscore <= 8000 )
        {
            std::cout << "Wow, you did slightly better then average." << std::endl;
        }
        else if ( highscore <= 9000 )
        {
            std::cout << "You have a knack for randomly guessing numbers." << std::endl;
        }
        else if ( highscore <= 10000 )
        {
            std::cout << "Go buy a lottery ticket, NOW!" << std::endl;
        }
        else if ( highscore <= 11000 )
        {
            std::cout << "You are a ******* cheater." << std::endl;
        }
        winner = true;
    }


}
what compiler are you using?
mine isn't happy with std::time unless i #include<ctime>

(i'm on vs2013 premium)
Last edited on
Codeblocks
Thanks, I was actually curious about my organization and thought process, if it is good or not.
One suggestion: separate your difficulty selection logic from your secret number generator.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
enum Difficulty { EASY, MEDIUM, HARD, INSANE, IMPOSSIBLE };

//...

Difficulty getDifficultyFromUser()
{
    std::cout << "Choose a difficulty by typing the corresponding letter choice!" << std::endl;
    std::cout << "A) Easy (0 - 100)\nB) Medium (0 - 1000)\nC) Hard (0 - 10000)\nD) Insane (0 - 100000)\nE) Impossible (0 - 1000000)" << std::endl;
    char userInput;

    while ( true )
    {
        std::cin >> userInput;
        userInput = toupper(userInput);

        switch ( userInput )
        {
        case 'A': return Difficulty::EASY;
        case 'B': return Difficulty::MEDIUM;
        case 'C': return Difficulty::HARD;
        case 'D': return Difficulty::INSANE;
        case 'E': return Difficulty::IMPOSSIBLE;
        default: std::cout << "Invalid, try again." << std::endl;
        }
    }
}

int getSecretNumber(Difficulty dif)
{
    int maxForDifficulty;
    switch(dif)
    {
    case Difficulty::EASY:
        maxForDifficulty = 100;
        break;
    case Difficulty::MEDIUM:
        maxForDifficulty = 1000;
        break;
    case Difficulty::HARD:
        maxForDifficulty = 10000;
        break;
    case Difficulty::INSANE:
        maxForDifficulty = 100000;
        break;
    case Difficulty::IMPOSSIBLE:
        maxForDifficulty = 1000000;
        break;
    default:
        maxForDifficulty = -1;
        break;
    }
    
    return std::rand() % (maxForDifficulty + 1);
}

//...

int main()
{
    //....
    Difficulty dif = getDifficultyFromUser();
    int secret = getSecretNumber(dif);
    //....
}
Topic archived. No new replies allowed.