我在C中学习Structures
,并尝试为此问题编写解决方案 -
创建一个结构来指定下面给出的学生数据:Roll 编号,姓名,部门,课程,加入年份。
假设班上的学生人数不超过5人。 (a)写一个函数 打印在特定年份加入的所有学生的姓名。 (b)写 打印给出滚动号码的学生数据的功能。
接下来给出我的代码,简要描述程序中的各种组件,然后是问题的根源 -
#include<stdio.h>
struct student
{
int roll_no;
char name[20];
char department[30];
char course[20];
int year_joined;
};
void main()
{
struct student s[5];
int i=0,choice;
printf("\n Enter the following information for each student: \n");
printf("\n Roll No., Name, Department, Course, Year of Joining\n");
for(i=0;i<=4;i++)
scanf("%d %s %s %s %d", &s[i].roll_no, &s[i].name, &s[i].department, &s[i].course, &s[i].year_joined);
printf("\n Please choose from the following options :\n");
printf("\n 1. Print names of students who joined in a given year.");
printf("\n 2. Print data of all students whose roll.no is given.");
scanf("%d",&choice);
switch(choice)
{
case 1: display_names(s);
break;
case 2: student_data(s);
break;
default: printf("\n Incorrect choice, please try again.");
}
}
void display_names(struct student p[])
{
int i,count=0;
int year;
printf("\n Enter the year you wish to search student info. for : \n");
scanf("%d",&year);
for(i=0;i<=4;i++)
{
if(p[i].year_joined==year)
{
//printf("%d %s %s %s %d", p[i].roll_no, p[i].name, p[i].department, p[i].course, p[i]. year_joined);
count++;
printf("\n Student Name: %s\n", p[i].name);
}
}
printf("\n Total Number of students who joined in the year %d is %d\n", year,count);
if(count==0)
printf("\n No match found.");
}
void student_data(struct student st[])
{
int i,count=0;
int roll;
printf("\n Enter roll number of the student: \n");
scanf("%d",&roll);
for(i=0;i<=4;i++)
{
if(st[i].roll_no=roll)
{
count++;
printf("\n Student Data: Roll No. Name Department Course Year of Joining \n");
printf("\n %d %s %s %s %d", st[i].roll_no, st[i].name, st[i].department, st[i].course,st[i].year_joined);
break;
}
}
if(count==0)
printf("\n No matching Roll Numbers found\n");
}
以下是该计划的简要说明 -
struct student
在最开始定义,数组在main- struct student s[5]
scanf()
内的main()
接受用户输入来初始化结构。这两个函数有两个switch
个案例,问题是display_names(s) and student_data(s)
structure array
并要求用户输入一年。它遍历初始化的数组并搜索用户输入的输入(加入年份)。如果匹配,它会打印出那一年加入的所有学生的姓名。这是此功能的示例输出 - *
**Enter the following information for each student:
Roll No., Name, Department, Course, Year of Joining
10 Tim CS MS 2013
20 Shane CS MS 2013
30 John EE ME 2013
40 Mark MECH MS 2013
50 Matt EE ME 2010
Please choose from the following options :
1. Print names of students who joined in a given year.
2. Print data of all students whose roll.no is given.1
Enter the year you wish to search student info. for :
2013
Student Name: Tim
Student Name: Shane
Student Name: John
Student Name: Mark
Total Number of students who joined in the year 2013 is 4**
*
此功能正常。
问题出在下一个函数student_data()
中。与第一个函数类似,它接受结构数组作为输入。我设计功能的方式 - 想法是循环遍历数组,直到它找到与用户输入的滚动数相等的滚动数。如果匹配,它将打印出该特定学生本身的详细信息。但是,就目前而言,此函数始终打印数组中的第一个条目,我似乎无法理解为什么。我在break
中添加了if statement
,但它似乎没有任何区别。
这是一个示例输出 -
Enter the following information for each student:
Roll No., Name, Department, Course, Year of Joining
10 Tim CS MS 2013
20 Shane CS MS 2013
30 John EE ME 2013
40 Mark MECH MS 2013
50 Matt EE ME 2010
Please choose from the following options :
1. Print names of students who joined in a given year.
2. Print data of all students whose roll.no is given.2
Enter roll number of the student:
40
Student Data: Roll No. Name Department Course Year of Joining
40 Tim CS MS 2013
你能否说出我在第二个功能中出错的地方?
答案 0 :(得分:2)
你在student_data()中的if语句是分配而不是比较。
考虑使用-Wall(或特别是-Wparentheses)标志进行编译:
#include <stdio.h>
int main( void )
{
int a = 10;
if ( a = 11 ) printf( "a equals 11\n" );
return 0;
}
使用-Wall:
$ gcc main.c -Wall
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if ( a = 11 ) printf( "a equals 11\n" );
$