在C中显示输出时出现问题 [英] Problem on showing output in C

查看:73
本文介绍了在C中显示输出时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
struct subjectRecord
{
    char subjectCode;
    char subjectName;
    char lecturer;
    int creditHour;
}subject[10];
int main(void)
{
    int a;
    for(a = 1; a <= 10; a++)
    {
        printf("\nEnter Subject Code: ");
        scanf("%s", &subject[a].subjectCode);
        printf("Enter Subject Name: ");
        scanf("%s", &subject[a].subjectName);
        printf("Enter Lecturer Name: ");
        scanf("%s", &subject[a].lecturer);
        printf("Enter Credit Hour: ");
        scanf("%d", &subject[a].creditHour);
    }
    for(a = 1; a <=10; a++){
    printf("Subject Code :%s\n",subject[a].subjectCode);
        printf("Subject Name: %d %d%d\n", subject[a].subjectName);
    printf("Lecturer Name:%s\n",subject[a].lecturer);
    printf("Credit Hour:%s\n",subject[a].creditHour);
   }
}



请有人解释我的代码中的错误在哪里,因为当我尝试运行该程序时,错误消息显示如下:
程序出现问题,需要关闭."

[在下面的行中进行了编辑,因此很有意义:
"我可能知道我的错误在哪里,因为当我接受所有输入并显示输出时,它说需要关闭就出现了问题=="]



Please could someone explain where the error in my code is, as when I try to run the program an error message is displayed as follows:
''Program has got a problem and needs to close.''

[Edited following line to above so it makes sense:
''may i know where is my error because when i accept all the input and show the output, it say need to close got problem =='']

推荐答案

您似乎遇到了四个重大问题,两个与内存问题有关,两个与格式有关.第一个是当您读入subjectCode,subjectName和讲师字符串时,您尝试将它们分配给char.这将使您发生内存泄漏,因为它将溢出分配给一个字符的内存.您应该在结构中使用的代码如下:

You appear to have four significant problems two related to memory issues and two with format. The first is that when you read in the subjectCode, subjectName and lecturer strings you try to assign them to chars. This will give you a memory leak as it will overun the memory assigned to one char. The code you should use in your struct should be as follows:

struct subjectRecord
{
    char subjectCode[100]; //Make sure an array of 100 chars is enough space to fit the full string your expecting into. Likewise for the next two variables.
    char subjectName[100];
    char lecturer[100];
    int creditHour;
}subject[10];



下一个问题是oyu尝试让一个for循环从1开始一直到10,但是使用相同的变量(a)索引到subject数组中.这是一个问题,因为数组不是从索引1开始,而是从索引0开始,它们是从零开始的".这意味着您认为subject数组中的元素(或subjectRecord)1实际上是元素0,元素10是元素9、8是7等,因此,for循环应类似于以下内容:



The next problem is that oyu try to have a for loop starting at one and going up to ten but using the same variable (a) to index into the subjectarray. This is a problem because arrays do not start from index 1 but from index 0, they are ''zero-based''. This means that what you think of as element (or subjectRecord) 1 in your subject array is actually element 0 and element 10 is element 9, 8 is 7 etc etc. Your for loops should therefore look like the following:

for(a = 0; a < 10; a++)
{
    printf("\nEnter Subject Code: ");
    scanf("%s", &subject[a].subjectCode);
    printf("Enter Subject Name: ");
    scanf("%s", &subject[a].subjectName);
    printf("Enter Lecturer Name: ");
    scanf("%s", &subject[a].lecturer);
    printf("Enter Credit Hour: ");
    scanf("%d", &subject[a].creditHour);

}
//and then:
for(a = 0; a <10; a++)
{
    printf("Subject Code :%s\n",subject[a].subjectCode);
    //printf("Subject Name: %d %d%d\n", subject[a].subjectName); Your other problem here was using %d for a string, it should be the below:
    printf("Subject Name: %s\n", subject[a].subjectName);
    printf("Lecturer Name:%s\n",subject[a].lecturer);
    //printf("Credit Hour:%s\n",subject[a].creditHour); And again %s instead of %d:
    printf("Credit Hour:%d\n",subject[a].creditHour); 
}



您的最后两个问题可能会导致致命的格式化问题.请参阅上面的代码以获取解释.

希望这会有所帮助,

埃德:)



Your last two problems would cause probably fatal formatting issues. See the code above for explaination.

Hope this helps,

Ed :)


这篇关于在C中显示输出时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆