首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

构造不能存储数据

2013-09-12 
结构不能存储数据#include stdio.h#include string.h#define MAXBKS 100#define MAXAUTL 40#define MA

结构不能存储数据


#include <stdio.h>
#include <string.h>

#define MAXBKS 100
#define MAXAUTL 40
#define MAXTITL 40

struct book {
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS];
    int count=0, index;

    printf("Please enter the book title, press [enter] at the start"
        " of a line to stop.\n");
    while(fgets(library[count].title, MAXTITL-1, stdin)!= NULL
            && library[count].title[0] != '\n' && count<MAXBKS){
        library[count].title[strlen(library[count].title)-1] = '\0';
        printf("Now, enter the author: ");
        fgets(library[count].author, MAXAUTL-1, stdin);
        library[count].author[strlen(library[count].author)-1] = '\0';
        printf("Now, enter the value: ");
        scanf("%f", &library[count++].value);
        if(getchar() == '\n') 
            ; 
        printf("Now, enter the title: ");
    }
    if(count > 0){
        printf("Here is the list of your books: \n");
        for(index=0; index<count; index++);
            printf("%s by %s: $%.2f.\n", library[index].title,
                library[index].author, library[index].value);
    }
    else
        printf("No books? So bad...\nBye Bye!\n");

    return 0;
}

引用
Please enter the book title, press [enter] at the start of a line to stop.


The C programming
Now, enter the author: K&R
Now, enter the value: 68
Now, enter the title: 
Here is the list of your books: 

 by : $0.00.
Please enter the book title, press [enter] at the start of a line to stop.

No books? So bad...
Bye Bye!


这是我之前写的同样功能的代码,是我预想的结果。可是找不到两段代码的差异

int main(void)
{
    struct book library[MAXBKS];
    int count=0, index;

    printf("Please enter the book title. Please ENTER at the start of"
        " line to quit.\n");
    while(fgets(library[count].title, MAXTITL-1, stdin) != NULL
            && library[count].title[0] != '\n' && count<MAXBKS){
        library[count].title[strlen(library[count].title)-1] = '\0';
        printf("Now enter the author: ");
        fgets(library[count].author, MAXAUTL-1, stdin);
        library[count].author[strlen(library[count].author)-1] = '\0';
        printf("Now enter the value: ");
        scanf("%f", &library[count++].value);
        if(getchar() == '\n')
            ;
        printf("Enter the next title: ");
    }
    if(count > 0){
        printf("Here is the list of your books: \n");
        for(index=0; index<count; index++)
            printf("%s by %s: $%.2f.\n", library[index].title,
                library[index].author, library[index].value);
    }
    else
        printf("No books? Too bad.\n");

    return 0;


}


引用
Please enter the book title. Please ENTER at the start of line to quit.
The C programming
Now enter the author: K&R
Now enter the value: 68
Enter the next title: ABC
Now enter the author: Edf
Now enter the value: 12
Enter the next title: 
Here is the list of your books: 
The C programming by K&R: $68.00.
ABC by Edf: $12.00.
Please enter the book title. Please ENTER at the start of line to quit.

No books? Too bad.

请各位大侠指教了,谢谢 结构
[解决办法]
数据有存进去,原因在于
for(index=0; index<count; index++);
for循环后面多了一个分号,应该是笔误吧。

引用:

#include <stdio.h>
#include <string.h>

#define MAXBKS 100
#define MAXAUTL 40
#define MAXTITL 40

struct book {
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS];
    int count=0, index;

    printf("Please enter the book title, press [enter] at the start"
        " of a line to stop.\n");
    while(fgets(library[count].title, MAXTITL-1, stdin)!= NULL
            && library[count].title[0] != '\n' && count<MAXBKS){
        library[count].title[strlen(library[count].title)-1] = '\0';
        printf("Now, enter the author: ");


        fgets(library[count].author, MAXAUTL-1, stdin);
        library[count].author[strlen(library[count].author)-1] = '\0';
        printf("Now, enter the value: ");
        scanf("%f", &library[count++].value);
        if(getchar() == '\n') 
            ; 
        printf("Now, enter the title: ");
    }
    if(count > 0){
        printf("Here is the list of your books: \n");
        for(index=0; index<count; index++);
            printf("%s by %s: $%.2f.\n", library[index].title,
                library[index].author, library[index].value);
    }
    else
        printf("No books? So bad...\nBye Bye!\n");

    return 0;
}


引用
Please enter the book title, press [enter] at the start of a line to stop.
The C programming
Now, enter the author: K&R
Now, enter the value: 68
Now, enter the title: 
Here is the list of your books: 

 by : $0.00.
Please enter the book title, press [enter] at the start of a line to stop.

No books? So bad...
Bye Bye!

这是我之前写的同样功能的代码,是我预想的结果。可是找不到两段代码的差异

int main(void)
{
    struct book library[MAXBKS];
    int count=0, index;

    printf("Please enter the book title. Please ENTER at the start of"
        " line to quit.\n");
    while(fgets(library[count].title, MAXTITL-1, stdin) != NULL
            && library[count].title[0] != '\n' && count<MAXBKS){


        library[count].title[strlen(library[count].title)-1] = '\0';
        printf("Now enter the author: ");
        fgets(library[count].author, MAXAUTL-1, stdin);
        library[count].author[strlen(library[count].author)-1] = '\0';
        printf("Now enter the value: ");
        scanf("%f", &library[count++].value);
        if(getchar() == '\n')
            ;
        printf("Enter the next title: ");
    }
    if(count > 0){
        printf("Here is the list of your books: \n");
        for(index=0; index<count; index++)
            printf("%s by %s: $%.2f.\n", library[index].title,
                library[index].author, library[index].value);
    }
    else
        printf("No books? Too bad.\n");

    return 0;
}


Quote:

Please enter the book title. Please ENTER at the start of line to quit.
The C programming
Now enter the author: K&R
Now enter the value: 68
Enter the next title: ABC
Now enter the author: Edf
Now enter the value: 12
Enter the next title: 
Here is the list of your books: 
The C programming by K&R: $68.00.
ABC by Edf: $12.00.
Please enter the book title. Please ENTER at the start of line to quit.

No books? Too bad.

请各位大侠指教了,谢谢

[解决办法]
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...


 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。

热点排行