0

これは非常にあいまいですが、C++は初めてです。初めてのプロジェクトとして電卓を作成しています。やりたいことは、「y」で応答した場合、スクリプトを最初から再実行することです...基本的に。

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;
int main()
    {
        cout << "Hello and Welcome to the Test Calculator!\n";
        signed char choice;
        char resp;
        cout << "Choose your problem:\n a)Addition\n b)Subtraction\n c)Multiplication\n d)Division\n e)Square Root\n f)Hypotenuse\n";
        scanf ("%c", &choice);
        switch (choice)
        {
            case 'a':
            {
                int a;
                int b;
                cout << "Addition\n";
                cout << "Please enter a number:\n";
                cin >> a;
                cout << "Please enter your second number:\n";
                cin >> b;
                cin.ignore();
                int result = a + b;
                cout << "Calculating...\n";
                cout << "Your total is:\n"<<"  "<<result;
                cin.get();
                break;
            }
            case 'b':
            {
                int c;
                int d;
                cout << "Subtraction\n";
                cout << "Please enter a number:\n";
                cin >> c;
                cout << "Please enter your second number:\n";
                cin >> d;
                cin.ignore();
                int result2 = c - d;
                cout << "Calculating...\n";
                cout << "Your total is:\n"<<"  "<<result2;
                cin.get();
                break;
            }
            case 'c':
            {
                int e;
                int f;
                cout << "Multiplication\n";
                cout << "Please enter a number:\n";
                cin >> e;
                cout << "Please enter your second number:\n";
                cin >> f;
                cin.ignore();
                int result3 = e * f;
                cout << "Calculating...\n";
                cout << "Your total is:\n"<<"  "<<result3;
                cin.get();
                break;
            }
            case 'd':
            {
                int g;
                int h;
                cout << "Division\n";
                cout << "Please enter a number:\n";
                cin >> g;
                cout << "Please enter your second number:\n";
                cin >> h;
                cin.ignore();
                int result4 = g / h;
                cout << "Calculating...\n";
                cout << "Your total is:\n"<<"  "<<result4;
                cin.get();
                break;
            }
            case 'e':
            {
                int x;
                #define square ((x)*(x))
                cout << "Square Root\n";
                cout << "Please enter a number:\n";
                cin >> x;
                cin.ignore();
                cout << "Calculating...\n";
                cout << "Your total is:\n"<<"  "<<square;
                cin.get();
                break;
            }
            case 'f':
            {
                int i;
                int j;
                cout << "Hypotenuse\n";
                cout << "Enter your smaller side:\n";
                cin >> i;
                cout << "Please enter the longer side:\n";
                cin >> j;
                cin.get();
                int hypotenuse = ((i*i)+(j*j));
                cout << "Calculating...\n";
                cout << "The hypotenuse is the square root of:\n"<<"  "<<hypotenuse;
                cin.ignore();
                cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
                cin >> resp; //this is where im trying to test this at
            }
            default:
            {
                cout << " \n";
                cout << "Error: Undefined response\n";
                cout << "Contact the source programmer for details\n";
            }
    }
    }
4

2 に答える 2

0

char == 'n' かどうかを確認する while ループにスイッチを入れると、それが見つかるまで続行されます。

while(choice != 'n')
{
    switch (choice)
    {
        case 'a':
        {
            int a;
            int b;
            cout << "Addition\n";
            cout << "Please enter a number:\n";
            cin >> a;
            cout << "Please enter your second number:\n";
            cin >> b;
            cin.ignore();
            int result = a + b;
            cout << "Calculating...\n";
            cout << "Your total is:\n"<<"  "<<result;
            cin.get();
            break;
        }
        case 'b':
        {
            int c;
            int d;
            cout << "Subtraction\n";
            cout << "Please enter a number:\n";
            cin >> c;
            cout << "Please enter your second number:\n";
            cin >> d;
            cin.ignore();
            int result2 = c - d;
            cout << "Calculating...\n";
            cout << "Your total is:\n"<<"  "<<result2;
            cin.get();
            break;
        }
        case 'c':
        {
            int e;
            int f;
            cout << "Multiplication\n";
            cout << "Please enter a number:\n";
            cin >> e;
            cout << "Please enter your second number:\n";
            cin >> f;
            cin.ignore();
            int result3 = e * f;
            cout << "Calculating...\n";
            cout << "Your total is:\n"<<"  "<<result3;
            cin.get();
            break;
        }
        case 'd':
        {
            int g;
            int h;
            cout << "Division\n";
            cout << "Please enter a number:\n";
            cin >> g;
            cout << "Please enter your second number:\n";
            cin >> h;
            cin.ignore();
            int result4 = g / h;
            cout << "Calculating...\n";
            cout << "Your total is:\n"<<"  "<<result4;
            cin.get();
            break;
        }
        case 'e':
        {
            int x;
            #define square ((x)*(x))
            cout << "Square Root\n";
            cout << "Please enter a number:\n";
            cin >> x;
            cin.ignore();
            cout << "Calculating...\n";
            cout << "Your total is:\n"<<"  "<<square;
            cin.get();
            break;
        }
        case 'f':
        {
            int i;
            int j;
            cout << "Hypotenuse\n";
            cout << "Enter your smaller side:\n";
            cin >> i;
            cout << "Please enter the longer side:\n";
            cin >> j;
            cin.get();
            int hypotenuse = ((i*i)+(j*j));
            cout << "Calculating...\n";
            cout << "The hypotenuse is the square root of:\n"<<"  "<<hypotenuse;
            cin.ignore();
            cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
            cin >> choice; //this is where im trying to test this at
        }
   }
}
于 2012-04-04T15:55:20.893 に答える
0

別のオプションを追加して、電卓を続行できます。このようなもの:


while(doContinue == true){

    switch{.....}

}

つまり、コードの切り替え部分を while ループで囲みます。最初に doContinue を true に設定し、最後にユーザー入力に従って変更します。

于 2012-04-04T16:02:10.043 に答える