Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c+中的函数错误+; 我正在学习C++中的函数,这是一本叫做“跳转到C++”的书,有一个问题是练习创建计算器,我需要在单独的函数中进行算术运算,声音很容易,我认为我做了90%好,程序给了我正确的答案,但是有一些随机数。_C++_Function - Fatal编程技术网

c+中的函数错误+; 我正在学习C++中的函数,这是一本叫做“跳转到C++”的书,有一个问题是练习创建计算器,我需要在单独的函数中进行算术运算,声音很容易,我认为我做了90%好,程序给了我正确的答案,但是有一些随机数。

c+中的函数错误+; 我正在学习C++中的函数,这是一本叫做“跳转到C++”的书,有一个问题是练习创建计算器,我需要在单独的函数中进行算术运算,声音很容易,我认为我做了90%好,程序给了我正确的答案,但是有一些随机数。,c++,function,C++,Function,代码是: #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

代码是:

#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");

}
请注意“结果是:”前面的错误:


感谢您的帮助,谢谢此函数返回一个随机整数。将其转换为void

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

}
intask()
{
cout>a;
cout>b;
}
新的

void ask()
{
cout>a;
cout>b;
}

此函数返回一个随机整数。将其转换为void

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

}
intask()
{
cout>a;
cout>b;
}
新的

void ask()
{
cout>a;
cout>b;
}

ask()
不返回任何内容,因此它应该是空的。另外,您不需要执行
cout
ask()
不会返回任何内容,因此它应该是空的。另外,您不需要执行
cout问题在于
intask()
函数。
它必须返回您正在使用
cout写入控制台的int值问题在于
int ask()
函数。
它必须返回您正在使用
cout make
ask()
return
void
写入控制台的int值,并且不要执行
cout,并确保启用和读取编译器警告-应该有一种说法,即
ask()
虽然声明返回
int
,但不会返回任何内容。我建议:(1)使用一个开关而不是if/else的长链,它更具可读性(2)使用名称空间std删除并使用std::cin(3)使用std::endl关闭输出流(4)main是一个函数,与其他函数一样,因此应该添加return 0;(它表示程序终止时没有错误)。如果您刚开始使用c++,请记住这些建议。感谢您的帮助和建议,请欣赏make
ask()
return
void
和don't
cout,并确保启用和阅读编译器警告-应该有一个说法是
ask()
虽然声明为返回一个
int
,但不返回任何内容。我建议:(1)使用一个开关,而不是长链的if/else,它更可读(2)使用名称空间std删除并使用std::cin(3)使用std::endl关闭输出流(4)main是一个函数,与其他函数一样,因此您应该添加返回0;(它表示程序终止时没有错误)。如果你是从c++开始学习的话,这些都是很好的建议,谢谢你的帮助,谢谢你的建议
void ask()
{
    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;

}
#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");
}
void ask()
{
    cout << "Give me the first number: ";
    cin >> a;
    cout << "\nGive me the second number: ";
    cin >> b;
}
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) ...