从2D阵列打印输入范围

时间:2015-03-23 21:54:51

标签: arrays 2d

我需要输入要打印的范围,例如:邮政编码7003-7300。 我已经整理出代码块来处理输入范围,现在只需要将它与数组元素进行比较并相应地打印输出。要做到这一点,我需要将整数输入转换为字符串吗?数组是我生命中的祸根我一直在努力去理解它们是如何工作的。任何帮助深表感谢。

请求firstpostcode值输入的示例代码。

    printf("Enter number of first postcode to display through ranges [7000-7470]:");

    while (scanf_s(" %d", &Firstcode) != 1)
    {
        // Tell the user that the entry was invalid
        printf("Invalid Entry\n");
        // Asterisk * tells scanf to read and ignore the value
        scanf_s("%*s");
    }

    if ((Firstcode >= 7000) && (Firstcode < 7470))
    {
        printf("Displaying from postcode %d \n", Firstcode);
    }

    else
    {
        printf("...Input outside boundaries: Postcode 7000 assumed...\n");
        Firstcode = 7000;
        printf("Displaying from postcode %d \n", Firstcode);
    }

包含元素为字符串的二维邮政编码数组:     char * postcodes [613] [2] =     {

{ "7000", "Glebe" },
{ "7000", "Hobart" },
{ "7000", "Mount Stuart" },
{ "7000", "North Hobart" },
{ "7000", "West Hobart" },
{ "7001", "Hobart" },
{ "7002", "North Hobart" },
...

在获取第一个和最后一个邮政编码的正确输入值后,这是我的未完成循环:

if ((Lastcode > Firstcode) && (Lastcode <= 7470))
    {
        printf("Displaying Postcodes %d to %d:\n", Firstcode, Lastcode);

        for (i = 0; i < 613; i++)
        {
            for (j = 0; j < 2; j++)
            {

                printf("%s\t", postcodes[i][j]);
            }
            printf("\n");

0 个答案:

没有答案