I am new to the C++ language and am taking a class. I have a programming assignment that needs three functions.
Function one is to display the menu and grab the user data.
Function two is to calculate the user data based on their selection.
Function three is to output the data.
I have been trying to follow tutorials but I am not grasping it.
Here is my code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
double userNumber = 0;
double calcNum_one = 0;
double calcNum_two = 0;
void menu();
double calculator(double, double);
void solution();
int main() {
do {
menu();
calculator(answer);
solution();
}
while (userNumber != 7);
return 0;
}
void menu () {
cout << "Welcome to the CIS 151 calculator." << endl;
cout << "To make a selection, enter the number and press enter." << endl;
cout << " 1. Add two numbers." << endl;
cout << " 2. Subtract one number from another." << endl;
cout << " 3. Multiply two numbers." << endl;
cout << " 4. Divide two numbers." << endl;
cout << " 5. Modulus of two numbers." << endl;
cout << " 6. Raise a number to a power." << endl;
cout << " 7. Quit the program." << endl;
cout << endl;
cout << "Number: ";
cin >> userNumber;
}
void calculator(double calcNum_one, double calcNum_two) {
if(userNumber == 1) {
system("CLS");
cout << "You have chosen to: add two numbers." << endl;
cout << "Please type in your numbers." << endl;
cout << endl;
cout << "First Number: ";
cin >> calcNum_one;
cout << "Second Number: ";
cin >> calcNum_two;
double answer = calcNum_one + calcNum_two;
return answer;
}
}
void solution() {
if(userNumber == 1) {
system("CLS");
cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << endl;
cout << endl;
system("PAUSE");
}
}
NOTE: I just put if-statements for one possibility to make it easier to read.
Can someone clean up my code to show my how to properly use the calculator function? I am getting error: 'answer' was not declared in this scope
in CodeBlocks.
Thank you to all!