Skip to content

Design and Code Main

The last function is main. This is responsible for coordinating the actions of the program. It will call play game in a loop that repeatedly plays the game until the user decides to quit. Main will need one local variable called again. This will store the text, and will be used to store the value read from the user’s response to the ‘play again’ prompt.

We can make use of our print line procedure to help separate different games. We can also use read string to check if the user wants to run this again.

Flowchart for the Main method

Flowchart for the Main method

The following pseudocode shows the steps in main.

Function: Main
-------------------------------
Local Variables:
- again (string)
Steps
Do
Play Game
Print Line
Read string with prompt "Play again? [Y or n]: " and store in again
While again != "n" and again != "N"
Pseudocode for Print Line

Have a go at coding this yourself. When this works you should be able to play multiple games of guess the number.

  • #include "splashkit.h"
    // add read string, read integer, and read integer range here
    // add print line here
    // add perform guess here
    // add play game here
    /**
    * Loops the guessing game until the user decided to quite.
    */
    int main()
    {
    string again = "";
    do
    {
    play_game();
    write_line();
    print_line(50);
    again = read_string("Do you want to play again [Y/n]? ");
    } while (again != "N" && again != "n");
    write_line("\nBye - enjoy the rest of your day!");
    return 0;
    }