3

For a technical writing class I will be leading and having the rest of the class (on the school computers with Visual Studio 2013) participate in making a a simple guess game program. I am trying to make it as straight forward as possible and easy to understand as many of my classmates are not programmers.

Is there anything I can make simpler or easier to follow?

using namespace std;
int main(){
    char response = 'y';

    while (response != 'n'){
        srand(time(NULL));
        int guess = -1;
        int answer = (rand() % 100) + 1;

        while (guess != 0){
            cout << "Guess a number between 1 and 100. Guess 0 to quit game." << endl;
            cin >> guess;
            if (guess == 0){
                break;
            }
            else if (guess == answer){
                cout << "Correct!" << endl;
                break;
            }
            else if (guess < answer){
                cout << "Too low, guess again!" << endl;
            }
            else {
                cout << "Too high, guess again!" << endl;
            }
        }
        cout << "Play again? (y/n): ";
        cin >> response;
    }
}
joe_young
  • 4,107
  • 2
  • 27
  • 38
Obyss
  • 39
  • 2
  • 2
    Write it as pseudo-code – Abdul Rehman Oct 23 '15 at 20:15
  • 3
    Only call `srand` once. The condition in your while loop will never be false. – Neil Kirk Oct 23 '15 at 20:17
  • 2
    One thing I would suggest is get rid of `using namespace std;`. see: [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – NathanOliver Oct 23 '15 at 20:18
  • The game can loop to be played multiple times without exiting, if 'srand' was only called once wouldnt the random number be the same each time? – Obyss Oct 23 '15 at 20:19
  • In addition to the code, a flow-chart would be nice to help explain the code logic for non-programmers. – wendelbsilva Oct 23 '15 at 20:25
  • 2
    `srand` seeds the random number generator and sets up the starting point for all following random numbers provided by `rand`. After `srand`, each random number will be the next number in the randomizer algorithm's sequence based on the seed provided by `srand`. Re calling `srand` with change the seed and give different random numbers unless it is called so quickly that the seed didn't change. By the way, I recommend ditching `srand` and `rand` in favour of [the random number generation library added in C++11](http://en.cppreference.com/w/cpp/numeric/random). – user4581301 Oct 23 '15 at 20:56
  • @user4581301 But would that really be beneficial in a demo for non-programmers? I feel that it would just add to the overwhelming-ness of it all. :) – Numeri Oct 23 '15 at 21:05
  • Good question, @Numeri . In it's simplest form it's not much uglier than `srand`/`rand` to set up, but then a lot easier to use. [I play around with random 1 to 100 in this answer](http://stackoverflow.com/questions/31059976/my-program-loops-forever-and-does-only-a-half-of-what-i-told-it-to-do/31061880#31061880), so take a look and decide. – user4581301 Oct 23 '15 at 21:14
  • @Numeri Programming is hard. Non-programmers need to know this ;) – Neil Kirk Oct 23 '15 at 21:36
  • @NeilKirk :) But do we want to amaze them with our incredibly mighty magic powers, or make it look easy at first, and **then** lure them slowly into the lair called Computer Science, where they will remain trapped for years to come? :D – Numeri Oct 23 '15 at 21:39
  • I've found it surprisingly difficult to explain to non-programmers how `if` and `while` work, mind you that generally requires understanding types, boolean logic, and grasping "blocks" as an idea. Don't expect this to be easy. – Ryan Haining Oct 24 '15 at 01:57

1 Answers1

1

Consider dropping the "play again" or drop the "Guess 0 to quit". In fact, drop the "quit" because the Ctrl-C or close-button of the terminal are already there.

Rename answer to something less confusing (pick or secret) and make it const.

Extract a function for the "voodoo" to generate a random number. Makes it more legible!

Turn the while into a do {} while so you don't have this unintuitive check response != 'n' before anything ever happened at all!

Same for while(guess!=0). Except, you can lose the redundant condition altogether. You already had break there...

Lose the else if where break already made the branch redundant.

Make a polite comment about missing error handling... So people don't sue you when their programs runs haywire :)

Also, write it incrementally, so do e.g.

#include <iostream>

using namespace std;

void play_round();

int main() {
    srand(time(NULL));

    char response;
    do {
        play_round();

        cout << "Play again? (y/n): ";
        cin >> response;
    } while (response != 'n');
}

int pick_random(int from, int to) {
    return (rand() % (to-from+1)) + from;
}

void play_round() {
    const int secret = pick_random(1, 100);

    do {
        cout << "Guess a number between 1 and 100: ";

        int guess;
        cin >> guess;

        if (guess == secret) {
            cout << "Correct!" << endl;
            break;
        } 
    } while (true);
}

And then elaborate adding

        if (guess == 0) {
            break;
        }

And eventually

        if (guess < secret) {
            cout << "Too low, guess again!" << endl;
        }

        if (guess > secret) {
            cout << "Too high, guess again!" << endl;
        }

Note how the branches are independent!

Optionally elaborate, doing:

int pick_random(int from, int to) {
    return (rand() % (to-from+1)) + from;
}

and use pick_random(1, 100) etc :)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • In the interest of overkill (@JerryCoffin), here's something more viable I'd like to end up with with a serious group of C++ pupils: http://coliru.stacked-crooked.com/a/0f3d5fcfa4223dc0 – sehe Oct 23 '15 at 22:50