Program to display a pesudo-random message to a user
Write a program that asks the user to enter any number of her choice. When she enters her number, your program should use that number in a certain way to display one of these five messages: "Hello", "Hi", "Fine", "OK" and "Nice". The user will have a feeling that she is getting a random message. Use a loop to keep asking her and keep showing her a message each time.
There are five messages, we can use a modulus operator with a switch to show a different message to the user.
int main() { cout << "Enter any number: "; int i; while(true) { cin >> i; switch(i % 5) { case 0: { cout << "Hello" << endl; } break; case 1: { cout << "Hi" << endl; } break; case 2: { cout << "Fine" << endl; } break; case 3: { cout << "OK" << endl; } break; default: case 4: { cout << "Nice" << endl; } break; } } return 0; }
My C/C++ Videos on Youtube
Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.
This Blog Post/Article "Program to display a pesudo random message to a user" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2016-03-18