Tag: scanf

使用C中的scanf在EOF退出while循环

我正在为我的入门C课程写一些非常小的课程。 其中一个要求我读取双倍值,每行一个数字,然后在EOF后打印出基本统计数据。 这是我的代码段,它给了我一些问题: double sample[1000000]; int result; double number; int i = 0; int count = 0; double sum = 0; double harmosum = 0; result = scanf(” %lf \n”, &number); double min = number; double max = number; while(result != EOF){ sample[i] = number; if(number max){ max = number; } sum += number; if(number […]

scanf:未链接的浮点格式

我得到错误scanf:浮点格式没有链接,同时读取以下结构的’info’的值。 struct node { float info; struct node *next; }*start; 在主() void main() { struct node *temp; temp = (struct node*)malloc(sizeof(struct node)); printf(“enter data = “); scanf(“%f”,&temp->info); } 它没有读取该scanf的任何值并从程序中退出。 怎么解决这个问题?

C 中scanf函数的格式说明符中%c规范之前的空格

如果我在以下程序中的scanf函数的格式字符串中不包含%d和%c规范之间的空格,并在运行时将输入设置为“4 h”,则输出为“Integer = 4且Character = 。 在这种情况下,变量“c”如何获取输入,如果我在%d和%c规范之间包含空格,它会有什么不同? #include main() { char c; int i; printf(“Enter an Integer and a character : \n”); scanf(“%d %c”,&i,&c); printf(“Integer = %d and Character = %c\n”,i,c); getch(); }

scanf函数如何在C中工作?

为什么在scanf函数中需要&符号(&)。 以下C代码中的输出或错误类型(编译或运行时)是什么? #include void main() { int a; printf(“enter integer:”); scanf(“%d”, a); }

你如何阅读scanf直到C中的EOF?

我有这个,但一旦达到假定的EOF,它只是重复循环和scanf。 int main(void) { char words[16]; while(scanf(“%15s”, words) == 1) printf(“%s\n”, words); return 0; }

扫描程序在NetBeans中无法正常运行的程序

我今天安装了NetBeans 7.0.1当我尝试用“scanf”执行C程序时,它会给出奇怪的错误 这就是我写的: 它一直运行,直到我在输出控制台中输入内容。 输入后显示printf语句并显示“RUN FAILED” 任何人都可以告诉我该怎么做才能做到这一点?

为什么scanf在无效输入上陷入无限循环?

在第5行中,我读取一个整数,如果它读取整数,则isint为1,如果不是整数, isint 0。 如果isint为0,我有一个循环要求用户给出一个整数,我会读取,直到用户给出一个整数。 我尝试这个代码给出一个字符而不是一个整数,但我有一个无限循环。 该程序只是不等待提供新的输入。 我的代码出了什么问题? #include int main(void) { int arg1; //int arg2; int attacknum = 1; int isint = 1; //printf(“Insert argument attacks and press 0 when you have done this.\n”); printf(“Attack %d\n”, attacknum); attacknum++; printf(“Give attacking argument:”); isint = scanf(“%d”, &arg1); //line 5 while(isint == 0){ printf(“You did not enter a […]

C getchar vs scanf

在我正在研究的函数中发现的一段代码让我感到困惑: char GetCommand( void ) { char command; do { printf( “Enter command (q=quit, n=new, l=list): ” ); scanf( “%c”, &command ); Flush(); } while ( (command != ‘q’) && (command != ‘n’) && (command != ‘l’) ); printf( “\n———-\n” ); return( command ); } void Flush( void ) { while ( getchar() != ‘\n’ […]

如何在scanf中只接受一定的精度(如此多的小数位)?

在scanf()函数中,我想只取十进制值。我们能实现它吗? 例如,为了显示最多两个小数位,我们使用printf(“%.2f”,A)我们如何为scanf()函数执行此操作。

C:函数在代码中跳过用户输入

我遇到了这个function(战舰游戏的一部分)的问题,它将完美地运行它,但在后续的执行中,它会跳过: scanf(“%c”,&rChar); 出于某种原因,rChar在没有上述代码的用户输入的情况下变成另一个值。 我试过在整个函数中输入显示rChar值的printf语句。 函数Conv_rChar_Int()将用户输入的Char转换为整数值。 但是因为rChar没有作为指针传递,所以rChar的值rChar保持不变,直到用户在下一次迭代中替换它。 (再次使用printfvalidation)。 奇怪的是,它在这些代码行之间发生了变化。 并且从不提示用户输入rChar 。 printf(“please enter the row you want to place your %d ship in\n”,length); scanf(“%c”,&rChar); 请记住,它只发生在第一次之后。 即使我在每次迭代后重新初始化变量rChar , r , c和dir ,这个问题仍然会发生。 我99%肯定问题是在这个函数内而不是在它内部调用的任何函数中(因为rChar在每一行之后保持不变,除了上面的两行之外)。 提前感谢您的帮助。 如果您对代码有任何疑问,我会尝试解释一下。 int Gen_Ship_Place(int length, int flag3, int PlayGrid[10][10]){ int ShipPlaceFlag = 0; //coordinates and direction int r; char rChar; int c; int dir; […]