简体   繁体   English

C 中的结构给出错误的 output

[英]Structures in C giving erroneous output

I not a C programmer by any means but have been using PHP and Perl for many years.我无论如何都不是 C 程序员,但多年来一直在使用 PHP 和 Perl。 Am assisting someone with a C program using structures.我正在帮助某人使用结构进行 C 程序。

I have the following code -我有以下代码 -

#include<stdlib.h>
#include<stdio.h>
#define size 2
#define max 2

struct student   //initialising...
{
    char coursecode[100];
    char coursename[10];
};

main()
{
    int i,z;//for the loops

    struct student stud[10];


    printf("Enter available course name and their corresponding codes\n");

    for(z=0;z<max;z++)
    {
        printf("Course Name:  ");
        scanf("%s",&stud[z].coursename);
        printf("Course Code:  ");
        scanf("%d",&stud[z].coursecode);
        printf("-----INFORMATION RECORDED-----\n");
        printf("\n");

    }
    printf("\n");
    printf("SUBJECTS");
    printf("\n");
    printf("\n");
    for(z=0;z<max;z++) // Prints them back to the screen
        {
            printf("%s               %d\n",stud[z].coursename, stud[z].coursecode);
            printf("\n");
        }


    printf("\n");
    printf("\n");

}

I get the following output我得到以下 output

Enter available course name and their corresponding codes
Course Name:  Art
Course Code:  101
-----INFORMATION RECORDED-----

Course Name:  Biology
Course Code:  102
-----INFORMATION RECORDED-----


SUBJECTS

Art               6485496

Biology               6485704




--------------------------------
Process exited after 7.501 seconds with return value 0
Press any key to continue . . .```

Am not sure why it giving this result and would appreciate any assistance.我不确定为什么它会给出这个结果,并希望得到任何帮助。

Thanks谢谢

Steve史蒂夫

You are using a char[100] to store your course code and you are asking for an int with the %d formatter !您正在使用char[100]来存储您的课程代码,并且您正在使用%d格式化程序请求一个 int !
You can rather ask for a string with %s or %99s as suggested in comments, or if your coursecode is just an int you can maybe make the coursecode attribute an int.您可以按照评论中的建议要求一个带有%s%99s的字符串,或者如果您的课程代码只是一个 int,您可以将 coursecode 属性设置为一个 int。

Your course code is declared as an array of chars, char coursecode[100] .您的课程代码被声明为一个字符数组char coursecode[100] Therefore, in因此,在

scanf("%d",&stud[z].coursecode);

The "%d" format specifier shouldn't be used, and you also don't need the & (address of) operator.不应使用"%d"格式说明符,并且您也不需要& (地址)运算符。 Do

scanf("%99s",stud[z].coursecode);

instead, where 99 is to limit the input and "%s" is to read input into a char array.相反,其中99是限制输入,而"%s"是将输入读入 char 数组。 Do this also for scanf("%s",&stud[z].coursename);scanf("%s",&stud[z].coursename);也这样做so that it reads scanf("%9s",stud[z].coursename);使其读取scanf("%9s",stud[z].coursename); . . You should also change your printf to use the "%s format specifier instead.您还应该更改printf以使用"%s格式说明符。

At line 7 you define an array of char char coursecode[100] (which is used as String type).在第 7 行,您定义了一个 char char coursecode[100]数组(用作字符串类型)。

First of all when you read an input with: scanf("%d",&stud[z].coursecode) you are asking for int variable首先,当您使用以下命令读取输入时: scanf("%d",&stud[z].coursecode)您要求输入 int 变量

And then you print using again %d which is used for int type variables然后你再次使用用于int类型变量的%d打印

You have two possible solutions depending how you want your coursecode :您有两种可能的解决方案,具体取决于您希望coursecode的方式:

1) You can use instead %s in your scanf and printf (the same way you are doing with the coursename[] ), so your code would change: 1)您可以在 scanf 和 printf 中使用%s代替(与 coursename[] 相同的方式),因此您的代码会更改:

scanf("%s",&stud[z].coursecode)

printf("%s %s\n",stud[z].coursename, stud[z].coursecode); (maybe using \t as spacing) (也许使用\t作为间距)

This way you are treating the course code as a "String".这样,您将课程代码视为“字符串”。

OR或者

2) You declare your char coursecode[100] into int coursecode and keep your scanf and printf the same 2)您将您的char coursecode[100]声明为int coursecode coursecode 并保持您的 scanf 和 printf 相同

In your structure, you have indicated that coursecode is a character.在您的结构中,您已经指出 coursecode 是一个字符。 But then you are referencing it as %d, as if it was an integer.但是你将它引用为 %d,就好像它是一个 integer。 Change it to %s and yo good to go将其更改为 %s,然后将其更改为 go

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM