簡體   English   中英

C ++中的函數錯誤

[英]Function error in c++

我正在研究c ++中的函數,形成了一本名為“跳轉到c ++”的書,有一個問題練習是創建一個計算器,我需要在單獨的函數中進行算術運算,聽起來很容易,我想我做到了90%,該程序為我提供了正確的答案,但帶有一些隨機數。

代碼是:

#include <iostream>

using namespace std;

int a, b;

int sum()
{

       return a + b;

}

int subs()
{

    return a - b;

}

int div()
{

    return a / b;

}

int mult()
{

    return a * b;

}

int ask()
{

    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;

}

int main()
{

    int opcion;

    cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
    cin >> opcion;

    if(opcion == 1)
    {

        cout << ask();

        cout << "The result is: " <<sum() <<"\n\n";

    } else if (opcion == 2)
      {

          cout << ask();

          cout << "The result is: " << subs() <<"\n\n";

      }else if (opcion == 3)
        {

            cout <<ask();

            cout << "The result is: " << div() <<"\n\n";

        }else if(opcion == 4)
         {

            cout << ask();

             cout << "The result is: " << mult() <<"\n\n";

         }else
            {

                cout << "Error.\n\n";

            }

    system("pause");

}

這就是“錯誤/錯誤/任何內容”

1. Sum
2. Substraction
3. Division
4. Multiplication

Choose one option from above:

4
Give me the first number: 5

Give me the second number: 5
1878005856The result is: 25

Press any key to continue . . .

注意“結果是:”之前的錯誤

感謝任何幫助,謝謝

ask()不返回任何內容,因此應為空。 另外,您不需要執行cout << ask(); 因為ask()已經在其中進行打印,並且它是一個空白(現在),所以無法進行打印。

這是經過修改的代碼,有關更改,請參見前面帶有****的注釋:

#include <iostream>

using namespace std;

int a, b;

int sum() {
  return a + b;
}

int subs() {
  return a - b;
}

int div() {
  return a / b;
}

int mult() {
  return a * b;
}

void ask() { // **** Changed to void here
  cout << "Give me the first number: ";
  cin >> a;
  cout << "\nGive me the second number: ";
  cin >> b;
}

int main() {
  int opcion;

  cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
  cin >> opcion;

  if (opcion == 1) {
    ask(); // **** Removed cout << 
    cout << "The result is: " << sum() << "\n\n";
  } else if (opcion == 2) {
    ask(); // **** Removed cout << 
    cout << "The result is: " << subs() << "\n\n";
  } else if (opcion == 3) {
    ask(); // **** Removed cout << 
    cout << "The result is: " << div() << "\n\n";
  } else if (opcion == 4) {
    ask(); // **** Removed cout << 
    cout << "The result is: " << mult() << "\n\n";
  } else {
    cout << "Error.\n\n";
  }
  system("pause");
}

你可以在這里嘗試

隨機數是由您執行cout << ask();引起的cout << ask(); 即使您沒有退貨。

正如aschepler指出的那樣:“確保啟用並閱讀編譯器警告-應該有這樣的說法:ask()盡管已聲明要返回int,但不會返回任何內容。”

問題是您的int ask()函數。 它必須返回使用cout << ask();寫入控制台的int值cout << ask();

上面的答案將不起作用,因為您不能將void寫入cout。

由於您不返回任何值,因此會隨機返回一個隨機數。 我的編譯器將其標記為錯誤。

替換詢問功能的類型:

void ask()
{
    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;
}

然后替換cout << ask(); 在每一個if語句中,只需ask();

像這樣:

if (opcion == 1)
    {
        ask();
        cout << "The result is: " << sum() << "\n\n";
    }
    else if (opcion == 2)
    {
        ask();
        cout << "The result is: " << subs() << "\n\n";

    }
    else if (opcion == 3) ...

如果進行除法運算,請考慮檢查b == 0。 否則,如果您嘗試將其設置為零,則您的程序將崩潰。

該函數返回一個隨機整數。 轉換為空

int ask()
{
    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;

}

void ask()
{
    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM