我没有使用结构获得任何输出
I am not getting any output using structures
我应该打印平均分最高的学生的名字,但我的程序没有输出任何东西,有什么解决办法吗?当我尝试在 printf 中将 %s 更改为 %c 时,我成功获得了输出,但显然只有第一个字符如何输出整个字符串?
#include<stdio.h>
#include "LV2 Z2.h"
int main()
{
int n,i;
struct student students[40];
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name:\n");
scanf("%s",&students[i].name);
printf("Enter the surname:\n");
scanf("%s",&students[i].surname);
printf("Enter the ID:\n");
scanf("%d",&students[i].studentID);
printf("Enter the average:\n");
scanf("%f",&students[i].average);
printf("Enter the date:\n");
scanf("%s",&students[i].date);
}
float max = 0;
int bestStudent = 0;
for(i=0;i<n;i++)
{
if(students[i].average > max) {
max = students[i].average;
bestStudent = i;
}
}
printf("Student : %s %s ima najbolji prosjek",students[bestStudent].name,students[bestStudent].surname);
return 0;
}
struct student
{
char name[100];
char surname[100];
int studentID;
float average;
char date[100];
};
没有你的头文件,我运行你的程序。我不得不将您的结构定义移至 main 之上,以便 main 能够识别它。否则,它不会编译。您之前是否在头文件中定义了结构,但为了向我们展示,您只是将其移至 main 中?如果是这样,那将导致您的程序无法运行。
我还更改了涉及 %s 的 scanf()。数组的名称计算为第一个元素的地址,即
int myArray[20]; // myArray evaluates as &myArray[0]
所以,删除所有涉及 %s 的 scanf 的 & 符号。
进行这些更改后,我 运行 你的程序似乎运行良好。
主要
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
struct student
{
char name[100];
char surname[100];
int studentID;
float average;
char date[100];
};
int main()
{
int n, i;
struct student students[40];
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the name:\n");
scanf("%s", students[i].name);
printf("Enter the surname:\n");
scanf("%s", students[i].surname);
printf("Enter the ID:\n");
scanf("%d", &students[i].studentID);
printf("Enter the average:\n");
scanf("%f", &students[i].average);
printf("Enter the date:\n");
scanf("%s", students[i].date);
}
float max = 0;
int bestStudent = 0;
for (i = 0; i < n; i++)
{
if (students[i].average > max) {
max = students[i].average;
bestStudent = i;
}
}
printf("Student : %s %s ima najbolji prosjek", students[bestStudent].name, students[bestStudent].surname);
return 0;
}
输出
4
Enter the name:
John
Enter the surname:
Smith
Enter the ID:
111
Enter the average:
90
Enter the date:
2/6/21
Enter the name:
Sarah
Enter the surname:
Jones
Enter the ID:
222
Enter the average:
95
Enter the date:
2/6/21
Enter the name:
Mike
Enter the surname:
Johnson
Enter the ID:
333
Enter the average:
80
Enter the date:
2/6/21
Enter the name:
Bob
Enter the surname:
Sampson
Enter the ID:
444
Enter the average:
70
Enter the date:
2/6/21
Student : Sarah Jones ima najbolji prosjek
我应该打印平均分最高的学生的名字,但我的程序没有输出任何东西,有什么解决办法吗?当我尝试在 printf 中将 %s 更改为 %c 时,我成功获得了输出,但显然只有第一个字符如何输出整个字符串?
#include<stdio.h>
#include "LV2 Z2.h"
int main()
{
int n,i;
struct student students[40];
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name:\n");
scanf("%s",&students[i].name);
printf("Enter the surname:\n");
scanf("%s",&students[i].surname);
printf("Enter the ID:\n");
scanf("%d",&students[i].studentID);
printf("Enter the average:\n");
scanf("%f",&students[i].average);
printf("Enter the date:\n");
scanf("%s",&students[i].date);
}
float max = 0;
int bestStudent = 0;
for(i=0;i<n;i++)
{
if(students[i].average > max) {
max = students[i].average;
bestStudent = i;
}
}
printf("Student : %s %s ima najbolji prosjek",students[bestStudent].name,students[bestStudent].surname);
return 0;
}
struct student
{
char name[100];
char surname[100];
int studentID;
float average;
char date[100];
};
没有你的头文件,我运行你的程序。我不得不将您的结构定义移至 main 之上,以便 main 能够识别它。否则,它不会编译。您之前是否在头文件中定义了结构,但为了向我们展示,您只是将其移至 main 中?如果是这样,那将导致您的程序无法运行。
我还更改了涉及 %s 的 scanf()。数组的名称计算为第一个元素的地址,即
int myArray[20]; // myArray evaluates as &myArray[0]
所以,删除所有涉及 %s 的 scanf 的 & 符号。
进行这些更改后,我 运行 你的程序似乎运行良好。
主要
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
struct student
{
char name[100];
char surname[100];
int studentID;
float average;
char date[100];
};
int main()
{
int n, i;
struct student students[40];
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the name:\n");
scanf("%s", students[i].name);
printf("Enter the surname:\n");
scanf("%s", students[i].surname);
printf("Enter the ID:\n");
scanf("%d", &students[i].studentID);
printf("Enter the average:\n");
scanf("%f", &students[i].average);
printf("Enter the date:\n");
scanf("%s", students[i].date);
}
float max = 0;
int bestStudent = 0;
for (i = 0; i < n; i++)
{
if (students[i].average > max) {
max = students[i].average;
bestStudent = i;
}
}
printf("Student : %s %s ima najbolji prosjek", students[bestStudent].name, students[bestStudent].surname);
return 0;
}
输出
4
Enter the name:
John
Enter the surname:
Smith
Enter the ID:
111
Enter the average:
90
Enter the date:
2/6/21
Enter the name:
Sarah
Enter the surname:
Jones
Enter the ID:
222
Enter the average:
95
Enter the date:
2/6/21
Enter the name:
Mike
Enter the surname:
Johnson
Enter the ID:
333
Enter the average:
80
Enter the date:
2/6/21
Enter the name:
Bob
Enter the surname:
Sampson
Enter the ID:
444
Enter the average:
70
Enter the date:
2/6/21
Student : Sarah Jones ima najbolji prosjek