【问题标题】:Return to int main() instead of to previous function?返回 int main() 而不是上一个函数?
【发布时间】:2016-03-29 14:35:23
【问题描述】:

我是编程新手,这是我设计的第一个程序。这是一个计算器,在一个部分中,我要求用户输入他/她是否想输入另一个总和,如果他们说是,重复输入 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"&lt;iostream&gt; 的头文件。

【问题讨论】:

  • 如果您认为#pragma once 会有所帮助,那么您真的需要回到您拥有的任何资源来学习 C++。话虽如此,你想要的是一个循环。

标签: c++ visual-studio function calculator


【解决方案1】:

您应该查看loops,如果您的代码需要阻止您,只需跳出它即可。

【讨论】:

    【解决方案2】:

    使用 do..while 循环

        int main(){
        do{
        //your code section
        cout<<"Do you want to continue(Y/N):";
        cin>>ch;
        }while(ch=='Y'||ch=='y');
        }
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2016-05-28
      • 2012-01-10
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      相关资源
      最近更新 更多