为什么最后没有输出?

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

Why is there no output at the end?

问题

以下是程序的一部分:

 17 #include <stdio.h>
 18 
 19 struct Student
 20 {
 21   int number;
 22   char name[15];
 23 } student1, student2, student3, student4;
 24 
 25 void input_name();
 26 
 27 void input_name()
 28 {
 29   int h, i, j, k;
 30   printf("********************************\n");
 31   printf("*   输入学生信息  *\n");
 32   printf("********************************\n");
 33   printf("请输入第一个学生的学号: ");
 34   scanf("%d", &student1.number);
 35   printf("请输入第一个学生的姓名: ");
 36   while(1)
 37   {
 38     for(h = 0; h < 15; h++)
 39     {
 40       scanf("%c", &student1.name[h]);
 41       if(student1.name[h] == 13)
 42       {
 43         student1.name[h] = '
 17 #include <stdio.h>
 18 
 19 struct Student
 20 {
 21   int number;
 22   char name[15];
 23 } student1, student2, student3, student4;
 24 
 25 void input_name();
 26 
 27 void input_name()
 28 {
 29   int h, i, j, k;
 30   printf("********************************\n");
 31   printf("*   输入学生信息  *\n");
 32   printf("********************************\n");
 33   printf("请输入第一个学生的学号: ");
 34   scanf("%d", &student1.number);
 35   printf("请输入第一个学生的姓名: ");
 36   while(1)
 37   {
 38     for(h = 0; h < 15; h++)
 39     {
 40       scanf("%c", &student1.name[h]);
 41       if(student1.name[h] == 13)
 42       {
 43         student1.name[h] = '\0';
 44         break;
 45       }
 46     }
 47   }
 48   printf("请输入第二个学生的学号: \n");
 49   scanf("%d", &student2.number);
 50   printf("请输入第二个学生的姓名: \n");
 51   while(1)
 52   {
 53     for(i = 0; i < 15; i++)
 54     {
 55       scanf("%c", &student2.name[i]);
 56       if(student2.name[i] == 13)
 57       {
 58         student2.name[i] = '\0';
 59         break;
 60       }
 61     }
 62   }
}
'
;
44 break; 45 } 46 } 47 } 48 printf("请输入第二个学生的学号: \n"); 49 scanf("%d", &student2.number); 50 printf("请输入第二个学生的姓名: \n"); 51 while(1) 52 { 53 for(i = 0; i < 15; i++) 54 { 55 scanf("%c", &student2.name[i]); 56 if(student2.name[i] == 13) 57 { 58 student2.name[i] = '
 17 #include <stdio.h>
 18 
 19 struct Student
 20 {
 21   int number;
 22   char name[15];
 23 } student1, student2, student3, student4;
 24 
 25 void input_name();
 26 
 27 void input_name()
 28 {
 29   int h, i, j, k;
 30   printf("********************************\n");
 31   printf("*   输入学生信息  *\n");
 32   printf("********************************\n");
 33   printf("请输入第一个学生的学号: ");
 34   scanf("%d", &student1.number);
 35   printf("请输入第一个学生的姓名: ");
 36   while(1)
 37   {
 38     for(h = 0; h < 15; h++)
 39     {
 40       scanf("%c", &student1.name[h]);
 41       if(student1.name[h] == 13)
 42       {
 43         student1.name[h] = '\0';
 44         break;
 45       }
 46     }
 47   }
 48   printf("请输入第二个学生的学号: \n");
 49   scanf("%d", &student2.number);
 50   printf("请输入第二个学生的姓名: \n");
 51   while(1)
 52   {
 53     for(i = 0; i < 15; i++)
 54     {
 55       scanf("%c", &student2.name[i]);
 56       if(student2.name[i] == 13)
 57       {
 58         student2.name[i] = '\0';
 59         break;
 60       }
 61     }
 62   }
}
'
;
59 break; 60 } 61 } 62 } }

第33到47行的代码可以执行,但第48到62行的代码不能执行。当我按回车键时,会不断换行。所以,我想问为什么后面的部分无法执行,中间的哪个环节有问题?

如果你的问题是关于程序的运行问题,那么需要查看更多的代码和上下文才能确定问题的原因。根据提供的代码,第33到47行是接收第一个学生的学号和姓名,第48到62行是接收第二个学生的学号和姓名。如果第48到62行的代码无法执行,可能是因为程序陷入了无限循环,导致无法继续执行后面的部分。你需要检查循环条件和输入数据的方式,以确保程序能够正常执行。

英文:

Here is a part of the program:

 17 #include&lt;stdio.h&gt;
 18 
 19 struct Student
 20 {
 21   int number;
 22   char name[15];
 23 }student1,student2,student3,student4;
 24 
 25 void input_name();
 26 
 27 void input_name()
 28 {
 29   int h,i,j,k;
 30   printf(&quot;********************************\n&quot;);
 31   printf(&quot;*   Enter student information  *\n&quot;);
 32   printf(&quot;********************************\n&quot;);
 33   printf(&quot;Please enter the student number of the first student: &quot;);
 34   scanf(&quot;%d&quot;,&amp;student1.number);
 35   printf(&quot;Please enter the first student&#39;s name: &quot;);
 36   while(1)
 37   {
 38     for(h=0;h&lt;15;h++)
 39     {
 40       scanf(&quot;%c&quot;,&amp;student1.name[h]);
 41       if(student1.name[h]==13)
 42       {
 43         student1.name[h]=&#39;
 17 #include&lt;stdio.h&gt;
18 
19 struct Student
20 {
21   int number;
22   char name[15];
23 }student1,student2,student3,student4;
24 
25 void input_name();
26 
27 void input_name()
28 {
29   int h,i,j,k;
30   printf(&quot;********************************\n&quot;);
31   printf(&quot;*   Enter student information  *\n&quot;);
32   printf(&quot;********************************\n&quot;);
33   printf(&quot;Please enter the student number of the first student: &quot;);
34   scanf(&quot;%d&quot;,&amp;student1.number);
35   printf(&quot;Please enter the first student&#39;s name: &quot;);
36   while(1)
37   {
38     for(h=0;h&lt;15;h++)
39     {
40       scanf(&quot;%c&quot;,&amp;student1.name[h]);
41       if(student1.name[h]==13)
42       {
43         student1.name[h]=&#39;\0&#39;;
44         break;
45       }
46     }
47   }
48   printf(&quot;Please enter the student number of the second student: \n&quot;);
49   scanf(&quot;%d&quot;,&amp;student2.number);
50   printf(&quot;Please enter the second student&#39;s name: \n&quot;);
51   while(1)
52   {
53     for(i=0;i&lt;15;i++)
54     {
55       scanf(&quot;%c&quot;,&amp;student2.name[i]);
56       if(student2.name[i]==13)
57       {
58         student2.name[i]=&#39;\0&#39;;
59         break;
60       }
61      }
62   }
&#39;; 44 break; 45 } 46 } 47 } 48 printf(&quot;Please enter the student number of the second student: \n&quot;); 49 scanf(&quot;%d&quot;,&amp;student2.number); 50 printf(&quot;Please enter the second student&#39;s name: \n&quot;); 51 while(1) 52 { 53 for(i=0;i&lt;15;i++) 54 { 55 scanf(&quot;%c&quot;,&amp;student2.name[i]); 56 if(student2.name[i]==13) 57 { 58 student2.name[i]=&#39;
 17 #include&lt;stdio.h&gt;
18 
19 struct Student
20 {
21   int number;
22   char name[15];
23 }student1,student2,student3,student4;
24 
25 void input_name();
26 
27 void input_name()
28 {
29   int h,i,j,k;
30   printf(&quot;********************************\n&quot;);
31   printf(&quot;*   Enter student information  *\n&quot;);
32   printf(&quot;********************************\n&quot;);
33   printf(&quot;Please enter the student number of the first student: &quot;);
34   scanf(&quot;%d&quot;,&amp;student1.number);
35   printf(&quot;Please enter the first student&#39;s name: &quot;);
36   while(1)
37   {
38     for(h=0;h&lt;15;h++)
39     {
40       scanf(&quot;%c&quot;,&amp;student1.name[h]);
41       if(student1.name[h]==13)
42       {
43         student1.name[h]=&#39;\0&#39;;
44         break;
45       }
46     }
47   }
48   printf(&quot;Please enter the student number of the second student: \n&quot;);
49   scanf(&quot;%d&quot;,&amp;student2.number);
50   printf(&quot;Please enter the second student&#39;s name: \n&quot;);
51   while(1)
52   {
53     for(i=0;i&lt;15;i++)
54     {
55       scanf(&quot;%c&quot;,&amp;student2.name[i]);
56       if(student2.name[i]==13)
57       {
58         student2.name[i]=&#39;\0&#39;;
59         break;
60       }
61      }
62   }
&#39;; 59 break; 60 } 61 } 62 }

Lines 33 to 47 can be executed, but lines 48 to 62 can't be executed. When I press the Enter key, I keep breaking lines. So, I want to ask why the latter part can't be implemented, and which link in the middle has a problem?

答案1

得分: 1

以下是您要翻译的部分:

The first loop (lines 36-47) has the following problems:

  1. You are not taking into account that line 34 left a newline character on the input stream, so the first character read in line 40 will be this leftover newline character. It is also possible that other characters were left on the input stream. For example, if the user entered 6abc on line 34, then scanf on line 34 will only consume the 6, but leave abc\n on the input stream.
  2. The break statement will only break out of the innermost loop, but not the outer loop.
  3. If the user enters at least 15 characters (not including the newline character), then the string you write will not be null-terminated and your program will not consume all characters of the line of user input.

The second loop (lines 51-62) has the same problems.

In order to solve issue #1, you should discard the remainder of the line, including the newline character, for example like this:

scanf( "%*[^\n]" );

//discard the newline character itself
scanf( "%*c" );

Or like this:


do
{
    c = getchar();

} while ( c != EOF && c != '\n' );

To solve issue #2, you should remove the outer while(1) loop.

To solve issue #3, you should exit the program with an error message, instead of continuing the program.

Here is the fixed code:

#include <stdlib.h>

struct Student
{
    int number;
    char name[15];
} student1, student2, student3, student4;
 
int main( void )
{
    printf( "********************************\n" );
    printf( "*   Enter student information  *\n" );
    printf( "********************************\n" );

    //first student

    printf( "Please enter the student number of the first student: " );
    scanf( "%d", &student1.number );

    //discard remainder of line
    scanf( "%*[^\n]" );
    scanf( "%*c" );

    printf( "Please enter the first student's name: " );
    for( int h = 0; ; h++ )
    {
        if ( h == 15 )
        {
            printf( "Input is too long for input buffer!\n" );
            exit( EXIT_FAILURE );
        }

        scanf( "%c", &student1.name[h] );
        if( student1.name[h] == '\n' )
        {
            student1.name[h] = '
#include <stdlib.h>
struct Student
{
int number;
char name[15];
} student1, student2, student3, student4;
int main( void )
{
printf( "********************************\n" );
printf( "*   Enter student information  *\n" );
printf( "********************************\n" );
//first student
printf( "Please enter the student number of the first student: " );
scanf( "%d", &student1.number );
//discard remainder of line
scanf( "%*[^\n]" );
scanf( "%*c" );
printf( "Please enter the first student's name: " );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( "Input is too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
scanf( "%c", &student1.name[h] );
if( student1.name[h] == '\n' )
{
student1.name[h] = '\0';
break;
}
}
//second student
printf( "Please enter the student number of the second student: " );
scanf( "%d", &student2.number );
//discard remainder of line
scanf( "%*[^\n]" );
scanf( "%*c" );
printf( "Please enter the second student's name: " );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( "Input is too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
scanf( "%c", &student2.name[h] );
if( student2.name[h] == '\n' )
{
student2.name[h] = '\0';
break;
}
}
//print back the information
printf( "\nYou entered the following information:\n\n" );
printf( "Student 1:\n" );
printf( "Number: %d\n", student1.number );
printf( "Name: %s\n", student1.name );
printf( "\n" );
printf( "Student 2:\n" );
printf( "Number: %d\n", student2.number );
printf( "Name: %s\n", student2.name );
printf( "\n" );
} 
This program has the following behavior:
```none
********************************
*   Enter student information  *
********************************
Please enter the student number of the first student: 67
Please enter the first student's name: Mike
Please enter the student number of the second student: 78
Please enter the second student's name: Jimmy
You entered the following information:
Student 1:
Number: 67
Name: Mike
Student 2:
Number: 78
Name: Jimmy
'; break; } } //second student printf( "Please enter the student number of the second student: " ); scanf( "%d", &student2.number ); //discard remainder of line scanf( "%*[^\n]" ); scanf( "%*c" ); printf( "Please enter the second student's name: " ); for( int h = 0; ; h++ ) { if ( h == 15 ) { printf( "Input is too long for input buffer!\n" ); exit( EXIT_FAILURE ); } scanf( "%c", &student2.name[h] ); if( student2.name[h] == '\n' ) { student2.name[h] = '
#include <stdlib.h>
struct Student
{
int number;
char name[15];
} student1, student2, student3, student4;
int main( void )
{
printf( "********************************\n" );
printf( "*   Enter student information  *\n" );
printf( "********************************\n" );
//first student
printf( "Please enter the student number of the first student: " );
scanf( "%d", &student1.number );
//discard remainder of line
scanf( "%*[^\n]" );
scanf( "%*c" );
printf( "Please enter the first student's name: " );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( "Input is too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
scanf( "%c", &student1.name[h] );
if( student1.name[h] == '\n' )
{
student1.name[h] = '\0';
break;
}
}
//second student
printf( "Please enter the student number of the second student: " );
scanf( "%d", &student2.number );
//discard remainder of line
scanf( "%*[^\n]" );
scanf( "%*c" );
printf( "Please enter the second student's name: " );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( "Input is too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
scanf( "%c", &student2.name[h] );
if( student2.name[h] == '\n' )
{
student2.name[h] = '\0';
break;
}
}
//print back the information
printf( "\nYou entered the following information:\n\n" );
printf( "Student 1:\n" );
printf( "Number: %d\n", student1.number );
printf( "Name: %s\n", student1.name );
printf( "\n" );
printf( "Student 2:\n" );
printf( "Number: %d\n", student2.number );
printf( "Name: %s\n", student2.name );
printf( "\n" );
} 
This program has the following behavior:
```none
********************************
*   Enter student information  *
********************************
Please enter the student number of the first student: 67
Please enter the first student's name: Mike
Please enter the student number of the second student: 78
Please enter the second student's name: Jimmy
You entered the following information:
Student 1:
Number: 67
Name: Mike
Student 2:
Number: 78
Name: Jimmy
'; break; } } //print back the information printf( "\nYou entered the following information:\n\n" ); printf( "Student 1:\n" ); printf( "Number: %d\n", student1.number ); printf( "Name: %s\n", student1.name ); printf( "\n" ); printf( "Student 2:\n" ); printf( "Number: %d\n", student2.number ); printf( "Name: %s\n", student2.name ); printf( "\n" ); } This program has the following behavior: ```none ******************************** * Enter student information * ******************************** Please enter the student number of the first student: 67 Please enter the first student's name: Mike Please enter the student number of the second student: 78 Please enter the second student's name: Jimmy You entered the following information: Student 1: Number: 67 Name: Mike Student 2: Number: 78 Name: Jimmy

Note however that your code has a lot of code duplication, and it will get worse if you add the code for the third and fourth students. Therefore, it would be better to handle all students in a loop, by creating an additional outer loop:

#include <stdlib.h>

#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15

struct Student
{
    int number;
    char name[MAX_NAME_LENGTH];
};
 
int main( void )
{
    struct Student students[NUM_STUDENTS];

    static const char *numeral_adjectives[] =
    {
        "zeroth", "first", "second", "third", "fourth",
        "fifth", "sixth", "seventh", "eighth", "ninth"
    };

    printf( "********************************\n" );
    printf( "*   Enter student information  *\n" );
    printf( "********************************\n" );

    for ( int i = 0; i < NUM_STUDENTS; i++ )
    {
        printf(
            "Please enter the student number of the %s student: ",
            numeral_adjectives[i+1]
        );
        scanf( "%d", &students[i].number );

        //discard remainder of line
        scanf( "%*[^\n]" );
        scanf( "%*c" );

        printf(
            "Please enter the %s student's name: ",
            numeral_adjectives[i+1]
        );
        for( int j = 0; ; j++ )
        {
            if ( j == MAX_NAME_LENGTH )
            {
                printf( "Input is too long for input buffer!\n" );
                exit( EXIT_FAILURE );
            }

            scanf( "%c", &students[i].name[j] );
            if( students[i].name[j] == '\n' )
            {
                students[i].name[j] = '
#include <stdlib.h>
#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15
struct Student
{
int number;
char name[MAX_NAME_LENGTH];
};
int main( void )
{
struct Student students[NUM_STUDENTS];
static const char *numeral_adjectives[] =
{
"zeroth", "first", "second", "third", "fourth",
"fifth", "sixth", "seventh", "eighth", "ninth"
};
printf( "********************************\n" );
printf( "*   Enter student information  *\n" );
printf( "********************************\n" );
for ( int i = 0; i < NUM_STUDENTS; i++ )
{
printf(
"Please enter the student number of the %s student: ",
numeral_adjectives[i+1]
);
scanf( "%d", &students[i].number );
//discard remainder of line
scanf( "%*[^\n]" );
scanf( "%*c" );
printf(
"Please enter the %s student's name: ",
numeral_adjectives[i+1]
);
for( int j = 0; ; j++ )
{
if ( j == MAX_NAME_LENGTH )
{
printf( "Input is too long for input buffer!\n" );
exit( EXIT_FAILURE );
}
scanf( "%c", &students[i].name[j] );
if( students[i].name[j] == '\n' )
{
students[i].name[j] = '\0';
break;
}
}
}
//print back the information
printf( "\nYou entered the following information:\n\n" );
for ( int i = 0; i < NUM_STUDENTS; i++ )
{
printf( "Student %d:\n", i+1 );
printf( "Number: %d\n", students[i].number );
printf( "Name: %s\n", students[i].name );
printf( "\n" );
}
} 
<details>
<summary>英文:</summary>
The first loop (lines 36-47) has the following problems:
1. You are not taking into account that line 34 left a newline character on the input stream, so the first character read in line 40 will be this leftover newline character. It is also possible that other characters were left on the input stream. For example, if the user entered `6abc` on line 34, then `scanf` on line 34 will only consume the `6`, but leave `abc\n` on the input stream.
2. The `break` statement will only break out of the innermost loop, but not the outer loop.
3. If the user enters at least 15 characters (not including the newline character), then the string you write will not be null-terminated and your program will not consume all characters of the line of user input.
The second loop (lines 51-62) has the same problems.
In order to solve issue #1, you should discard the remainder of the line, including the newline character, for example like this:
'; break; } } } //print back the information printf( "\nYou entered the following information:\n\n" ); for ( int i = 0; i < NUM_STUDENTS; i++ ) { printf( "Student %d:\n", i+1 ); printf( "Number: %d\n", students[i].number ); printf( "Name: %s\n", students[i].name ); printf( "\n" ); } } <details> <summary>英文:</summary> The first loop (lines 36-47) has the following problems: 1. You are not taking into account that line 34 left a newline character on the input stream, so the first character read in line 40 will be this leftover newline character. It is also possible that other characters were left on the input stream. For example, if the user entered `6abc` on line 34, then `scanf` on line 34 will only consume the `6`, but leave `abc\n` on the input stream. 2. The `break` statement will only break out of the innermost loop, but not the outer loop. 3. If the user enters at least 15 characters (not including the newline character), then the string you write will not be null-terminated and your program will not consume all characters of the line of user input. The second loop (lines 51-62) has the same problems. In order to solve issue #1, you should discard the remainder of the line, including the newline character, for example like this:

//discard all leftover characters up to the newline character
scanf( "%*[^\n]" );

//discard the newline character itself
scanf( "%*c" );


Or like this:

int c;

do
{
c = getchar();

} while ( c != EOF && c != '\n' );


To solve issue #2, you should remove the outer `while(1)` loop.

To solve issue #3, you should exit the program with an error message, instead of continuing the program.

Here is the fixed code:

#include <stdio.h>
#include <stdlib.h>

struct Student
{
int number;
char name[15];
} student1, student2, student3, student4;

int main( void )
{
printf( "\n" );
printf( "* Enter student information *\n" );
printf( "
\n" );

//first student

printf( &quot;Please enter the student number of the first student: &quot; );
scanf( &quot;%d&quot;, &amp;student1.number );

//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );

printf( &quot;Please enter the first student&#39;s name: &quot; );
for( int h = 0; ; h++ )
{
    if ( h == 15 )
    {
        printf( &quot;Input is too long for input buffer!\n&quot; );
        exit( EXIT_FAILURE );
    }

    scanf( &quot;%c&quot;, &amp;student1.name[h] );
    if( student1.name[h] == &#39;\n&#39; )
    {
        student1.name[h] = &#39;
//first student
printf( &quot;Please enter the student number of the first student: &quot; );
scanf( &quot;%d&quot;, &amp;student1.number );
//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );
printf( &quot;Please enter the first student&#39;s name: &quot; );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
scanf( &quot;%c&quot;, &amp;student1.name[h] );
if( student1.name[h] == &#39;\n&#39; )
{
student1.name[h] = &#39;\0&#39;;
break;
}
}
//second student
printf( &quot;Please enter the student number of the second student: &quot; );
scanf( &quot;%d&quot;, &amp;student2.number );
//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );
printf( &quot;Please enter the second student&#39;s name: &quot; );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
scanf( &quot;%c&quot;, &amp;student2.name[h] );
if( student2.name[h] == &#39;\n&#39; )
{
student2.name[h] = &#39;\0&#39;;
break;
}
}
//print back the information
printf( &quot;\nYou entered the following information:\n\n&quot; );
printf( &quot;Student 1:\n&quot; );
printf( &quot;Number: %d\n&quot;, student1.number );
printf( &quot;Name: %s\n&quot;, student1.name );
printf( &quot;\n&quot; );
printf( &quot;Student 2:\n&quot; );
printf( &quot;Number: %d\n&quot;, student2.number );
printf( &quot;Name: %s\n&quot;, student2.name );
printf( &quot;\n&quot; );
&#39;; break; } } //second student printf( &quot;Please enter the student number of the second student: &quot; ); scanf( &quot;%d&quot;, &amp;student2.number ); //discard remainder of line scanf( &quot;%*[^\n]&quot; ); scanf( &quot;%*c&quot; ); printf( &quot;Please enter the second student&#39;s name: &quot; ); for( int h = 0; ; h++ ) { if ( h == 15 ) { printf( &quot;Input is too long for input buffer!\n&quot; ); exit( EXIT_FAILURE ); } scanf( &quot;%c&quot;, &amp;student2.name[h] ); if( student2.name[h] == &#39;\n&#39; ) { student2.name[h] = &#39;
//first student
printf( &quot;Please enter the student number of the first student: &quot; );
scanf( &quot;%d&quot;, &amp;student1.number );
//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );
printf( &quot;Please enter the first student&#39;s name: &quot; );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
scanf( &quot;%c&quot;, &amp;student1.name[h] );
if( student1.name[h] == &#39;\n&#39; )
{
student1.name[h] = &#39;\0&#39;;
break;
}
}
//second student
printf( &quot;Please enter the student number of the second student: &quot; );
scanf( &quot;%d&quot;, &amp;student2.number );
//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );
printf( &quot;Please enter the second student&#39;s name: &quot; );
for( int h = 0; ; h++ )
{
if ( h == 15 )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
scanf( &quot;%c&quot;, &amp;student2.name[h] );
if( student2.name[h] == &#39;\n&#39; )
{
student2.name[h] = &#39;\0&#39;;
break;
}
}
//print back the information
printf( &quot;\nYou entered the following information:\n\n&quot; );
printf( &quot;Student 1:\n&quot; );
printf( &quot;Number: %d\n&quot;, student1.number );
printf( &quot;Name: %s\n&quot;, student1.name );
printf( &quot;\n&quot; );
printf( &quot;Student 2:\n&quot; );
printf( &quot;Number: %d\n&quot;, student2.number );
printf( &quot;Name: %s\n&quot;, student2.name );
printf( &quot;\n&quot; );
&#39;; break; } } //print back the information printf( &quot;\nYou entered the following information:\n\n&quot; ); printf( &quot;Student 1:\n&quot; ); printf( &quot;Number: %d\n&quot;, student1.number ); printf( &quot;Name: %s\n&quot;, student1.name ); printf( &quot;\n&quot; ); printf( &quot;Student 2:\n&quot; ); printf( &quot;Number: %d\n&quot;, student2.number ); printf( &quot;Name: %s\n&quot;, student2.name ); printf( &quot;\n&quot; );

}


This program has the following behavior:

```none
********************************
*   Enter student information  *
********************************
Please enter the student number of the first student: 67
Please enter the first student&#39;s name: Mike
Please enter the student number of the second student: 78
Please enter the second student&#39;s name: Jimmy

You entered the following information:

Student 1:
Number: 67
Name: Mike

Student 2:
Number: 78
Name: Jimmy

Note however that your code has a lot of code duplication, and it will get worse if you add the code for the third and fourth students. Therefore, it would be better to handle all students in a loop, by creating an additional outer loop:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15

struct Student
{
    int number;
    char name[MAX_NAME_LENGTH];
};
 
int main( void )
{
    struct Student students[NUM_STUDENTS];

    static const char *numeral_adjectives[] =
    {
        &quot;zeroth&quot;, &quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;,
        &quot;fifth&quot;, &quot;sixth&quot;, &quot;seventh&quot;, &quot;eighth&quot;, &quot;ninth&quot;
    };

    printf( &quot;********************************\n&quot; );
    printf( &quot;*   Enter student information  *\n&quot; );
    printf( &quot;********************************\n&quot; );

    for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
    {
        printf(
            &quot;Please enter the student number of the %s student: &quot;,
            numeral_adjectives[i+1]
        );
        scanf( &quot;%d&quot;, &amp;students[i].number );

        //discard remainder of line
        scanf( &quot;%*[^\n]&quot; );
        scanf( &quot;%*c&quot; );

        printf(
            &quot;Please enter the %s student&#39;s name: &quot;,
            numeral_adjectives[i+1]
        );
        for( int j = 0; ; j++ )
        {
            if ( j == MAX_NAME_LENGTH )
            {
                printf( &quot;Input is too long for input buffer!\n&quot; );
                exit( EXIT_FAILURE );
            }

            scanf( &quot;%c&quot;, &amp;students[i].name[j] );
            if( students[i].name[j] == &#39;\n&#39; )
            {
                students[i].name[j] = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15
struct Student
{
int number;
char name[MAX_NAME_LENGTH];
};
int main( void )
{
struct Student students[NUM_STUDENTS];
static const char *numeral_adjectives[] =
{
&quot;zeroth&quot;, &quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;,
&quot;fifth&quot;, &quot;sixth&quot;, &quot;seventh&quot;, &quot;eighth&quot;, &quot;ninth&quot;
};
printf( &quot;********************************\n&quot; );
printf( &quot;*   Enter student information  *\n&quot; );
printf( &quot;********************************\n&quot; );
for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
{
printf(
&quot;Please enter the student number of the %s student: &quot;,
numeral_adjectives[i+1]
);
scanf( &quot;%d&quot;, &amp;students[i].number );
//discard remainder of line
scanf( &quot;%*[^\n]&quot; );
scanf( &quot;%*c&quot; );
printf(
&quot;Please enter the %s student&#39;s name: &quot;,
numeral_adjectives[i+1]
);
for( int j = 0; ; j++ )
{
if ( j == MAX_NAME_LENGTH )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
scanf( &quot;%c&quot;, &amp;students[i].name[j] );
if( students[i].name[j] == &#39;\n&#39; )
{
students[i].name[j] = &#39;\0&#39;;
break;
}
}
}
//print back the information
printf( &quot;\nYou entered the following information:\n\n&quot; );
for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
{
printf( &quot;Student %d:\n&quot;, i+1 );
printf( &quot;Number: %d\n&quot;, students[i].number );
printf( &quot;Name: %s\n&quot;, students[i].name );
printf( &quot;\n&quot; );
}
}
&#39;; break; } } } //print back the information printf( &quot;\nYou entered the following information:\n\n&quot; ); for ( int i = 0; i &lt; NUM_STUDENTS; i++ ) { printf( &quot;Student %d:\n&quot;, i+1 ); printf( &quot;Number: %d\n&quot;, students[i].number ); printf( &quot;Name: %s\n&quot;, students[i].name ); printf( &quot;\n&quot; ); } }

This program has the following behavior:

********************************
*   Enter student information  *
********************************
Please enter the student number of the first student: 67
Please enter the first student&#39;s name: Mike
Please enter the student number of the second student: 78
Please enter the first student&#39;s name: Jimmy
Please enter the student number of the third student: 105
Please enter the first student&#39;s name: Jane
Please enter the student number of the fourth student: 112
Please enter the first student&#39;s name: Bob 

You entered the following information:

Student 1:
Number: 67
Name: Mike

Student 2:
Number: 78
Name: Jimmy

Student 3:
Number: 105
Name: Jane

Student 4:
Number: 112
Name: Bob

Note that when dealing with line-based user input, it is generally better to use fgets instead of scanf. In contrast to scanf, fgets will always read an entire line at once, unless it is unable to do so, due to the size of the supplied memory buffer not being large enough.

That way, you won't have the problem of scanf reading partial lines whose leftovers you must discard.

Here is an example program which uses fgets instead of scanf:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15

struct Student
{
    int number;
    char name[MAX_NAME_LENGTH];
};
 
int main( void )
{
    struct Student students[NUM_STUDENTS];

    static const char *numeral_adjectives[] =
    {
        &quot;zeroth&quot;, &quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;,
        &quot;fifth&quot;, &quot;sixth&quot;, &quot;seventh&quot;, &quot;eighth&quot;, &quot;ninth&quot;
    };

    printf( &quot;********************************\n&quot; );
    printf( &quot;*   Enter student information  *\n&quot; );
    printf( &quot;********************************\n&quot; );

    for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
    {
        char input[200];
        char *p;

        //prompt user for student&#39;s number
        printf(
            &quot;Please enter the student number of the %s student: &quot;,
            numeral_adjectives[i+1]
        );

        //read exactly one line of input
        fgets( input, sizeof input, stdin );

        //convert input string to a number
        students[i].number = strtol( input, NULL, 10 );

        //prompt user for student&#39;s name
        printf(
            &quot;Please enter the %s student&#39;s name: &quot;,
            numeral_adjectives[i+1]
        );

        //attempt to read exactly one line of input
        fgets( students[i].name, sizeof students[i].name, stdin );

        //attempt to find newline character in input
        p = strchr( students[i].name, &#39;\n&#39; );

        //verify that the newline character is present,
        //because otherwise, we must assume that the input
        //was too long to fit into the memory buffer
        if ( p == NULL )
        {
            printf( &quot;Input is too long for input buffer!\n&quot; );
            exit( EXIT_FAILURE );
        }

        //remove the newline character by overwriting it
        //with a null character
        *p = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#define NUM_STUDENTS 4
#define MAX_NAME_LENGTH 15
struct Student
{
int number;
char name[MAX_NAME_LENGTH];
};
int main( void )
{
struct Student students[NUM_STUDENTS];
static const char *numeral_adjectives[] =
{
&quot;zeroth&quot;, &quot;first&quot;, &quot;second&quot;, &quot;third&quot;, &quot;fourth&quot;,
&quot;fifth&quot;, &quot;sixth&quot;, &quot;seventh&quot;, &quot;eighth&quot;, &quot;ninth&quot;
};
printf( &quot;********************************\n&quot; );
printf( &quot;*   Enter student information  *\n&quot; );
printf( &quot;********************************\n&quot; );
for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
{
char input[200];
char *p;
//prompt user for student&#39;s number
printf(
&quot;Please enter the student number of the %s student: &quot;,
numeral_adjectives[i+1]
);
//read exactly one line of input
fgets( input, sizeof input, stdin );
//convert input string to a number
students[i].number = strtol( input, NULL, 10 );
//prompt user for student&#39;s name
printf(
&quot;Please enter the %s student&#39;s name: &quot;,
numeral_adjectives[i+1]
);
//attempt to read exactly one line of input
fgets( students[i].name, sizeof students[i].name, stdin );
//attempt to find newline character in input
p = strchr( students[i].name, &#39;\n&#39; );
//verify that the newline character is present,
//because otherwise, we must assume that the input
//was too long to fit into the memory buffer
if ( p == NULL )
{
printf( &quot;Input is too long for input buffer!\n&quot; );
exit( EXIT_FAILURE );
}
//remove the newline character by overwriting it
//with a null character
*p = &#39;\0&#39;;
}
//print back the information
printf( &quot;\nYou entered the following information:\n\n&quot; );
for ( int i = 0; i &lt; NUM_STUDENTS; i++ )
{
printf( &quot;Student %d:\n&quot;, i+1 );
printf( &quot;Number: %d\n&quot;, students[i].number );
printf( &quot;Name: %s\n&quot;, students[i].name );
printf( &quot;\n&quot; );
}
}
&#39;; } //print back the information printf( &quot;\nYou entered the following information:\n\n&quot; ); for ( int i = 0; i &lt; NUM_STUDENTS; i++ ) { printf( &quot;Student %d:\n&quot;, i+1 ); printf( &quot;Number: %d\n&quot;, students[i].number ); printf( &quot;Name: %s\n&quot;, students[i].name ); printf( &quot;\n&quot; ); } }

huangapple
  • 本文由 发表于 2023年2月19日 20:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500222.html
匿名

发表评论

匿名网友

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

确定