-1

制作了我的第一个 C++ 程序。一个基本的计算器。卡在了我应该让程序重复的部分。

因此,在“A 点”,您选择是否要计算除法/加法等,它会将您带到那里。当您运行一次时,它会询问您是要重复该功能(例如除法)还是返回“A点”(您只需键入 y[yes](重复除法)或 n[no](转到“点 A"))。我是 C++ 新手,还没有真正熟悉循环。代码结构也让我头晕目眩,所以谷歌对我帮助不大。我听说过“goto”函数(或任何你称之为的函数),但有人告诉我在这种情况下不应该使用我。

看一看。文本和大部分评论都是芬兰语,但我希望你能从英文评论中明白这一点。

#include <iostream>
using namespace std;

float addition(float num1, float num2)
{
    return num1 + num2;
}
float substraction(float num1, float num2)
{
    return num1 - num2;
}
float multiplication(float num1, float num2)
{
    return num1 * num2;
}
float division(float num1, float num2)
{
    return num1 / num2;
}
//This function should throw you back to point 'A'
int valinta2{
    while (valinta2 == y){

    }
}


int main(void)
{
    //Point A
    float number1;
    float number2;
    int valinta;
    cout << "\n-*-*-*-*-*-*-*-*-*-*\nClaudion Laskin\n-*-*-*-*-*-*-*-*-*-*";
//Select what you want to count
    cout << "\n\n\nValitse mita haluat laskea.  \n\nVaihtoehdot: " << endl;
    cout << "1. Plus-laskut\n 2. Vahennys-laskut\n  3. Kerto-laskut\n   4. Jako-laskut \n\nValinta: ";
    cin >> valinta;

    if (valinta == 1){

        //Addition
        cout << "\n\n\n===============\n||Plus laskut||\n=============== \n\nSyota ensimmainen numero: ";
        cin >> number1;
        cout << "\n\n+\n\nSyota toinen numero: ";
        cin >> number2;
        cout << "\nTulos: " << addition(number1, number2) << "\n----------\n" << endl;
        cin.get();
        //if 'y' run the task again, if 'n' goto start
        cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
        cin.get();
    }

    else {
        if (valinta == 2){
            //Subtraction
            cout << "\n\n\n===================\n||Vahennys laskut||\n=================== \n\nSyota ensimmainen numero: ";
            cin >> number1;
            cout << "\n\n-\n\nSyota toinen numero: ";
            cin >> number2;
            cout << "\nTulos: " << substraction(number1, number2) << "\n----------\n" << endl;
            cin.get();
            //if 'y' run the task again, if 'n' goto start
            cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
            cout << "Valinta: ";
            cin >> valinta2;
        }
        else {
            if (valinta == 3){
                //Multiplication
                cout << "\n\n\n================\n||Kerto laskut||\n================ \n\nSyota ensimmainen numero: ";
                cin >> number1;
                cout << "\n\n*\n\nSyota toinen numero: ";
                cin >> number2;
                cout << "\nTulos: " << multiplication(number1, number2) << "\n----------\n" << endl;
                cin.get();
                //if 'y' run the task again, if 'n' goto start
                cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nKirjoita 'y' jos haluat jatkaa, 'n' jos haluat valikkoon\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
                cin.get();
            }
            else {
                if (valinta == 4){
                    //Division
                    cout << "\n\n\n===============\n||Jako laskut||\n=============== \n\nSyota ensimmainen numero: ";
                    cin >> number1;
                    cout << "\n\n/\n\nSyota toinen numero: ";
                    cin >> number2;
                    cout << "\nTulos: " << division(number1, number2) << "\n----------\n" << endl;
                }
            }
        }
    }
    system("pause");
    return 0;
}
4

3 回答 3

2

您需要重组代码以while在 main 方法中包含循环。有办法“去”你的“A点”,但它们很乱,不应该使用(goto这里是关键字)。

所以在“A点”,插入

do {
    float number1;
    // ...

然后在你想调用的地方valinta2(注意你现在甚至没有调用这个函数——我猜它必须在调用之前system("pause")),检查条件,像这样:

} while (...);

最好重温您选择的 C++ 教程中关于流控制/循环的章节,例如这里提到的其中之一:权威 C++ 书籍指南和列表

于 2013-10-22T13:39:04.667 回答
1

Alternative to nyarlathotep's perfectly good answer, one method I like using follows this form:

while(true) {
    //do stuff
    //then when ready to check for exit condition
    if(exitCondition == true) { //the ==true part is redundant
        break;
    }
    //do more stuff
    //if you need to go to the beginning of the loop and 
    //skip any code following a point, do this:
    if(skipRestOfLoopCondition) {
        continue;
    }
    //you can always check other exit conditions or check 
    //the same one at multiple places
    if(someOtherExitCondition) {
        break;
    }
    //do even more stuff
}
system("pause");
return 0;

As a caveat, you must provide a way for at least one of the if's that result in break; to actually execute, because while(true) loop condition never gets you out of the loop, you can only get out of the loop with a break;.

于 2013-10-22T13:47:42.423 回答
-1

我对此很陌生,我不知道我是否正确。我几乎看不懂你的代码。但是,当我在 c 中为 arduino 编程并且我希望我的代码永远循环时,我只是放了 void loop() {//我的代码从这里开始

于 2013-10-22T13:41:15.467 回答