Major Issues with scope and conversion

I am trying to fix a calculator that is supposed to add and subtract. I am getting scope and conversion errors. Please help

calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "calculator_funcs.h"
#include <iostream>
using namespace std;
int main(){
	// Variables to hold local data
	int firstNum, secondNum;
	int mathChoice();
	int result;
		
	// Call the showWelcome() function
	showWelcome();
	
	// Call the getInteger() function (for the first integer)
	// and store the result in the "firstNum" variable
	firstNum = GetInteger(true);
	
	
	// Call the getMathChoice() function and store result in "mathChoice" variable
	mathChoice = getMathChoice(true);
	
	// Call validateMathChoice() function, passing it the user's math choice
	// and using the return value to decide what to do next
	if (validateMathChoice())
	{
		// Call the getInteger() function (for the second and subsequent integers)	
// and store the result in the "secondNum" variable			
		secondNum = getInteger(false);
		
		// Call the doMath() function and pass it all of the user input
		// and store the return value in the "result" variable.
		result = doMath(firstNum,secondNum,mathChoice);
		
		// Call the showResult() function to show the result
		showResult(result);
	}
	else
	{
		// If the user chose an invalid math function...
		cout<< "Not a valid math choice<<endl";
	}
	
	return 0;
}


calculator_funcs.h

1
2
3
4
5
6
7
8
9
void showWelcome(void);
int getUserIntegerInput();
char getMathChoice();
int getInteger(bool);
bool validateMathChoice(char choice);
int doAddition(int int1, int int2);
int doSubtraction(int, int);
int doMath(int firstInt, int secondInt, char mathFunc);
void showResult(int);


calculator_funcs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include"calculator_funcs.h"
#include<iostream>
using namespace std;
// This function shows a nice welcome message
void showWelcome()
{
	cout<<"******************************************"<<endl;
	cout<<"Welcome to the simple calculator program!"<<endl;
	cout<<"This program will do simple addition and"<<endl;
	cout<<"subtraction. Math is fun, so enjoy!"<<endl;
	cout<<"******************************************"<<endl;
}

// This function gets integer input from the user
int getUserIntegerInput()
{
	int input;
	cin >> input;
	return input;
}

// This function asks the user for a math function
// and returns the user's input
char getMathChoice()
{
	char choice;
	cout <<"Please select a math function to perform (\"+\" = Addition, \"-\" = Subtraction): "<< endl;
	cin>> choice;
	return choice;
}

// this function asks the user for either the first integer
// or the second and returns the user's input
int getInteger(bool firstNumber)
{
	cout <<"Please enter the "<<endl;
	
	// if the "firstNumber" variable is true, then this
	// is the first number being collected
	if (firstNumber)
	{
		cout <<"first " <<endl;
	}
	// Otherwise, it's the second number being collected
	else
	{
		cout <<"second "<< endl;
	}
	
	cout <<"integer: " << endl;
	// Call the getUserIntegerInput() function and return the return value from it
	return getUserIntegerInput();
}

// This function validates the user's match function choice
// by returning a true for valid, and a false for invalid
bool validateMathChoice(char choice)
{
	if (choice == '+' && choice == '-')
	{
		return true;
	}
	else
	{
		return false;
	}
}

// This function adds two integers
int doAddition(int int1, int int2)
{
	return int1 + int2;
}

// This function subtracts the second integer
// parameter from the first integer parameter
int doSubtraction(int int1, int int2)
{
	return int1 - int2;
}


// This function determines the result of the math
// operation requested by the user
int doMath(int firstInt, int secondInt, char mathFunc)
{
	// Initialize result to zero (0)
	int result = 0;
	
	// If the math function is a "+", then call the
	// doAddition() function and store the return 
	// value in the "result" variable
	if (mathFunc = '+')
	{
		result = DoAddition( firstInt,secondInt);
	}
	// If the math function is a "-", then call the
	// doSubtraction() function and store the return 
	// value in the "result" variable
  else if (mathFunc == '-')
	{
		result = DoSubtraction(firstInt,secondInt);
	}
	
	return result;
}


// This function displays the result of a math operation
void showResult(int result)
{
	cout <<"The result is "<<  result << endl;
}
Last edited on
I see a few things - there might be more.

In the main function, I think you mean to define a variable, not a function() here on line 7.

int mathChoice();

mathChoice is defined as an integer, but getMathChoice returns a character. There's also a bool argument being sent to getMathChoice, but the definition doesn't allow for one.

mathChoice = getMathChoice(true)

char getMathChoice()

This doesn't seem correct - choice is not going to be equal to both + and - at the same time.

1
2
3
bool validateMathChoice(char choice)
{
	if (choice == '+' && choice == '-')


In the doMath function, check for assignment operators (=) being used where you want to check equality (==).
Topic archived. No new replies allowed.