简体   繁体   English

如何将值返回到另一个函数

[英]How to return a value to another function

I cannot seem to figure out how to make this work any help would be greatly appreciated as i have been trying for days :( 我似乎无法弄清楚如何使这项工作有效,因为我已经尝试了几天了:(

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

using namespace std;

int input;
int sum;
int number1;
int number2;
int number3;

void isArmstrong (int input, int sum)
{
    if (input == sum)
        cout << input << " is an Armstrong number" << endl;

    if (input != sum)
        cout << input << " is not an Armstrong number" << endl;
}

cubeOfDigits does not return input and sum to isArmstrong, (return input,sum) error is as follows: expression result unused [-Wunused-value] cubeOfDigits不返回输入和之和为isArmstrong,(返回输入和)错误如下:表达式结果未使用[-Wunused-value]

int cubeOfDigits (int input, int sum, int number1, int number2, int number3)
{
    cout << "Enter an integer between 0-999" << endl;
    cin >> input;

    number1 = input / 100;
    number2 = input % 100;
    number3 = number2 % 10;

    sum = pow(number1, 3) + pow(number2, 3) + pow(number3, 3);

    isArmstrong(input, sum);

    return input,sum;
}

main calls cubeOfDigits 主要调用cubeOfDigits

int main(void)
{
    cout << "Welcome" << endl;

    cubeOfDigits(input, sum, number1, number2, number3);

    return 0;
}

将cubeofdigits函数从(int)cubeofdigits更改为(void)cubeofdigits,然后从cubeofdigits函数末尾删除return语句。然后它必须起作用。

I think what you intended was.. 我想你的意图是..

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

using namespace std;

int input;
int sum;
int number1;
int number2;
int number3;

void isArmstrong(int input, int sum){

if(input == sum)
cout << input << " is an Armstrong number" << endl;
if(input != sum)
cout << input << " is not an Armstrong number" << endl;

}

int cubeOfDigits (int input)
{



number1 = input/100;
number2 = input % 100;
number3 = number2 % 10;
number2 = number2/10;
sum = pow(number1,3) + pow(number2,3) + pow(number3,3);


return sum;

}

int main(void){
cout << "Welcome" << endl;
cout << "Enter an integer between 0-999" << endl;
cin >> input;
isArmstrong(input,cubeOfDigits(input));
return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将返回值传递给另一个 function 并将返回值分配给该 function 内的变量? - How do I pass a return value to another function and assign the return value to a variable within that function? 如何在 C++ 中使用另一个 function 的返回值 - How to use the return value from one function in another in C++ 使用 args 传递函数并将值返回给另一个函数 - Pass function with args and return value to another function 如何在另一个成员函数中用常量返回类型修改成员函数的返回值? - How to modify the return value of a member function with constant return type within another member function? C ++-在另一个函数中使用的返回值 - C++ - return value used in another function 一个检测参数类型,值以及另一个函数的名称和返回值的函数? - A function that detects the parameter type, value, and the name and return value of another function? c ++如何在另一个函数中立即使用函数的返回值作为参考 - c++ how to use return value of function immediately in another function as reference 如何在函数中返回通用值 - How to return a generic value in a function 当调用另一个模拟 function 时更改模拟 function 的返回值 - Change return value of a mock function when another mock function is called 使用constexpr函数的返回值作为另一个函数的参数 - Using return value of constexpr function as parameter to another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM