Kyles calculator program should allow a user to enter in num
Kyle\'s calculator program should allow a user to enter in numbers to either add or subtract. Help Kyle by fixing all of his coding mistakes so that his program will successfully compile and run as outlined above. Be sure to make comments in the code to highlight where Kyle went wrong.
You may work with others but each individual is required to submit a solution.
After you get the code working, divide the code into three files:
calculator_funcs.h - The function prototypes
calculator_funcs.cpp - The function definitions
calculator.cpp - the main-function
Ensure you can compile and run the program after it has been separated into separate files.
Here is an example of how the program should operate:
i was having problem, with this question, i just could not, figure it out, can anyone help me, pls.
this is the question, and this is the code , that has to be corrected (down)
1 /***********************************************************
2 Program Name: Simple Math Calculator
3 Program Author: Kyle NoCompile
4 Date Created: 9/12/16
5 Program Description:
6 This program performs simple arithmetic calculations.
7 The user enters numbers and the math operation to
8 perform on those numbers. The program will then display
9 the result of the operation.
10
11 Modified Date:
12 Modified Description:
13 ***********************************************************/
14
15 #include <iostream>
16 using namespace std;
17
18 // Function prototypes:
19 void showWelcome(void);
20 int getUserIntegerInput();
21 char getMathChoice()
22 int getInteger(bool);
23 bool validateMathChoice(char choice)
24 int doAddition(int int1, int int2);
25 int doSubtraction(int, int);
26 int doMath(int firstInt, int secondInt, char mathFunc);
27 void showResult(int)
28
29
30 // This is the main function (where the program begins)
31 int main()
32 {
33 // Variables to hold local data
34 int firstNum;int secondNum;
35 int mathChoice();
36 int result;
37
38 // Call the showWelcome() function
39 showWelcome(void);
40
41 // Call the getInteger() function (for the first integer)
42 // and store the result in the \"firstNum\" variable
43 firstNum = GetInteger(true);
44
45
46 // Call the getMathChoice() function and store result in \"mathChoice\" variable
47 mathChoice = getMathChoice(42);
48
49 // Call validateMathChoice() function, passing it the user\'s math choice
50 // and using the return value to decide what to do next
51 if (validateMathChoice())
52 {
53 // Call the getInteger() function (for the second and subsequent integers)
54 // and store the result in the \"secondNum\" variable
55 secondNum = getInteger(false);
56
57 // Call the doMath() function and pass it all of the user input
58 // and store the return value in the \"result\" variable.
59 result = doMath(firstNum secondNum mathChoice);
60
61 // Call the showResult() function to show the result
62 showResult(result);
63 }
64 else
65 {
66 // If the user chose an invalid math function...
67 cout<<Not a valid math choice<<endl;
68 }
69
70 return 0;
71 }
72
73
74 // This function shows a nice welcome message
75 void showWelcome()
76 {
77 cout<<\"******************************************\"<<endl;
78 cout<<\"Welcome to the simple calculator program!\"<<endl;
79 cout<<\"This program will do simple addition and\"<<endl
80 cout<<\"subtraction. Math is fun, so enjoy!\"<<endl;
81 cout<<\"******************************************\"<<endl;
82 }
83
84 // This function gets integer input from the user
85 string getUserIntegerInput(void)
86 {
87 int input;
88 cin<<input;
89 return input;
90 }
91
92 // This function asks the user for a math function
93 // and returns the user\'s input
94 char getMathChoice()
95 {
96 char choice;
97 cout<<endl<<\"Please select a math function to perform (\\\"+\\\" = Addition, \\\"-\\\" = Subtraction): \";
98 cin>>choice;
99 Return choice;
100 }
101
102 // this function asks the user for either the first integer
103 // or the second and returns the user\'s input
104 int getInteger(bool firstNumber)
105 {
106 cout<<endl<<\"Please enter the \";
107
108 // if the \"firstNumber\" variable is true, then this
109 // is the first number being collected
110 if (firstNumber)
111 {
112 cout<<\"first \";
113 }
114 // Otherwise, it\'s the second number being collected
115 else
116 (
117 cout<<\"second \";
118 )
119
120 cout<<\"integer: \";
121
122 // Call the getUserIntegerInput() function and return the return value from it
123 return = getUserIntegerInput();
124 }
125
126
127
128 // This function validates the user\'s match function choice
129 // by returning a true for valid, and a false for invalid
130 bool validateMathChoice(char choice)
131 {
132 if (choice == \'+\' && choice == \'-\')
133 {
134 return true;
135 }
136 else
137 {
138 return false;
139 }
140 }
141
142 // This function adds two integers
143 int doAddition(int int1, int int2);
144 {
145 return int1 + int2;
146 }
147
148 // This function subtracts the second integer
149 // parameter from the first integer parameter
150 int doSubtraction(int int1, int int2);
151 {
152 return int1 - int2;
153 }
154
155
156 // This function determines the result of the math
157 // operation requested by the user
158 int doMath(int firstInt, int secondInt, char mathFunc)
159 {
160 // Initialize result to zero (0)
161 int result == 0;
162
163 // If the math function is a \"+\", then call the
164 // doAddition() function and store the return
165 // value in the \"result\" variable
166 if (mathFunc = \'+\')
167 {
168 result = DoAddition(firstInt, secondInt);
169 }
170 // If the math function is a \"-\", then call the
171 // doSubtraction() function and store the return
172 // value in the \"result\" variable
173 elseif (mathFunc == \'-\')
174 {
175 result = DoSubtraction(firstInt, secondInt);
176 }
177
178 return result;
179 }
180
181 // This function displays the result of a math operation
182 bool showResult(int result)
183 {
184 cout<<endl<<\"The result is \"<<result<<endl;
185 }
****************************************** Welcome to the simple calculator program! This program will do simple addition and subtraction. Math is fun, so enjoy! ****************************************** Please enter the first integer: 42 Please select a math function to perform (\"+\" = Addition, \"-\" = Subtraction): + Please enter the second integer: 17 The result is 59 |
Solution
// calculator_funcs.h - The function prototypes
#include <iostream>
using namespace std;
#ifndef calculator_funcs_H
#define calculator_funcs_H
class Calculator
{
public:
// Function prototypes:
void showWelcome();
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);
};
#endif
// calculator_funcs.cpp - The function definitions
#include <iostream>
#include \"calculator_funcs.h\"
using namespace std;
// This function shows a nice welcome message
void Calculator::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 Calculator::getUserIntegerInput()
{
int input;
cin >> input;
return input;
}
// This function asks the user for a math function
// and returns the user\'s input
char Calculator::getMathChoice()
{
char choice;
cout<<endl<<\"Please select a math function to perform (\\\"+\\\" = Addition, \\\"-\\\" = Subtraction): \";
cin>>choice;
return choice;
}
// this function asks the user for either the first integer
// or the second and returns the user\'s input
int Calculator::getInteger(bool firstNumber)
{
cout<<endl<<\"Please enter the \";
// if the \"firstNumber\" variable is true, then this
// is the first number being collected
if (firstNumber)
{
cout<<\"first \";
}
// Otherwise, it\'s the second number being collected
else
{
cout<<\"second \";
}
cout<<\"integer: \";
// 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 Calculator::validateMathChoice(char choice)
{
if (choice == \'+\' || choice == \'-\')
{
return true;
}
else
{
return false;
}
}
// This function adds two integers
int Calculator::doAddition(int int1, int int2)
{
return int1 + int2;
}
// This function subtracts the second integer
// parameter from the first integer parameter
int Calculator::doSubtraction(int int1, int int2)
{
return int1 - int2;
}
// This function determines the result of the math
// operation requested by the user
int Calculator::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 Calculator::showResult(int result)
{
cout<<endl<<\"The result is \"<<result<<endl;
}
// calculator.cpp - the main-function
#include <iostream>
#include \"calculator_funcs.h\"
using namespace std;
// This is the main function (where the program begins)
int main()
{
Calculator c;
// Variables to hold local data
int firstNum;int secondNum;
char mathChoice;
int result;
// Call the showWelcome() function
c.showWelcome();
// Call the getInteger() function (for the first integer)
// and store the result in the \"firstNum\" variable
firstNum = c.getInteger(true);
// Call the getMathChoice() function and store result in \"mathChoice\" variable
mathChoice = c.getMathChoice();
// Call validateMathChoice() function, passing it the user\'s math choice
// and using the return value to decide what to do next
if (c.validateMathChoice(mathChoice))
{
// Call the getInteger() function (for the second and subsequent integers)
// and store the result in the \"secondNum\" variable
secondNum = c.getInteger(false);
// Call the doMath() function and pass it all of the user input
// and store the return value in the \"result\" variable.
result = c.doMath(firstNum ,secondNum, mathChoice);
// Call the showResult() function to show the result
c.showResult(result);
}
else
{
// If the user chose an invalid math function...
cout<< \"Not a valid math choice\"<<endl;
}
return 0;
}
/*
output:
******************************************
Welcome to the simple calculator program!
This program will do simple addition and
subtraction. Math is fun, so enjoy!
******************************************
Please enter the first integer: 4
Please select a math function to perform (\"+\" = Addition, \"-\" = Subtraction): +
Please enter the second integer: 5
The result is 9
******************************************
Welcome to the simple calculator program!
This program will do simple addition and
subtraction. Math is fun, so enjoy!
******************************************
Please enter the first integer: 7
Please select a math function to perform (\"+\" = Addition, \"-\" = Subtraction): -
Please enter the second integer: 2
The result is 5
*/









