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

您可以添加另一个选项来继续计算器。像这样的东西:


while(doContinue == true){

    switch{.....}

}

也就是说,将代码的切换部分包含在一个while循环中。首先将 doContinue 设置为 true,然后根据用户输入在最后更改它。

于 2012-04-04T16:02:10.043 回答
0

如果您将开关置于 while 循环中,以检查 char == 'n' 是否会继续,直到找到它为止。

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 回答