热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

程序按顺序忽略某些功能-Programignoressomefunctionsinorder

Ivecreatedavariable-sizedarray(VLA)andIwanttofillitwithusingfgetsfunctionsinafor

I've created a variable-sized array (VLA) and I want to fill it with using fgets functions in a for loop. However program passes the some functions and it strangely ignores the first fgets action.

我已经创建了一个可变大小的数组(VLA),我想在for循环中使用fgets函数来填充它。但是程序传递了一些函数,它奇怪地忽略了第一个fgets动作。

My code is;

我的代码是;

void editorMain(void)
{
    printf("Please enter the number of items:  ");
    scanf("%u", &itemQuantity);
    printf("\n\n");

    char itemNames[itemQuantity][ITEM_NAME_LEN+1];

    memset(&itemNames, 0, sizeof(itemNames));

    printf("Please enter the names of items:  \n");

    char line[ITEM_NAME_LEN+1];

    memset(&line, 0, sizeof(line));

    for (int i = 0; i = 0; --a) {
            if (line[a] == '\n') {
                line[a] = 0;
            }

            else;
        }

        snprintf(itemNames[i], ITEM_NAME_LEN+1, "%s", line);
    }
...
}

And it outputs;

它输出;

Please make a choice:  2

Please enter the number of items:  4


Please enter the names of items:  
#1:  #2:  Marlboro
#3:  Parliament
#4:  Winston

Please enter the prices of items:  
#1:  25
#2:  950
#3:  1000
#4:  800
  ................... AVAILABLE ITEMS oo

         #           Item Name          Price
         =           =========        =======

         1.                           0.25 TL
         2.           Marlboro        9.50 TL
         3.         Parliament       10.00 TL
         4.            Winston        8.00 TL

  Enter your item selection:  

What's your suggestion?

你的建议是什么?

1 个解决方案

#1


1  

The scanf("%u" call reads up to the newline character, but leaves that character in the input stream. When you call your first fgets, it just reads that newline character and gives you an empty string.

scanf(“%u”调用会读取换行符,但会将该字符留在输入流中。当您调用第一个fgets时,它只会读取换行符并为您提供一个空字符串。

There are many ways to clear the input stream, in this case you could just add a dummy call to fgets after your scanf.

有许多方法可以清除输入流,在这种情况下,您可以在scanf之后向fgets添加一个虚拟调用。


推荐阅读
author-avatar
newbigstart
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有