Tag: scanf

scanf没有按预期工作

我尝试在ubuntu 15.10中执行以下简单代码但是代码的行为比预期的要奇怪 #include int main(){ int n,i=0; char val; char a[20]; printf(“\nEnter the value : “); scanf(“%s”,a); printf(“\nEnter the value to be searched : “); scanf(“%c”,&val); int count=0; for(i=0;i<20;i++){ if(a[i]==val){ printf("\n%c found at location %d",val,i); count++; } } printf("\nTotal occurance of %c is %d",val,count); return 0; } output: ————————– Enter the value : 12345678 Enter […]

scanf()行为不端

我有一个非常短的片段,读取整数: #include int main() { while (1) { int i = 0; int r = scanf(“%d”, &i); if (r == EOF) { printf(“Error using scanf()\n”); } else if (r == 0) { printf(“No number found\n”); } else { printf(“The number you typed in was %d\n”, i); } } } 但问题是,如果我输入任何字母,它只是循环并打印出“找不到号码”而不是等待下一个输入。 我究竟做错了什么?

如何在支持空字段的同时使用sscanf解析逗号分隔字符串中的字段?

我有一个逗号分隔的字符串,可能包含空字段。 例如: 1,2,,4 使用基本的 sscanf(string,”%[^,],%[^,],%[^,],%[^,],%[^,]”, &val1, &val2, &val3, &val4); 我得到空字段之前的所有值,以及从空字段开始的意外结果。 当我从sscanf()中删除空字段的表达式时, sscanf(string,”%[^,],%[^,],,%[^,],%[^,]”, &val1, &val2, &val3, &val4); 一切都很好。 由于我不知道何时会得到一个空字段,有没有办法重写表达式以优雅地处理空字段?

Fgets跳过输入

我试过环顾四周,似乎无法找到错误所在。 我知道它必须与我使用fgets的方式有关但我无法弄清楚它的生命是什么。 我已经读过混合fgets和scanf会产生错误,所以我甚至将我的第二个scanf更改为fgets,它仍会跳过其余的输入,只打印第一个。 int addstudents = 1; char name[20]; char morestudents[4]; for (students = 0; students<addstudents; students++) { printf("Please input student name\n"); fgets(name, 20, stdin); printf("%s\n", name); printf("Do you have more students to input?\n"); scanf("%s", morestudents); if (strcmp(morestudents, "yes")==0) { addstudents++; } } 我的投入是乔,是的,比尔,是的,约翰,不。 如果我使用scanf代替第一个fgets,所有都按计划进行,但我希望能够使用包含空格的全名。 我哪里错了?

使用C 中的scanf()函数处理空格

我有以下问题。 我想以下列格式扫描scanf中的三个点: {[4;-1],[7;1.5],[4;4]} 。 我的代码如下所示: scanf(“{[%lf;%lf],[%lf;%lf],[%lf;%lf]}”, &a1, &a2, &b1, &b2, &c1, &c2) 现在,我实际上正在测试这是否正确转换为进一步使用它。 但我想允许用户输入这样的东西 { [ 4 ; -1 ] , [ 7 ; 1.5 ] , [ 4 ; 4 ] } 甚至 { [ 4 ; -1 ] , [ 7 ; 1.5 ] , [ 4 ; 4 ] } 所以scanf()仍然可以给我6个正确的转换。 我该怎么办?

C:scanf for char无法正常工作

我最近在我的电脑上运行了ac程序。 它有一个for循环,其中扫描一些char。 for循环运行3次。 在每次运行期间,它打印运行计数,然后扫描char d的值。 该计划如下 #include int main(){ int f; char d; for(f=0;f<3;f++){ printf("Choice %d\n", f); scanf("%c", &d); } return 0; } 现在麻烦的是,当我运行程序时,当f为1时,for跳过scanf部分。现在,如果我更改了代码如下 #include int main(){ int f; int d; for(f=0;f<3;f++){ printf("Choice %d\n", f); scanf("%d", &d); } return 0; } 现在程序运行正常。 并且forf循环的每次迭代都执行scanf。 这里似乎有什么问题? 我的意思是当d是int类型时它工作正常,但是当d是char类型时它不能正常工作。

fgets()在fscanf()之后无法正常工作

我使用fscanf读取日期然后fgets读取笔记。 但是在第一次迭代之后,fscanf返回值-1。 我使用GDB逐步调试程序。 它在第一次使用fgets之前工作正常。 当我尝试在第一次迭代时打印出由fgets读取的行时,它给了我: (gdb) print line $6 = “\rtest\r18/04/2010\rtest2\r03/05/2010\rtest3\r05/08/2009\rtest4\r\n\000\000\000\000q\352\261\a\370\366\377\267.N=\366\000\000\000\000\003\000\000\000\370xC\000\000\000\000\000\000\000\000\000\001\000\000\000\227\b\000\000\070\367\377\267H\364\377\267\362\202\004\bdoD\000\354\201\004\b\001\000\000\000\304oC\000p\363\377\277\260zC\000D\363\377\277\n!B\000\064\363\377\277\354\201\004\b(\363\377\277TzC\000\000\000\000\000\070\367\377\267\001\000\000\000\000\000\000\000\001\000\000\000\370xC\000\001\000\000\000\000\000\312\000\000\000\000\000\377\260\360\000\001\000\000\000\277\000\000\000\364\317\000\000\344\261\\\000\000\000\000\000p\363\377\277|\233\004\b\350\362\377\277 \204\004\b\005\000\000\000|\233\004\b\030\363\377\277” 它看起来像fgets读取剩余的条目,然后将它们全部存储在一个字符串中。 我不确定为什么会这样做。 这是主要代码: int main(int argc, char* argv[]) { FILE* file; int numEntries, i = 0; int index = atoi(argv[1]); char line[SIZE]; JournalEntry *entry; /*argument provided is the entry user wants to be displayed*/ if (argc > 2) { perror(“Error: Too many arguments […]

scanf一直被跳过

在函数“leerdatos”中格式化“direccion”的scanf之后,它跳过第二个’for’循环中的“nro_corredor”。 我已经阅读了相关问题,但仍然没有得到答案。 我该怎么做才能解决它? #include typedef struct { int minutos; int segundos; } s_tiempo; typedef struct { int nro_corredor; // guarda nro de corredor s_tiempo tiempo; // guarda el tiempo que tardo el corredor } carrera; typedef struct { int nro_corredor; // guarda nro de corredor char apellido[20]; char nombres[20]; char direccion[30]; } datos; datos leerdatos(); […]

scanf的问题

#include int main() { char C, B; int x; printf(“What comes after G\n”); scanf(“%c”, &C); printf(“What comes after O\n”); scanf(“%c”, &B); printf(“What is your age?\n”); scanf(“%d”, &x); printf(“You said %c comes after G, %c after T and you’re %d years old? Right?”, C, B, x); return 0; } 问题在于,无论何时运行代码,它都会跳过第二个问题“O之后发生了什么”,然后问“你的年龄是多少?” 我可以避免程序跳过第二个问题的唯一方法是在代码中添加一个空格 printf(“What comes after O\n”); scanf(” […]

scanf没有收集数据

我在介绍C类,我遇到数据输入问题。 我正在练习子程序,我的代码看起来是正确的,但程序中的一个问题由于某种原因被绕过,我无法弄清楚。 1)程序读入ISBN书号作为10个单独的字符(检查) 2)程序读入书的价格(支票) 3)程序读入class级中的学生人数(检查) 4)该程序要求天气这本书是较新版本或旧版本(不工作!!) 5)程序要求天气书是必需的或建议的(检查) 我正在使用char来解决有关新旧问题的问题,以及我们为了利用迄今为止所学到的知识而提出要求或建议的问题。 我无法理解为什么其中一个问题被绕过了。 这是我的输出: Enter ISBN: 1231231231 Enter list price per copy: 54.99 Enter expected class enrollment: 45 Enter N for new edition or O for Older edition: Enter R for Required or S for Suggested: R ISBN: 1-23-123123-1 List Price: 54.99 Expected enrollment: 45 Edition, New or Old: […]