将argv [1]转换为int,将其数字转换为数组

时间:2013-07-16 13:29:45

标签: c++ arrays int argv

我一直在尝试用C ++创建一个试图完成这个伪代码的程序:

get argv[1] into int

get int’s digits into array[int length]

for int i = array length; i >= 0;

gen random number into check

if check == array[i]
    i
    say Number i was check
end if

我认为我真正努力的部分是

get argv[1] into int

get int’s digits into array[int length]

一部分。在我的完整代码中甚至没有尝试,因为我尝试过的任何东西都没有。我得到的最大错误是代码编译,但每次尝试cout << "Number 1:" << number时,无论我输入的实际数字是什么,我都会得到Number 1: 0。当0 == 0代码甚至没有注意到时。

我破坏了可破坏的常规代码如下:

#include <iostream>
#include <string>

int main (int argc, char **argv) {

    if (argc == 1 || argc == 3) {
        std::cout << "Argument count does not match (one argument expected)\n";
        return(-1);
    }

    std::cout << "Input: " << argv[1] << "\n";

    const char* text = argv[1];
    int number = atoi(text);

        int check = rand() % 10;

        std::cout << "Check 1: " << check << "\nNumber 1: " <<  number << "\n";

        if (check == array[i]) {
            i++;
            std::cout << "Success! Number " << i << " was " << check << ".\n";
        }
    }
}

TL; DR:我的“那种”数字破解者不希望将argv 1放入int中,而int的数字稍后会被放入数组中。

随意让我感到愚蠢。希望问题不是太具体。我会按照要求扩展细节。

编辑:这是转换的早期尝试:

    int array[];
    for (int i = strlen(text); i >= 0; i--) {
        array[i] = number % 10;
        number /= 10;
    }

EDIT2:很多回复,没有解决方案。谢谢你试图一次解释这个新手这么多东西。顺便说一句:Git

3 个答案:

答案 0 :(得分:2)

之前的尝试几乎是好的:只是你必须为数组实际分配空间,如下所示:

int array[strlen(text)];

如果您的编译器支持可变长度数组作为扩展名,并且

std::vector<int> array;
array.resize(strlen(text));

如果你想坚持使用标准C ++并遵循一些好的做法。

但是,如果你想变得棘手,你甚至不需要将参数转换为数字:

if (argv[1][i] == check % 10 + '0')

也有诀窍。总而言之,完整的程序将如下所示:

#include <iostream>
#include <cstdlib>


int main(int argc, char *argv[])
{
    int check = std::rand();
    std::cout << check << std::endl;

    char *p = argv[1] + strlen(argv[1]);
    while (p - argv[1] >= 0) {
        if (*--p == '0' + check % 10)
            std::cout << "guessed " << p - argv[1] << "th digit" << std::endl;

        check /= 10;
    }

    return 0;
}

答案 1 :(得分:1)

您的代码相对接近正确。您正在努力处理数组的声明(您必须为其指定大小)。 32位int不能超过十位,因此声明

int array[10];

应该足够了。

在将数字转换为数字数组之前,检查它是否为负数,如果是负数则翻转其符号:

if (number < 0) {
    number = -number;
}

否则,您的number%10技巧将不起作用。

进行转换时,请计算您拥有的位数。将结果放在actualCount变量中:很可能你不会用完数组中的所有数字,所以

int check = rand() % 10; // 10 is the max, not the actual digit count

应该是

int check = rand() % actualCount;

您的参数检查还需要改进:想想如果用户传递五个参数会发生什么?如果你只想要一个参数,你应该写

if (argc != 2) {
    std::cout << "Argument count does not match (one argument expected)\n";
    return(-1);
}

答案 2 :(得分:0)

为了从一个数字中一次只提取一个数字,你有几个选择。

为方便起见,您可以使用std::string,在其中插入原始字符串(argv[1]),然后一次提取一个char

#include <string>

...    

// put the input in a string
std::string text = argv[1];

for (unsigned i = 0; i < text.size(); i++)
{
   // extract only one char, a digit
   char ch = text.at(i);
   // convert that char in a number
   int n = ::atoi(& ch);

   // use n
   ...
}

如果您不想使用std::string,则可以始终使用类似c的数组(argv[1]本身):

#include <cstring>

...    

for (unsigned i = 0; i < strlen(argv[1]); i++)
{
   // extract only one char, a digit
   char digit = argv[1][i];
   // convert that char in a number
   int num = ::atoi(& digit);

   // use n
   ...
}