C program student ID

Pages: 12
closed account (DLUk92yv)
Hey everybody, I need a little help here. I am new to C programming and I can't work out this problem.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>

int main()
{
	//declare variables
	
	int id;
	int age;
	char name[16];
	char class_name[4];
	
	//get information
	
	printf("Enter ID: ");
	scanf("%d",&id);
	
	printf("Enter class_name: ");
	scanf("%s",class_name);
	
	printf("Enter name: ");
	scanf("%s",name);
	
	printf("Enter age: ");
	scanf("%d",&age);
	
	//printing out information given
	
	printf("\n\n\nStudent ID  :  %d\n",id);
	printf("Student name:  %s\n",name);
	printf("class name  :  %s\n",class_name);
	getchar();
	printf("Student age :  %d\n",age);
	
	printf("\n");
	
	return 0;
}


Look at this and compare with yours, see your mistakes and correct them.
closed account (DLUk92yv)
thanks but my problem is slightly different since i have 4 students and i have to print only one of them just by entering the ID
So you enter info about 4 students. Then you enter the ID for the one you want his info to be printed?
Do you know struct ??
Last edited on
closed account (DLUk92yv)
first i enter only the ID, then the info about 4 students, after that according to the ID i entered at the beginning the info of one of those 4 students will be printed..i dont know if i explained it the right way..
and nope i dont know struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>

int main()
{

	char name[4][16];
	int age[4];
	int id[4];
	char class_name[4][4];
	
	int ID;
	printf("Enter ID: ");
	scanf("%d",&ID);
	
	int i;
	for(i = 0; i < 4; i++)
	{
		printf("\n\nStduent %d\n",i+1);
		
		printf("Enter ID: ");
		scanf("%d",&id[i]);
		
		printf("Enter name: ");
		scanf("%s",name[i]);
		
		printf("Enter class name: ");
		scanf("%s",class_name[i]);
		getchar();
		printf("Enter age: ");
		scanf("%d",&age[i]);
	}
	
	//..SEARCH INDEX OF ID
	printf("Information of ID : = %d\n\n",ID);
	for(i =0; i < 4; i++)
		if(ID == id[i])
			break;
	
	if(i == 4)
		printf("ID not found\n");
	else
	{
		printf("\n\n\nStudent ID  :  %d\n",id[i]);
		printf("Student name:  %s\n",name[i]);
		printf("class name  :  %s\n",class_name[i]);
		printf("Student age :  %d\n",age[i]);
		
	}
}
closed account (DLUk92yv)
THANK YOU! You are a life saver
You welcome.
closed account (DLUk92yv)
wait one more question, char class_name[4][4]; why [4][4]??
The first 4 is for the number of students. The second 4 is the max length of the name.
class name: 3 characters long string which contains only letters and digits.

The extra 1 (4-3) is for the null character.
closed account (DLUk92yv)
i get it now, thanks again :)
You welcome again
closed account (DLUk92yv)
I'm sorry can i ask you for one more thing? this program its a little bit complicated for me to understand all and in the answer i should send for example:
I type ID 538. then it says:
1
then i can continue to the next step..and i dont want that 1.
+ in the end i want output to be just in one line..
i tried to delete some things and here its what i left from the code(I also changed the names)

#include <stdio.h>

int main()
{

char classname[4][16];
int id[4];
int grade[4];
char name[4][4];
char surname[4][4];

int ID;

scanf("%d",&ID);

int i;
for(i = 0; i < 4; i++)
{
printf("%d\n",i+1);


scanf("%d",&id[i]);


scanf("%s",classname[i]);


scanf("%s",name[i]);
getchar();

scanf("%s",surname[i]);
getchar();

scanf("%d",&grade[i]);
}

//..SEARCH INDEX OF ID
printf("%d\n\n",ID);
for(i =0; i < 4; i++)
if(ID == id[i])
break;

if(i == 4)
printf("ID not found\n");
else
{
printf("%d\n %s\n %s\n %s\n %d\n",id[i], classname[i], name[i], surname[i], grade[i] );


}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>

int main()
{

char classname[4][16];
int id[4];
int grade[4];
char name[4][4];
char surname[4][4];

int ID;

scanf("%d",&ID);

int i;
for(i = 0; i < 4; i++)
{
//printf("%d\n",i+1);   //remove the 1 you dont like

printf("\n\n");	//just to give a space between each new student input
scanf("%d",&id[i]);
scanf("%s",classname[i]);


scanf("%s",name[i]);
getchar();

scanf("%s",surname[i]);
getchar();

scanf("%d",&grade[i]);
}

//..SEARCH INDEX OF ID
printf("%d\n\n",ID);
for(i =0; i < 4; i++)
if(ID == id[i])
break;

if(i == 4)
printf("ID not found\n");
else
{
//removed '/n' 
printf("%d    %s    %s    %s     %d\n",id[i], classname[i], name[i], surname[i], grade[i] );


}
}

Is this what you want?

Do you want to finish my your welcome?? kidding.

Work on your indentation and use code tags.
Last edited on
closed account (DLUk92yv)
haha thanks for the third time, i promise ain't asking again :P
We are here to help any number of times you ask or encounter difficulties.
closed account (DLUk92yv)
thank god for that
closed account (DLUk92yv)
i need to ask you for one more question..i know i suck, im sorry!
So the question is that a professor is teaching in two classes. There are two students in one class and three in the other. According to the informations I have to find the class with the highest average.

So the input should contain( You will be given five lines of information. In every line, you will have class name, student name, student surname, and student grade. Where class name is at most 3 characters containing only English letters or digit from 0 to 9. Name and surname are at most 15 characters containing only English letters. Student grade is an integer between 1 and 100.)

and here is my code(your code, i tried using what u sent me earlier since i thought it might be the same but im stucked now, dont know what to do next)

#include <stdio.h>

int main()
{
char classname[5][4];
char* name[5][16];
char* surname[5][16];
int grade[5];

int i;
for (i=0; i<5; i++)
{
scanf("%s",classname[i]);
scanf("%s", name[i]);
scanf("%s", surname[i]);
scanf("%d", &grade[i]);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char classname[5][4];
	char name[5][16];
	char surname[5][16];
	int grade[5];

	int i;
	for (i=0; i<5; i++)
	{
		scanf("%s",classname[i]);
		scanf("%s", name[i]);
		scanf("%s", surname[i]);
		scanf("%d", &grade[i]);
	}
	//GOOD TO GO...

	//IF YOU ARE VERY SURE THAT THERE ARE ONLY 2 CLASSES....
	int class_grades[2] = {0};
	char classname_0[5];	
	char classname_1[5];
	strcpy(classname_0,classname[0]);

	for(int  i = 0; i < 5; i++)
	{
		if(strcmp(classname_0,classname[i]) == 0)
			++class_grades[0];
		else
		{
			strcpy(classname_1,classname[i]);
			++class_grades[1];
		}
	}
	char *winner = NULL;
	(class_grades[0] > class_grades[1]) ? winner = classname_0 : winner = classname_1;

	printf("The class with the highest grade is \"%s\"\n",winner);

	
	system("pause");
	return 0;
}
closed account (DLUk92yv)
Thank you thank you thank you :)
Pages: 12