Hi, everyone. I'm asking you this question because I don't understand something because I'm a C++ beginner.
#include <iostream>
using namespace std;
// *** Declaration of Global Variables ***
// #1
int m1, m2;
char choice;
// *** Circular of function ***
// #1
char menu1();
void get_number();
double add();
double subtract();
double multiply();
double divide();
double calculate();
int main() //main
{
//*****************************< lab7 #1 >**********************************
// #1 Formula Calculator for Four Rules: Programming Practice Questions #11
//**************************************************************************
cout << "lab7 #1" << endl << endl;
char q = 'q';
char Q = 'Q';
while (choice != q || Q) {
menu1();
get_number();
calculate();
}
system("pause");
return 0;
} } // main
//**************<lab7 #1 Function >**************************************************
char menu1()
{
cout <<"+ addition" << endl;
cout <<"- Subtraction" << endl;
cout <<"* multiplication" << endl;
cout <<"/ division" << endl;
cout <<"Q end" << endl;
cout <<endl << "Choose the type of operation: ";
cin >> choice;
return choice;
}
void get_number()
{
cout << "Enter two integers (e.g. 34) ==> ";
cin >> m1 >> m2;
}
// Addition, Subtraction, Multiplication, and Division Functions
double add()
{
static int count1 = 0;
if (choice == '+') {
count1++;
cout << "Additional function" << count1 << "Called number of times." <<< endl;
cout << m1 << " + " << m2 << " = " << m1 + m2 << endl << endl;
}
return m1 + m2;
}
double subtract()
{
static int count2 = 0;
if (choice == '-') {
count2++;
cout << "The subtraction function is" << count2 << "Called number of times." <<< endl;
cout << m1 << " - " << m2 << " = " << m1 - m2 << endl << endl;
}
return m1 - m2;
}
double multiply()
{
static int count3 = 0;
if (choice == '*') {
count3++;
cout << "The multiplication function is" << count3 << "Called number of times." <<< endl;
cout << m1 << " * " << m2 << " = " << m1 * m2 << endl << endl;
}
return m1 * m2;
}
double divide()
{
static int count4 = 0;
if (choice == '/') {
count4++;
cout << "The division function is" << count4 << "Called number of times." <<< endl;
cout << m1 << " / " << m2 << " = " << m1 / m2 << endl << endl;
}
return m1 / m2;
}
// Formula Calculation Function
double calculate()
{
switch (choice) {
case '+':
add();
break;
case '-':
subtract();
break;
case '*':
multiply();
break;
case '/':
divide();
break;
}
return choice;
}
We are currently writing the code in this way, and the while loop of the main function is the problem.
If you press q or Q after the word "Choose the type of operation:", the menu1();, get_number();, calculate(); I want to exit the whole while statement, but get_number() is executed again. If you change to while(true) and add an if statement, it will run only once and exit when you press any key, not just q or Q. What's the problem?
while-loop function
while (choice != q || Q) {
menu1();
get_number();
calculate();
}
Even if you change the value of choice to q/Q in menu1(), both of the remaining functions are executed because the end of the while statement has not been reached.
menu1();
while (choice != q || Q) {
get_number();
calculate();
menu1();
}
I think you can change it like this. And there are many unnecessary returns for each function. The return value literally returns the value of the function, but I don't think there's a need to return it without receiving that value. For example, in the menu function,
cin>>choice;
return choice;
There is a return of an unnecessary choice value even though the value of the global variable choice is entered.
© 2024 OneMinuteCode. All rights reserved.