c++ - 传递错误数字的函数调用 (C++ 11)

标签 c++ function c++11

我目前正在从事一个旨在教授 C++ 函数用法的项目。我在路径中使用 python 并且对代码中的函数有合理的理解(或者我认为)但是由于某种原因,当我将值传递给我当前的函数时,我遇到了一些令人震惊的错误。

这是完整的代码,我的问题在于 main 中的 narc_num(153,3) 调用。我在 narc_num 函数中添加了一些 cout 语句以查看为什么我得到了不稳定的结果,并发现 num 参数作为一个完全不同的数字传递。为什么会这样?

#include<iostream> 
#include<cmath> 
using std::cin; 
using std::cout; 
using std::endl; 

/* 
Function: order_parameters 
Purpose: If first is greater than second reassign the values so second is greater than first 
Algorithm: 
1.Pass arguments as references to first and second 
2.if statement that runs if first is greater than second 
3.within if statement switch values using a temporrary variable 
4.if else doesnt run nothing happens 
*/ 
void order_parameters(long & first,long & second)//passing long references of first and second 
{ 
 if(first > second)//start if when the ref to first > ref to second 
 { 
 long temp; // initialize temp 
 temp = second; //value temp is = to second 
 second = first; //second will now be first, temp is still = to first though as well 
 first = temp; //second is now set = to temp, which is first. dat swap 
 }//end if 
} 

/* 
Function: narc_num 
Purpose: check if a number is indeed a narcisstic number 
Algorithm: 
1.Pass num, the number to be checked, and power, the order the number will be checked against 
2.use a while loop to iterate through digits of number 
    -mod10 takes the last digit, the digit is raised to the power passed as an argument 
    -the value found is added to the total 
    -the number is then divided by 10 to remove the last digit, reiterates again. 
3.the total found by the while loop is checked against the number passed an as argument 
    -if the total is equal to the number it is narcisstic. 
*/ 
bool narc_num(long num, long power)// will return a boolean value, passing num and power integers 
{ 
//split all digits into seperate numbers, add together raised to power 
long total = 0,digit,num_copy; 
bool narc = false; //value to check if number is narcissitic or not 
num = num_copy; 
cout << "number"<< num << endl; 
while(num > 0) 
    {digit = num%10; 
     cout <<"digit" << digit << endl; 
     total += pow(digit,power); 
     cout <<"total" << total << endl; 
     num /= 10; //divides by 10 to go to the next digit down 
    } 
if (total == num) 
    narc = true; 
cout << total << endl; 
return narc; 
} 

long check_range(long first,long last,long power) 
{ 
bool check; 
order_parameters(first,last); //make sure parameters are in correct order 
for(first;first == last;first++);//iterate through all numbers from first to last 
  {check = narc_num(first,power); //check = True if narc number otherwise false 
   if (check == true) 
   {cout << first <<" is a narcissistic number of order " <<power<< endl; 
   }; 
   cout << "gothere"<< endl; 
   }; 
cout << "dick canoe"<< endl; 
} 

int main(){ 
narc_num(153,3); 
} 

最佳答案

您正在将 num_copy 的值(一个未初始化的 long)赋给 narc_num 函数顶部附近的 num。我相信您打算为 num_copy 分配 num 的值。这可能是导致您出现意外结果的原因。

关于c++ - 传递错误数字的函数调用 (C++ 11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21536747/

相关文章:

C++:获取当前时间作为 W3C UTC 时间字符串

c++ - C++ 类中线程的函数

c++ - 函数声明中的最大参数个数

function - Call 必须与卸载部分中以 "un."开头的函数名一起使用

postgresql - 触发器中的动态函数调用

c++ - 标准的 atomic bool 和 atomic flag 之间的区别

c++ - 开始使用类进行绘图

c++ - 无法使用 XLNT 库构建 C++ 项目

C++ 新手 : Passing an fstream to a function to read data

c++ - 在 C++ 中显示一行与单词