为什么我的第一个输入没有显示?

时间:2017-03-05 11:30:48

标签: c console

#include <stdio.h>

int main()
{
    char name[100], subject1[100], subject2[100], stdsignature[100], advisorsignature[100];
    char code1[7], code2[7], stdID[7];
    int credit1, credit2, total;

    printf ("Universiti of Gambang \n");
    printf ("Subjects Registration Form \n");

    printf ("Name: ");
    fflush (stdin);
    fgets ( name , 100 , stdin );

    printf ("Student ID: ");
    fflush(stdin);
    scanf ("%s",&stdID);

    printf ("Total subjects for every semester must be 2 \nSubject name 1: ");
    fflush(stdin);
    scanf ("%s",&subject1);

    printf ("Subject code 1: ");
    scanf ("%s",&code1);

    printf ("Credit 1: ");
    scanf ("%d",&credit1);

    printf ("Subject name 2: ");
    fflush(stdin);
    scanf ("%s",&subject2);

    printf ("Subject code 2: ");
    scanf ("%s",&code2);

    printf ("Credit 2:");
    scanf ("%d",&credit2);

    total= credit1 + credit2;

    printf ("Total %d \n",total);


    printf ("Advisor's signature ");
    fflush(stdin);
    scanf ("%s",&advisorsignature);

    printf ("                                         Universiti of Gambang \n");
    printf ("                                       Subjects Registration Form \n");

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

    printf ("Name: %s \n",name);
    printf ("Student ID: %s \n",stdID);
    printf ("Total subject: 2 \n");

    printf ("---------------------------------------------------------------------------------------------------------------\n");
    printf ("     Subject name                            Subject code                            Credit                    \n");
    printf ("          %s                                       %s                                 %d \n",subject1 ,code1,credit1);
    printf ("          %s                                       %s                                 %d \n",subject2 ,code2,credit2);
    printf ("---------------------------------------------------------------------------------------------------------------\n");
    printf ("     Total credits                                                                    %d \n ",total             );

    printf ("Academic Advisor: %s \n",advisorsignature);

    return 0;
}
.

我不知道我的第一次输入在哪里。请帮帮我 enter image description here

3 个答案:

答案 0 :(得分:1)

你的节目会发出santhosh提到的警告。

两件事,

1)您在为&变量intcredit1获取整数输入时未提供credit2。 对&

中的整数变量使用scanf

scanf ("%d",&credit1); .. scanf ("%d",&credit2);

2)在进行角色输入时,您无需提供&。 在C中,string是一个字符数组。当我们传递一个数组时,它实际上指向数组的第一个元素的地址。

所以没有必要使用“&amp;”在scanf中同时获取数组中的输入。

关于您的问题,您的程序会提供预期的输出。能否请您提供更多详情?

答案 1 :(得分:0)

正如BLUEPIXY所提到的,此代码段提供输出,但警告很少。 以下是更多细节,

In function ‘main’:
ex.c:30:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[7]’ [-Wformat=]
 scanf ("%s",&stdID);
 ^
ex.c:34:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=]
 scanf ("%s",&subject1);
 ^
ex.c:37:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[7]’ [-Wformat=]
 scanf ("%s",&code1);
 ^
ex.c:44:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=]
 scanf ("%s",&subject2);
 ^
ex.c:47:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[7]’ [-Wformat=]
 scanf ("%s",&code2);
 ^
ex.c:59:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=]
 scanf ("%s",&advisorsignature);

答案 2 :(得分:0)

#include <stdio.h>

int main()
{
    char name[100], subject1[100], subject2[100], stdsignature[100], advisorsignature[100];
    char code1[50], code2[50], stdID[50];
    int credit1, credit2, total;

    printf ("Universiti of Gambang \n");
    printf ("Subjects Registration Form \n");

    printf ("Name: ");
    gets ( name );
    fflush (stdin);

    printf ("Student ID: ");
    scanf (" %s",&stdID);
    fflush(stdin);

    printf ("Total subjects for every semester must be 2 \nSubject name 1: ");
    fflush(stdin);
    scanf ("%s",&subject1);

    printf ("Subject code 1: ");
    fflush(stdin);
    scanf ("%s",&code1);

    printf ("Credit 1: ");
    scanf ("%d",&credit1);

    printf ("Subject name 2: ");
    fflush(stdin);
    scanf ("%s",&subject2);

    printf ("Subject code 2: ");
    fflush(stdin);
    scanf ("%s",&code2);

    printf ("Credit 2:");
    scanf ("%d",&credit2);

    total= credit1 + credit2;

    printf ("Total %d \n",total);

    printf ("Student's signature ");
    fflush(stdin);
    scanf ("%s",&stdsignature);

    printf ("Advisor's signature ");
    fflush(stdin);
    scanf ("%s",&advisorsignature);

    printf ("                                         Universiti of Gambang \n");
    printf ("                                       Subjects Registration Form \n");

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

    printf ("Name : %s \n",name);
    printf ("Student ID: %s \n",stdID);
    printf ("Total subject: 2 \n");

    printf ("---------------------------------------------------------------------------------------------------------------\n");
    printf ("     Subject name                            Subject code                            Credit                    \n");
    printf ("          %s                                       %s                                 %d \n",subject1 ,code1,credit1);
    printf ("          %s                                       %s                                 %d \n",subject2 ,code2,credit2);
    printf ("---------------------------------------------------------------------------------------------------------------\n");
    printf ("     Total credits                                                                    %d \n ",total             );

    printf ("Academic Advisor: %s \n",advisorsignature);

    return 0;
}

所以,我设法通过操纵一些代码来解决我的缺失输入。谢谢你的回应。 enter image description here