Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- /* These are prototypes of functions. This means that when the program starts, it moves from top to bottom (Chronologically).
- Since these are declared above main, they can be used anywhere within main as the program refers to the top which directs the program down to the bottom where the function
- containing the code is placed.
- Having lots of functions can help clean up code and allow people to more easily intepret the code within a program. Good for groupwork. Makes main look
- a lot cleaner.*/
- int ager (int &age);
- string namer (string &name);
- int Addition (double num1, double num2, double total);
- int Minus (double num1, double num2, double total);
- int Division (double num1, double num2, double total);
- int Modulus (double num1, double num2, double total);
- int Counter (double num1);
- int main()
- {
- char response; // Response is given by the user to choose an option from the menu. This is decided through the use of an if statement.
- string name; // This is typed by the user.
- int age; // Determined by user.
- double num1, num2, total; // For math functions. Values are decided later in the program by the user.
- system("clear");
- cout << "\nWelcome to my program" << endl;
- while(1) { // while(1) is an endless loop which means after the user is finished with whatever they're doing within the loop, it will always come back to the menu.
- //Menu containing options for user to choose from
- cout << "\n1. Age" << endl;
- cout << "2. Name" << endl;
- cout << "3. Age and Name" << endl;
- cout << "------------------" << endl;
- cout << "4. Add numbers" << endl;
- cout << "5. Minus numbers" << endl;
- cout << "6. Divide numbers" << endl;
- cout << "7. Modulus (Remainder)" << endl;
- cout << "8. Counter" << endl;
- cout << "------------------" << endl;
- cout << "Q. Quit\n> " ;
- cin >> response; // Takes the user's response.
- if (response == '1') { // If response is 1 then it will carry out the code within the squiggly brackets. Else it will progress onto the next parameter to see if it's true.
- ager (age);
- cout << "You are " << age << " years old." << endl;
- } else if (response == '2') { // If response is not equal to 2 then move on.
- namer (name);
- cout << "You are " << name << "." << endl;
- } else if (response == '3') {
- ager (age);
- namer (name);
- cout << "You are "<< name << " and are " << age << " years old."<< endl;
- } else if (response == '4') { // Addition
- system("clear");
- cout << "Welcome to Addition." << endl;
- Addition (num1, num2, total);
- } else if (response == '5') { // Minus
- system("clear");
- cout << "Welcome to Minus." << endl;
- Minus (num1, num2, total);
- } else if (response == '6') { // Divide
- system("clear");
- cout << "Welcome to Divide." << endl;
- Division (num1, num2, total);
- } else if (response == '7') { //Modulus
- system("clear");
- cout << "Welcome to Modulus." << endl;
- Modulus (num1, num2, total);
- } else if (response == '8') { // Counter
- system("clear");
- cout << "Welcome to the Counter." << endl;
- Counter (num1);
- } else if (response == 'q' || response == 'Q') { // If response is equal to q or Q (Lowercase or higher case) then exit loop and come to end of the program.
- exit(0);
- } else {
- cout << "Give a valid response please." << endl; // If the response given by the user doesn't meet any of the criteria set above then output response here.
- }
- cout << "\nPress any key to continue..." ;
- cin.ignore();
- cin.get(); // These two lines pause the program after the user has selected a choice and carried out the functions purpose.
- system("clear");
- }
- return 0;
- }
- int ager (int &age) // Function for age. Need to use Ampersand symbol to pass out the variable value from the function to main.
- {
- cout << "Enter your age :" ;
- cin >> age; // cin is only used for any number data type or chars.
- cin.get();
- return age;
- }
- string namer (string &name) // Need to use ampersand to pass out the value of name to main function.
- {
- cout << "Enter your name : " ;
- getline(cin, name); // This is the line of code you use to retrieve a string from the user. It takes two parameters. The type (cin) and the variable name.
- return name;
- }
- int Addition (double num1, double num2, double total)
- {
- cout << "Enter number 1: " ;
- cin >> num1;
- cout << "Enter number 2: " ;
- cin >> num2;
- total = num1 + num2;
- cout << num1 << " + " << num2 << " = " << total << endl;
- return total;
- }
- int Minus (double num1, double num2, double total)
- {
- cout << "Enter number 1: " ;
- cin >> num1;
- cout << "Enter number 2: " ;
- cin >> num2;
- total = num1 - num2;
- cout << num1 << " - " << num2 << " = " << total << endl;
- return total;
- }
- int Division (double num1, double num2, double total)
- {
- cout << "Enter number 1: " ;
- cin >> num1;
- cout << "Enter number 2: " ;
- cin >> num2;
- total = num1 / num2;
- cout << num1 << " / " << num2 << " = " << total << endl;
- return total;
- }
- int Modulus (double num1, double num2, double total)
- {
- cout << "Enter number 1: " ;
- cin >> num1;
- cout << "Enter number 2: " ;
- cin >> num2;
- total = num1 / num2;
- cout << num1 << " goes into " << num2 << ", " << total << " times."<< endl;
- return total;
- }
- int Counter (double num1)
- {
- int i; // Declaring variable i within the function.
- cout << "Enter a number for this program to count up to : ";
- cin >> num1;
- for (i=0; i < num1; i++) // Standard 3 parameters of a for loop counter.
- {
- cout << i+1 << endl; // i has '+1' because the counter starts from 0 meaning it would only reach 19.
- }
- return num1;
- }
- char Reuse (char &response) {
- cout << "Would you like to use this again?" ;
- cin >> response;
- if (response == 'y' || response == 'Y')
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment