简体   繁体   English

将整数放入数组C ++

[英]putting ints into an array c++

this is a super simple problem but it's late and I cant figure out for the life of me why this function doesnt work. 这是一个非常简单的问题,但是已经很晚了,我无法弄清楚该功能为何不起作用。 I want it to print 1234, but instead it prints 123121. can someone explain what's going on and how to fix it? 我希望它打印1234,但是却打印123121。有人可以解释发生了什么以及如何解决它吗? thanks 谢谢

#include <iostream>

const int size = 20;

void set_int( int num )
{
    int digits[size];
    for ( int i = size - 1; i >= 0; i-- )
    {
        digits[i] = num % 10;
        num /= 10;
        if ( num != 0 )
            std::cout << num;
    }
}

int main()
{
    set_int( 1234 );
    return 0;
}

Well you are outputting the number instead of the digit . 好吧,您输出的是数字而不是数字

Try changing like, 尝试改变,

cout << digits[i] 

Further clarification :

On the first run of the loop your num will be 1234 / 10 = 123 在循环的第一次运行中,您的数字将是1234 / 10 = 123

Next run your number will be 123 / 10 = 12 下一轮您的号码将是123 / 10 = 12

Next is going to be 1 下一步将是1

You are outputing num, so you get 123121 . 您正在输出num,因此得到123121

There are several things wrong with that code. 该代码有几处错误。

Firstly, the definition 首先,定义

int digits[size];

is a variable length array, which is valid C (since the 1999 C standard) but is not valid C++. 是一个可变长度数组,它是有效的C(自1999 C标准以来),但是无效的C ++。 Unfortunately, some C++ compilers support such things as an extension. 不幸的是,某些C ++编译器支持诸如扩展之类的东西。

Second, even if we assume that definition is valid, your code is essentially stating that you need an array with 1234 elements to hold integral values corresponding to four digits (1,2,3, and 4). 其次,即使我们假设定义是有效的,您的代码实际上也表明您需要一个具有1234个元素的数组来保存对应于四个数字(1、2、3和4)的整数值。

As MichaelCMS has described, your code is outputting something other than the digits too. 如MichaelCMS所述,您的代码也输出除数字以外的其他内容。 A value of 1234 has 4 digits, so you would need to loop a total of 4 times to find all digits (if doing it right). 值1234有4位数字,因此您需要总共循环4次才能找到所有数字(如果操作正确)。 You would not need to loop 1234 times. 您无需循环1234次。

MichaelCMS explained correctly, why you have such output. MichaelCMS正确解释了为什么会有这样的输出。 There are mistakes in your function. 您的功能存在错误。 I wrote another one. 我写了另一个。

You can use next code, which helps to find digits of number. 您可以使用下一个代码,这有助于查找数字位数。

#include <iostream>

int FindNumberOfDigits(int number);
void SplitNumberIntoDigits(int number);  

// Splits number into digits. 
// Works with not big numbers.
void SplitNumberIntoDigits(int number)
{
    int size = FindNumberOfDigits(number);      

    int * digits = new int[size];

    int divider = 0;
    int degree = 0;     

    for(int digit = size; digit > 0; digit --)
    {
        // Find degree of divider
        degree = digit; 

        // Find divider for each digit of number.
        // For 1234 it will be 1000. For 234 it will be 100.            
        divider = pow(10, degree - 1);

        // We use "abs" to get digits without "-".
        // For example, when -1234 / 1000, you get -1.
        digits[digit] = abs(number / divider);  

        // Cut number to find remaining digits.
        number %= divider;                      
        std::cout << digits[digit];
    }   

    std::cout << std::endl; 
}

// If number = 0, number of digits will be 1.
// Else returns number of digits.   
int FindNumberOfDigits(int number)
{
    int digitsNumber = 0;   

    if (number)
    {
        // calculates number of digits
        while (number / 10)
        {
            number /= 10;
            digitsNumber ++;
        }                  
    }

    digitsNumber += 1;          

    return digitsNumber;        
}


int _tmain(int argc, _TCHAR* argv[])
{   
    SplitNumberIntoDigits(1234);
    SplitNumberIntoDigits(0);
    SplitNumberIntoDigits(1);
    SplitNumberIntoDigits(-1234);
    SplitNumberIntoDigits(1234567890);  

    return 0;
}

As a result this code can help you to find digits of not big numbers. 因此,此代码可以帮助您查找数字不大的数字。 It works with positive, negative numbers and zero. 它适用于正数,负数和零。

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

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