循环在输入数组时未正常工作。

huangapple go评论61阅读模式
英文:

Loop is not working properly while taking input in array

问题

scanf的循环对于索引(索引为2)不起作用。在printf循环中,两个索引的输出是垃圾。我不知道这里发生了什么。

这个程序来自书籍"Let us C",结构章的第一页。

我的真实输出(我得到的)是:

输入三本书的名称、价格和页数:

a 100 200 // 由我输入
b 100 200

// 但无法提供第三个索引的值

你输入的是:

b 100.000000 200

84227675241280636545541341184.000000 0

0.000000 70

这是一个简单的程序,根据我的看法,我应该得到如下输出。

我的输出是我期望的:

输入三本书的名称、价格和页数:

a 100 200
b 100 200

c 100 200

你输入的是:

a 100.000000 200
b 100.000000 200

c 100.000000 200
英文:

scanf's loop is not working for index (which is 2). In the printf loop, I am getting garbage output for two indexes. I don't know what is happening here.

#include <stdio.h>
int main() {
    int i;
    char name[3];
    float price[3];
    int pages[3];

    printf("Enter names, prices and pages of 3 books:\n ");
    for (i = 0; i <=2; i++) {
        scanf("%c%f%d", &name, &price, &pages);
    }
    printf("what you entered:\n");

    for (i = 0; i <=2; i++) {
    printf("%c %f %d\n", name[i], price[i], pages[i]);
    }
    return 0;
}

This program is from the book "Let us C", first page of the structure chapter.

My real output (what I am getting) is:

Enter names, prices and pages of 3 books:

a 100 200 // given by me  
b 100 200

// but not able to give third index values

what you entered:

b 100.000000 200

84227675241280636545541341184\.000000 0

0\.000000 70

This is a simple program and according to me, I should get like the below output.

My output is what I am expecting:

 Enter names, prices and pages of 3 books:

a 100 200  
b 100 200

c 100 200  

what you entered:

a 100.000000 200  
b 100.000000 200

c 100.000000 200

答案1

得分: 2

你应该在每次迭代中向 scanf() 提供数组的下一个元素的地址,而不是每次都传递给第一个元素的地址。
尝试编写:

scanf("%c%f%d", &name[i], &price[i], &pages[i]);
英文:

You should to put to scanf() an address for the next element of array at each iteration, but you pass address to first element every time instead of that.
Try to write:

scanf("%c%f%d", &name[i], &price[i], &pages[i]);

答案2

得分: 2

数组 `name` 被声明为

    char name[3];

表达式 `&name` 的类型为 `char ( * )[3]`,而转换说明符 `c` 需要相应类型的参数 `char *`。

同时,使用转换说明符 `c` 输入字符时,需要跳过空白字符,例如输入缓冲区中按 Enter 键后存储的换行字符 `'\n'`。

因此,你需要这样写

    printf("输入3本书的名称、价格和页数:\n ");
    for (i = 0; i <=2; i++) {
        scanf(" %c%f%d", name + i, price + i, pages + i);
    }

在这个调用中,表达式 `name + i` 等同于 `&name[i]`。其他两个数组也是相同的。

注意格式字符串中的前导空格。它允许跳过空白字符。

此外,使用类似 `3`  `2` 的魔术数字是一种不好的编程风格。相反,你可以引入一个命名常量,例如

    enum { N = 3 };
    char name[N];
    float price[N];
    int pages[N];

    printf("输入%d本书的名称、价格和页数:\n", N);
    for (i = 0; i < N; i++) {
        scanf(" %c%f%d", name + i, price + i, pages + i);
    }
    //...
英文:

The array name is declared like

char name[3];

The expression &name has the type char ( * )[3] while the conversion specifier c expects a corresponding argument of the type char *.

Also entering a character using the conversion specifier c you need to skip white space characters as for example the new line character '\n' stored in the input buffer after pressing the key Enter.

So you need to write

printf("Enter names, prices and pages of 3 books:\n ");
for (i = 0; i <=2; i++) {
    scanf( " %c%f%d", name + i, price + i, pages + i );
}

In this call the expression name + i is equivalent to &name[i]. The same is valid for other two arrays.

Pay attention to the leading space in the format string. It allows to skip white space characters.

Also it is a bad style of programming to use magic numbers like 3 and 2. Instead you could introduce a named constant like for example

enum { N = 3 };
char name[N];
float price[N];
int pages[N];

printf("Enter names, prices and pages of %d books:\n", N);
for ( i = 0; i < N; i++) {
    scanf( " %c%f%d", name + i, price + i, pages + i );
}
//...

huangapple
  • 本文由 发表于 2023年3月9日 23:37:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定