简体   繁体   English

C++ 试图从 main 调用函数

[英]C++ trying to call functions from main

I'm trying to write a function that will find the reverse of a number, so if 1234 is input, the reverse is 4321.我正在尝试编写一个函数来查找数字的倒数,因此如果输入 1234,则倒数为 4321。

#include <iostream>
using namespace std;

int reverse(int);

int main()
{
    cout << "This is the special difference calculator. Please enter 
positive integers: " << endl;

reverse();

}

int reverse(int num)
{
    int num, remainder, reversenum;
    cin >> num;
    while (num != 0)
    {
        remainder = num % 10;
        reversenum = reversenum * 10 + remainder;
    }
    return reversenum;
}

I also tried making a variable in main and setting it equal to reverse(int) but it showed it was wrong.我还尝试在 main 中创建一个变量并将其设置为等于 reverse(int) 但它表明它是错误的。 I honestly have no idea what I'm doing so any help would be greatly appreciated!老实说,我不知道我在做什么,所以任何帮助将不胜感激!

You still do not understand how functions work.你仍然不明白函数是如何工作的。 Your logic was wrong (as far I was concerned) I coded this for 2 digit numbers.您的逻辑是错误的(就我而言)我将其编码为 2 位数字。 I left you the math equations for 3, 4, and 5 digit numbers.我给你留下了 3、4 和 5 位数的数学方程式。 Use nested loops or input manipulation to choose the correct equation.使用嵌套循环或输入操作来选择正确的方程。 Read the following stuff to learn more about functions.阅读以下内容以了解有关函数的更多信息。 Stack Overflow is not for homework help, next time do more reading and try more in your code before asking for help. Stack Overflow 不是为了作业帮助,下次在寻求帮助之前多阅读并尝试更多代码。 Read: http://www.cplusplus.com/doc/tutorial/functions/阅读: http : //www.cplusplus.com/doc/tutorial/functions/

#include <iostream>
using namespace std;

int rev_function (int num)
{
  int r;
   //For 2 digit numbers 
  r =  (10)*(num % 10) + (num - (num % 10)) / 10 ;

//For 3 digit numbers 
//f(x) = (100)(x % 10) + ((x - (x % 10)) % 100) + (x - (x % 100)) / 100 

//For 4 digit numbers 
//f(x) = (1000)(x % 10) + ((x - (x % 10)) % 100) * 10 + ((x - (x % 100)) % 1000) / 10 + (x - (x % 1000)) / 1000 

//For 5 digit numbers 
//f(x) = (10000)(x % 10) + ((x - (x % 10)) % 100) * 100 + ((x - (x % 100)) % 1000) + ((x - (x % 1000)) % 10000) / 100 + (x - (x % 10000)) / 10000
  return r;
}

int main ()
{
  int z;
  int num;
  cout << "\nThis is the special difference calculator.";
    cout << "\n Please enter positive integers: ";
    cin >> num;
  z = rev_function (num);
  cout << "The result is " << z;
}

So the output would be:所以输出将是:

This is the special difference calculator.
 Please enter positive integers: 12
The result is 21 

There are a few logic errors in your code.您的代码中有一些逻辑错误。 Try the following code ( please note the comments ):尝试以下代码(请注意注释):

#include <iostream>
using namespace std;

int reverse();

int main()
{
    cout << "This is the special difference calculator. Please enter  positive integers: " << endl;

    reverse();

}

int reverse()
{
    int num, remainder, reversenum=0;//reversenum should be initialized to zero
    cin >> num;
    while (num != 0)
    {
        remainder = num % 10;
        num = num/10; // Add this line so that while statement works correctly
        reversenum = reversenum * 10 + remainder;
    }
    return reversenum;
}

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

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