2016-03-29 6 views
-2

私はプログラミングの初心者です。これは私が設計している最初のプログラムです。それは計算機であり、1つのセクションでは、別の合計を入力するかどうかを入力するようにユーザーに依頼し、そうであれば、入力x、入力演算子、yを入力してすべての処理を追加します。私が抱えている唯一の問題は、プロセスを複数回繰り返して終了すると、関数が自分自身(前回の呼び出し元)に戻り、必要なようにメインに戻りません。これまで私は#pragma once#defineを試していましたが、余分な機能を使用していましたが、解決策はまだ下に行きます。前の関数ではなく、int main()に戻りますか?

私はこれを説明するのが難しいと思うので、ここではプログラム全体が何であるかを見ることができます。私は"stdafx.h"<iostream>が含まれているヘッダファイルを作っ

// 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*/ 

注:私は話しています。

+3

あなたが考える場合は 'の#pragma once'何とか役立つだろう、そしてあなたは*本当に*バックあなたがC++を学ぶために持っているものは何でもリソースに移動する必要があります。つまり、あなたが望むのはループです。 – Biffen

+0

のように前後にジャンプすると、[BASIC](https://en.wikipedia.org/wiki/BASIC)のような憎まれた言葉が、[*スパゲッティコード*](https://en.wikipedia .org/wiki/Spaghetti_code)は、追跡、理解、維持が不可能ではありません。 –

答えて

1

あなたのコードがただちに中断する必要がある場合は、loopsが必要です。

0

使用do..whileループの場合

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