Building A Mad Libs Game

Lesson 12
Author : Afrixi
Last Updated : November, 2022
C++ - Programming Language
This course covers the basics of programming in C++.

Building A Mad Libs Game

a simple Mad Libs game in C++. Mad Libs is a game where one player prompts the other to provide words of a certain type, such as nouns, verbs, adjectives, etc., which are then used to fill in the blanks of a story to create a funny or absurd result.

#include <iostream>
#include <string>

int main() {
    std::string noun, verb, adjective;
    int number;
    
    std::cout << "Enter a noun: ";
    std::getline(std::cin, noun);
    
    std::cout << "Enter a verb: ";
    std::getline(std::cin, verb);
    
    std::cout << "Enter an adjective: ";
    std::getline(std::cin, adjective);
    
    std::cout << "Enter a number: ";
    std::cin >> number;
    
    std::cout << "Once upon a time, there was a " << adjective << " " << noun << "." << std::endl;
    std::cout << "This " << noun << " liked to " << verb << " " << number << " times a day." << std::endl;
    std::cout << "One day, the " << noun << " decided to go on a " << adjective << " adventure." << std::endl;
    std::cout << "The end." << std::endl;
    
    return 0;
}

In this code, the user is prompted to enter a noun, verb, adjective, and number. The inputs are stored in the variables noun, verb, adjective, and number, respectively.

Then, the program uses the user’s input to fill in the blanks of a simple story. The story is output to the console using std::cout.

Note that std::getline is used to read in the user’s input for the nouns, verbs, and adjectives, since they can contain spaces. The std::cin function is used to read in the integer value for the number.