简体   繁体   中英

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]

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

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM