返回到int main(),而不是上一个函数
Return to int main() instead of to previous function?
我是编程新手,这是我设计的第一个程序。这是一个计算器,在一个部分中,我要求用户输入他/她是否想输入另一个和,如果他们说是,重复输入x、输入运算符、输入y,并将它们全部相加。我唯一的问题是,如果你重复这个过程不止一次,然后完成。。。函数返回到它自己(上一个调用方),而不是像我希望的那样返回到main。到目前为止,我已经尝试了#pragma once
、#define
并使用了一个额外的函数,但解决方案仍然失败了。
我觉得这很难解释,所以这是整个程序,你可以看看。我说的是:
// Simple Calculator v3.cpp : Defines the entry point for the console application.
//
#include "tots.h"
double userInput()
{
using namespace std;
//Prompt the user to enter a digit
cout << " Please enter a digit: " << endl;
cout << " ";
//Declare a variable and assign the user's input to that variable
double input;
cin >> input;
//Return the digit the user entered
return input;
}
int userOperatorInput()
{
using namespace std;
//Prompt the user to enter an operator
cout << " Please enter the desired mathematical operation: "
<< "+ = 1; - = 2, * = 3, / = 4 " << endl;
cout << " ";
//Declare a variable and assign the user's input to that variable
int op;
cin >> op;
//Return the operator the user entered;
return op;
}
int userFinished()
{
using namespace std;
/*Ask the user whether he/she has entered the function he/she desires or if they have not
yet finished*/
cout << " Have you finished the function? "
<< "yes = 1; no = 0" << endl;
cout << " ";
//Declare a variable and assign the user's input to that variable
int userfinished;
cin >> userfinished;
//Return the user's input
return userfinished;
}
double calculateSolution(double input1, int op, double input2)
{
//Determine which operator the user entered and calculate the solution accordingly
if (op == 1)
return input1 + input2;
else if (op == 2)
return input1 - input2;
else if (op == 3)
return input1 * input2;
else if (op == 4)
return input1 / input2;
else return -1;
}
double moreCalculus(double solution)
{
double xtrainput1 = solution;
int xtraop = userOperatorInput();
double xtrainput2 = userInput();
double xtrasolution = calculateSolution(xtrainput1, xtraop, xtrainput2);
bool xtrafinished = userFinished();
if (xtrafinished == 0)
moreCalculus(xtrasolution);
else
{
#ifndef RETURN
#define RETURN
return xtrasolution;
#endif
}
}
void displaySolution(double input1, int op, double input2, double solution)
{
using namespace std;
cout << " " << input1 << " " << op << " " << input2 << " = " << solution << endl;
}
void displayXtraSolution(double xtrasolution)
{
using namespace std;
cout << setprecision(20);
cout << " Solution: " << xtrasolution;
}
void end()
{
using namespace std;
int x;
cin >> x;
}
int main()
{
//Input from the user
double input1 = userInput();
//Operation user requires
int op = userOperatorInput();
//Second input from user
double input2 = userInput();
/*Declare and assign a boolean that informs whether the user has finished
the operation or not*/
bool finished = userFinished();
//Calculate the solution
double solution = calculateSolution(input1, op, input2);
//If the user has finished, display the solution
/*If the user hasn't finished, assign the required inputs from the user and calculate the
solution*/
if (finished == 1)
{
displaySolution(input1, op, input2, solution);
}
else
{
double xtrasolution = moreCalculus(solution);
displayXtraSolution(xtrasolution);
}
/*Finally, display some information on screen and ask whether the user requires to calculate
any more operations or if he/she wants to terminate the application*/
请注意,我制作了一个包含"stdafx.h"
和<iostream>
的头文件。
您应该关注循环,如果您的代码需要停止,您只需中断它。
循环时使用do
int main(){
do{
//your code section
cout<<"Do you want to continue(Y/N):";
cin>>ch;
}while(ch=='Y'||ch=='y');
}
相关文章:
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 类模板上一个特定函数的未解析外部符号
- Rcpp:一个函数在NumericVector上工作,另一个需要将其视为标量
- 在 android ndk 中的同一.cpp文件上调用另一个 C++ 函数
- 来自上一个声明的 G++ 错误,由于包含中的函数名称重复而导致的错误
- 如何在AnoteHR CPP文件中的另一个函数上使用CPP文件的函数中的变量
- 返回到int main(),而不是上一个函数
- 如何可移植地调用一个 C++ 函数,该函数在某些平台上采用 char**,在其他平台上采用 const char**
- C ++如何创建一个函数,该函数在终端和文件上打印
- 如何正确传递指向函数的指针,C++直接在该指针上运行的另一个函数
- 编写一个函数来分配一个上三角矩阵
- 动态创建一个函数指针,该指针调用给定实例上的方法
- c++读取上一个函数堆栈帧
- 我需要一个类似std::cin的Mac上的C++函数,但有一个超时
- 成员函数返回变量的上一个值
- 模板元组-在每个元素上调用一个函数
- 当使用复制构造函数为包含它的变量分配一个新对象时,上一个对象是否已销毁
- 使用Boost Thread在具有不同输入的多个线程上运行一个函数
- 在Linux上删除或替换一个函数中重复的c++代码
- 当一个c++程序在Windows上终止时,终止存根调用的最后一个函数是什么?