-1

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!

Gavin Youker
  • 330
  • 2
  • 6
  • 21
  • calculator(answer); - where is answer defined???? – OldProgrammer May 03 '16 at 00:33
  • That is the question, I am not sure how to properly define `answer`. My thought was to put it in the calculator function... `double answer = calcNum_one + calcNum_two;` – Gavin Youker May 03 '16 at 00:34
  • 4
    Have you tried reading any information about what functions are, why we use them, how parameter-passing works, and how return values work? It looks like you've tried to hit the ground running here, and there are many errors in the code. Just take a step back, and find the most basic example on the internet (and there are thousands) of a C++ program that takes a parameter, computes something and returns a result. – paddy May 03 '16 at 00:37
  • 1
    This is a question best answered with [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 May 03 '16 at 00:39
  • @GavinYouker You declared `userNumber`, `calcNum_one` and `calcNum_two` globally. Why do you think the same method won't apply for `answer`? – MikeCAT May 03 '16 at 00:40
  • 2
    Doesn't the class have an instructor or TA? Aren't they teaching you this stuff? Is there a textbook? How can they give an exercise that expects you to know this if they never taught it? – Barmar May 03 '16 at 00:46
  • @GavinYouker "My thought was to put it in the calculator function". I just went to the bank, and they told me they couldn't find my account! Sure, my money is with Wells Fargo, but shouldn't Bank of America be able to look me up anyway?!? Functions are a distinct scope, and variables are scoped. Variables live in the scope where you declare them. They can be passed between functions as parameters but I don't know if your class has covered that yet. – kfsone May 03 '16 at 01:01
  • Sorry guys. I know this looks like I don't know what I'm doing. This course is fast passed and office hours are closed this week. – Gavin Youker May 03 '16 at 02:10

1 Answers1

1

answer is not declared, so it cannot be used.

You decided to use global vairables, so use them consistently.

#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;
double answer = 0;

void menu();
void calculator();
void solution();

int main() {
    do {
        menu();
        calculator();
        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() {
    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;

        answer = calcNum_one + calcNum_two;
    }
}

void solution() {
    if(userNumber == 1) {
        system("CLS");
        cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << endl;
        cout << endl;
        system("PAUSE");
    }
}

Of course you shouldn't use global variables. using namespace std; and even using are also hated from some people.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>

double menu();
double calculator(double&, double&, double);
void solution(double, double, double, double);

int main() {
    double userNumber;
    double calcNum_one;
    double calcNum_two;
    double answer;
    do {
        userNumber = menu();
        answer = calculator(calcNum_one, calcNum_two, userNumber);
        solution(calcNum_one, calcNum_two, answer, userNumber);
    }
    while (userNumber != 7);

    return 0;
}

double menu () {
    double userNumber;
    std::cout << "Welcome to the CIS 151 calculator." << std::endl;
    std::cout << "To make a selection, enter the number and press enter." << std::endl;
    std::cout << "   1. Add two numbers." << std::endl;
    std::cout << "   2. Subtract one number from another." << std::endl;
    std::cout << "   3. Multiply two numbers." << std::endl;
    std::cout << "   4. Divide two numbers." << std::endl;
    std::cout << "   5. Modulus of two numbers." << std::endl;
    std::cout << "   6. Raise a number to a power." << std::endl;
    std::cout << "   7. Quit the program." << std::endl;
    std::cout << std::endl;
    std::cout << "Number: ";
    std::cin >> userNumber;
    return userNumber;
}

double calculator(double& calcNum_one, double& calcNum_two, double userNumber) {
    double answer = 0;
    if(userNumber == 1) {
        system("CLS");
        std::cout << "You have chosen to: add two numbers." << std::endl;
        std::cout << "Please type in your numbers." << std::endl;
        std::cout << std::endl;
        std::cout << "First Number: ";
        std::cin >> calcNum_one;
        std::cout << "Second Number: ";
        std::cin >> calcNum_two;

        answer = calcNum_one + calcNum_two;
    }
    return answer;
}

void solution(double calcNum_one, double calcNum_two, double answer, double userNumber) {
    if(userNumber == 1) {
        system("CLS");
        std::cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << std::endl;
        std::cout << std::endl;
        system("PAUSE");
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Lot of code there. Recommend highlighting the changes made to the OP is less likely to miss them. I like leaving non-compiling comments in the code to force cut-and-paste-ers to deal with them. – user4581301 May 03 '16 at 00:42
  • Exactly what I was looking for. Cleared up my thinking alot. – Gavin Youker May 03 '16 at 02:41