C++ Debug help!!!! PLEASE HELP I am not allowed to use global variables!! Phase
ID: 3589324 • Letter: C
Question
C++ Debug help!!!!
PLEASE HELP
I am not allowed to use global variables!!
Phase 1
Write a program that prints a menu. Based on the menu item selected by the user, the program should
request the required inputs from the user
call the appropriate function
print the function’s result (from the main program NOT the function)
For example, if the user selections the sum function, then your program will ask them for two numbers to be added together, call the sum function, and finally print the sum function’s result.
NOTE: In case of a typo on here – make your output match the CA output!
The functions to implement are:
Simple Sum: Takes two int parameters and returns their sum. You will need to ask the user for two integers before calling this function.
Simple Division: Takes two int parameters and returns their floating point quotient (e.g., 10/3 returns 3.33). You will need to ask the user for two integers before calling this function.
Complex Division: Takes four int parameters, sums the first three parameters and then divides the sum by the fourth parameter. Returns the floating point quotient (e.g., (10 + 7 + 5) / 4 returns 5.5). You will need to ask the user for three integers and an integer divisor before calling this function.
(note - all function parameters will need to match the requirements of the assignment. - thus you do not use arrays in this assignment anywhere)
Design:
It is imperative that each of these three operations be in their own function.
You should have an additional function that determines the operation type by printing the menu and then returning the integer that the user selects.
Your main program should use a switch statement that switches on the numeric integer entered by the user to select the correct function to call.
Your switch statement should also gather the inputs from the user before calling the function.
You will print the result returned by the function in your switch statement.
All function prototypes will be listed at the top of your code (after using namespace std;) and all your function definitions to be placed after main.
Please help me debug the code I have written for this so far. My Do while is not working, it is supposed to keep running that loop multiple times so the user can do multiple operations. I am not sure how to do this. Also I am having an error with simple division "Invalid operands of types ‘int’ and ‘’ to binary ‘operator<<’ "
#include
#include
using namespace std;
int main(){
int menu();
int SimpleSum(int, int);
double SimpleDivision(int, int);
//double ComplexDivision(int, int, int, int);
int int_1;
int int_2;
switch(menu())
{
case 1:
cout << "Please enter two integers:" << endl;
cin >> int_1;
cin >> int_2;
SimpleSum(int_1, int_2);
cout << "sum = " << SimpleSum(int_1, int_2) << endl;
break;
case 2:
cout << "Please enter two integers: " << endl;
cin >> int_1;
cin >> int_2;
SimpleDivision(int_1, int_2) << endl;
cout << "quotient = " << SimpleDivision(int_1, int_2);
break;
/* case 3:
ComplexDivision();
break; */
}
return 0;
}
//////////////////////////////////////////////////////////////////
int menu() {
int menu_choice;
do{
cout << "Please make a selection from the following menu"<< endl;
cout << endl;
cout << "1. Simple Sum "
"2. Simple Division "
"3. Complex Division "
"4. Quit " << endl;
cout << "Enter the number of the operation you wish to perform: " ;
cin >> menu_choice;
return menu_choice;
} while (menu_choice != 4);
return 0;
}
///////////////////////////////////////////////////////////////////
int SimpleSum(int int_1, int int_2){
int sum;
sum = int_1 + int_2;
return sum ;
}
///////////////////////////////////////////////////////////////////
double SimpleDivision( int int_1, int int_2){
double quotient;
quotient = (int_1 / int_2) ;
return quotient;
}
///////////////////////////////////////////////////////////////////
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
// List of function prototypes
int menu();
int SimpleSum(int, int);
double SimpleDivision(int, int);
double ComplexDivision(int, int, int, int);
int int_1;
int int_2;
while(1) // Infinite loop
{
switch(menu())
{
case 1:
cout << "Please enter two integers:" << endl;
cin >> int_1 >> int_2;
cout << "sum = " << SimpleSum(int_1, int_2) << endl;
break;
case 2:
cout << "Please enter two integers: " << endl;
cin >> int_1 >> int_2;
if(int_2!=0)
cout << "quotient = " << SimpleDivision(int_1, int_2) << endl;
else
cout << "Denominator should not be zero" << endl;
break;
case 3:
int int_3, int_4;
cout << "Please enter four integers: " << endl;
cin >> int_1 >> int_2 >> int_3 >> int_4;
if(int_4 != 0)
cout << "complex division = " << ComplexDivision(int_1,int_2,int_3,int_4) << endl;
else
cout << "4th integer should not be zero " << endl;
break;
case 4: return 0;
default: cout << "Please enter correct choice ...." << endl;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////
int menu()
{
int menu_choice;
cout << "Please make a selection from the following menu"<< endl;
cout << endl;
cout << "1. Simple Sum ";
cout << "2. Simple Division ";
cout << "3. Complex Division ";
cout << "4. Quit " << endl;
cout << "Enter the number of the operation you wish to perform: " ;
cin >> menu_choice;
return menu_choice;
}
///////////////////////////////////////////////////////////////////
int SimpleSum(int a, int b)
{
// a and b are the formal arguments. Formal arguments are local variable, becuase, this function contained a and b values are
// accessible within this function only
return a + b;
}
///////////////////////////////////////////////////////////////////
double SimpleDivision( int a, int b)
{
double quotient;
quotient = ((double)a / b) ;
return quotient;
}
double ComplexDivision(int a, int b, int c, int d)
{
return ( (double) (a + b + c) / d );
}
/*
So, I am no where using the global variables. The funtion prototypes also declared with in the main. that means
menu, SimpleSum, SimpleDivision and ComplexDivision can be accessed and call from only main function
lenovo@lenovo-Vbox:~/chegg$ g++ -Wall calc.cpp
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 1
Please enter two integers:
5
6
sum = 11
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 2
Please enter two integers:
5
2
quotient = 2.5
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 2
Please enter two integers:
8
0
Denominator should not be zero
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 3
Please enter four integers:
10 5 6 7
complex division = 3
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 8
Please enter correct choice ....
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 3
Please enter four integers:
8 3 2 0
4th integer should not be zero
Please make a selection from the following menu
1. Simple Sum
2. Simple Division
3. Complex Division
4. Quit
Enter the number of the operation you wish to perform: 4
lenovo@lenovo-Vbox:~/chegg$
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.