Home » A C++ program that displays a random quote

A C++ program that displays a random quote

by pqzrmred71

Here’s a C++ program that displays a random quote each time it’s run.

The quotes are stored in an array, and the program selects one randomly using the rand() function.

Code

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

int main() {
    // Array of quotes
    const std::string quotes[] = {
        "The only limit to our realization of tomorrow is our doubts of today. – Franklin D. Roosevelt",
        "Do not wait to strike till the iron is hot; but make it hot by striking. – William Butler Yeats",
        "Whether you think you can or think you can’t, you’re right. – Henry Ford",
        "The future belongs to those who believe in the beauty of their dreams. – Eleanor Roosevelt",
        "It does not matter how slowly you go as long as you do not stop. – Confucius",
        "Act as if what you do makes a difference. It does. – William James",
        "Success is not how high you have climbed, but how you make a positive difference to the world. – Roy T. Bennett",
        "Life is 10% what happens to us and 90% how we react to it. – Charles R. Swindoll",
        "The best way to predict the future is to create it. – Peter Drucker",
        "You miss 100% of the shots you don’t take. – Wayne Gretzky"
    };

    // Get the number of quotes
    const int num_quotes = sizeof(quotes) / sizeof(quotes[0]);

    // Seed the random number generator
    std::srand(std::time(0));

    // Generate a random index
    int random_index = std::rand() % num_quotes;

    // Display the randomly selected quote
    std::cout << "Random Quote: " << std::endl;
    std::cout << quotes[random_index] << std::endl;

    return 0;
}

Explanation of the Code

  1. Include Required Libraries:
    • #include <iostream>: Allows for input and output operations (e.g., std::cout).
    • #include <cstdlib>: Provides the std::rand() and std::srand() functions for random number generation.
    • #include <ctime>: Provides std::time() for seeding the random number generator.
    • #include <string>: Allows using std::string for storing the quotes.
  2. Define an Array of Quotes:
    • const std::string quotes[]: An array of quotes, where each element is a std::string representing a quote.
    • Each quote is assigned as a string literal within double quotes.
    • The array is marked const to prevent accidental modification.
  3. Calculate the Number of Quotes:
    • const int num_quotes = sizeof(quotes) / sizeof(quotes[0]);
      • sizeof(quotes): Gets the total size of the quotes array in bytes.
      • sizeof(quotes[0]): Gets the size of a single element in the array.
      • Dividing these gives the number of elements (quotes) in the array.
  4. Seed the Random Number Generator:
    • std::srand(std::time(0));: Seeds the random number generator with the current time. This ensures that each time the program runs, it generates a different random number.
  5. Generate a Random Index:
    • int random_index = std::rand() % num_quotes;
      • std::rand() % num_quotes generates a random number between 0 and num_quotes – 1, giving a valid index within the array.
  6. Display the Random Quote:
    • std::cout << “Random Quote: ” << std::endl;: Prints a header for the quote.
    • std::cout << quotes[random_index] << std::endl;: Prints the randomly selected quote from the array.
  7. Return Statement:
    • return 0;: Ends the program successfully.

Sample Output

Each time you run the program, it will display a different quote. Example output might look like:

Random Quote: 
The only limit to our realization of tomorrow is our doubts of today. – Franklin D. Roosevelt

or

Random Quote: 
Act as if what you do makes a difference. It does. – William James

Notes

  • Randomness: Seeding with std::srand(std::time(0)); ensures that the random number generator produces different results each time the program is run.
  • Adding More Quotes: To add more quotes, simply add more elements to the quotes array. The program will automatically calculate the new array size.

Summary

This C++ program is a simple random quote generator that selects a random quote from a predefined list.

It uses std::srand() and std::rand() for generating random indices and std::string for storing quotes.

This program is an excellent example for practicing arrays, random number generation, and string handling in C++.

You may also like