如何在下一个函数中读取外部"switch case"的状态?



我正试图弄清楚如何在ReSults函数中读取开关情况的状态,如代码中所示。。。result函数应从calculation函数读取switch状态并对其进行处理

代码:

void CalCulation()
{
cout << "Choice: "; char UserChoice; cin>>UserChoice;
double vAdd1, vAdd2;
switch(UserChoice)
{
case '+':
cout << " + :: Enter the first value: "; cin << vAdd1;
cout << " + :: Enter the second value: "; cin >> vAdd2;
ReSults(vAdd1, Vadd2);
}
}
void ReSults(double vAdd1, double vAdd2, char UserChoice)
{
if(UserChoice == '+')
{
double reSultOutputAdd = vAdd1+vAdd2;
cout << "The result for addition: "<< reSultOutputAdd << 'n';
}
}

Full version

#include <iostream>
#include <string>
#include <limits>
using namespace std;
//Calculator
void MainMenu();
void CalCulation();
void ReSults(double , double, char);
bool isValid(string err_msg)
{
if(cin.rdstate())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>:: max(), 'n');
system("cls");
MainMenu();
return false;
}
return true;
}
int main()
{
MainMenu();
cout << "nnnnnn The program runs Correctly" << 'n';
return 0;
}
void MainMenu()
{
cout << "Main Menu" << 'n';
cout << "Enter + for Calculation" << 'n';
cout << "Enter - for Subtraction" << 'n';
cout << "Enter * for Multiplication" << 'n';
cout << "Enter / for Devision" << 'n';
cout << "------------------------------" << 'n';
cout << "Chose any option for the list." << 'n';
CalCulation();
}
void CalCulation()
{
cout << "Choice: "; char UserChoice; cin>>UserChoice;
double vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2;
switch(UserChoice)
{
case '+':do{
cout << " + :: Enter the first value: "; cin >> vAdd1;
cout << " + :: Enter the second value: "; cin >> vAdd2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vAdd1, vAdd2, UserChoice);break;
case '-':do{
cout << " - :: Enter the first value: "; cin >> vSub1;
cout << " - :: Enter the second value: "; cin >> vSub2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vSub1, vSub2, UserChoice);break;
case '*':do{
cout << " * :: Enter the first value: "; cin >> vMul1;
cout << " * :: Enter the second value: "; cin >> vMul2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vMul1, vMul2, UserChoice);break;
case '/':do{
cout << " / :: Enter the first value: "; cin >> vDiv1;
cout << " / :: Enter the second value: "; cin >> vDiv2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vDiv1, vDiv2, UserChoice);break;
default:
cout << "Wrong Choice or Not allowed, Try again!" << 'n';
}
}
void ReSults(double vAdd1, double vAdd2, double vSub1, double vSub2, double vMul1, double vMul2, double vDiv1, double vDiv2, char UserChoice)
{
if(UserChoice == '+')
{
double reSultOutputAdd = vAdd1+vAdd2;
cout << "The result for addition: "<< reSultOutputAdd << 'n';
}else if(UserChoice == '-')
{
double reSultOutputSub = vSub1-vSub2;
cout << "The result for addition: "<< reSultOutputSub << 'n';
}else if(UserChoice == '+')
{
double reSultOutputMul = vMul1*vMul2;
cout << "The result for addition: "<< reSultOutputMul << 'n';
}else if(UserChoice == '+')
{
double reSultOutputDiv = vDiv1/vDiv2;
cout << "The result for addition: "<< reSultOutputDiv << 'n';
}
}
void CalCulation();
void ReSults(double, double, char);
void CalCulation()
{
cout << "Choice: "; char UserChoice; cin >> UserChoice;
double vAdd1, vAdd2;
switch (UserChoice)
{
case '+':
cout << " + :: Enter the first value: "; cin >> vAdd1;
cout << " + :: Enter the second value: "; cin >> vAdd2;
ReSults(vAdd1, vAdd2, UserChoice);
}
}
void ReSults(double vAdd1, double vAdd2, char UserChoice)
{
if (UserChoice == '+')
{
double reSultOutputAdd = vAdd1 + vAdd2;
cout << "The result for addition: " << reSultOutputAdd << 'n';
}
}
int main() {
CalCulation();  
}
#include <iostream>
#include <string>
#include <limits>
using namespace std;
//Calculator
void MainMenu();
void CalCulation();
void ReSults(double , double, double , double, double , double, double , double, char);
bool isValid(string err_msg)
{
if(cin.rdstate())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>:: max(), 'n');
system("cls");
MainMenu();
return false;
}
return true;
}
int main()
{
MainMenu();
cout << "nnnnnn The program runs Correctly" << 'n';
return 0;
}
void MainMenu()
{
cout << "Main Menu" << 'n';
cout << "Enter + for Calculation" << 'n';
cout << "Enter - for Subtraction" << 'n';
cout << "Enter * for Multiplication" << 'n';
cout << "Enter / for Devision" << 'n';
cout << "------------------------------" << 'n';
cout << "Chose any option for the list." << 'n';
CalCulation();
}
void CalCulation()
{
cout << "Choice: "; char UserChoice; cin>>UserChoice;
double vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2;
switch(UserChoice)
{
case '+':do{
cout << " + :: Enter the first value: "; cin >> vAdd1;
cout << " + :: Enter the second value: "; cin >> vAdd2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2, UserChoice);break;
case '-':do{
cout << " - :: Enter the first value: "; cin >> vSub1;
cout << " - :: Enter the second value: "; cin >> vSub2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2, UserChoice);break;
case '*':do{
cout << " * :: Enter the first value: "; cin >> vMul1;
cout << " * :: Enter the second value: "; cin >> vMul2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2, UserChoice);break;
case '/':do{
cout << " / :: Enter the first value: "; cin >> vDiv1;
cout << " / :: Enter the second value: "; cin >> vDiv2;
}while (!isValid("You have not entered any number, try again with a correct input data."));
ReSults(vAdd1, vAdd2, vSub1, vSub2, vMul1, vMul2, vDiv1, vDiv2, UserChoice);break;
default:
cout << "Wrong Choice or Not allowed, Try again!" << 'n';
}
}
void ReSults(double vAdd1, double vAdd2, double vSub1, double vSub2, double vMul1, double vMul2, double vDiv1, double vDiv2, char UserChoice){
if(UserChoice == '+'){
double reSultOutputAdd = vAdd1+vAdd2;
cout << "The result for Addition: "<< reSultOutputAdd << 'n';
}else if(UserChoice == '-'){
double reSultOutputSub = vSub1-vSub2;
cout << "The result for Subtraction: "<< reSultOutputSub << 'n';
}else if(UserChoice == '+'){
double reSultOutputMul = vMul1*vMul2;
cout << "The result for Multiplication: "<< reSultOutputMul << 'n';
}else if(UserChoice == '+'){
double reSultOutputDiv = vDiv1/vDiv2;
cout << "The result for Division: "<< reSultOutputDiv << 'n';
} }

好吧,你可以制作一个bool,并像这个一样检查bool是否为真

bool addition = false;
void CalCulation()
{
cout << "Choice: "; char UserChoice; cin>>UserChoice;
double vAdd1, vAdd2;
switch(UserChoice)
{
case '+':
cout << " + :: Enter the first value: "; cin << vAdd1;
cout << " + :: Enter the second value: "; cin >> vAdd2;
addition = true;
ReSults(vAdd1, Vadd2)
}
}
void ReSults(double, double)
{
if(addition)
{
double reSultOutputAdd = vAdd1+vAdd2;
cout << "The result for addition: "<< reSultOutputAdd << 'n';
}
}

最新更新