String Scanning

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[50];
char id[15];
float cgpa;
int pref[5];
int flag;
}s[50];
struct option
{
char title[100];
char teacher[50];
char dept[5];
char cat[5];
int all;
}o[50];
main()
{
int n,m,i,j,k,count;
char temp[100];
printf("Please enter the number of projects available\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Please enter project title\n");
gets(o[i].title);
printf("Please enter teacher in charge\n");
gets(o[i].teacher);
printf("Please enter department\n");
gets(o[i].dept);
printf("Please enter project category\n");
gets(o[i].cat);
}
printf("Please enter the number of students for whom allotment should be made\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Please enter the name of the student\n");
gets(s[i].name);
printf("Please enter the ID of the student\n");
gets(s[i].id);
printf("Please enter CGPA\n");
scanf("%f",&s[i].cgpa);
printf("Please enter preferences\n");
for(j=0;j<n;j++)
printf("%d: %s\n",j,o[j].title);
for(k=0;k<5;k++)
scanf("%d",&s[i].pref[k]);
}
for(i=0;i<n;i++)
o[i].all=0;
for(i=0;i<m;i++)
s[i].flag=-1;
char tname[50], tid[15];
float tcgpa;
int tpref;
for(i=m-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(s[j].cgpa<s[j+1].cgpa)
{
strcpy(tname,s[j].name);
strcpy(s[j].name,s[j+1].name);
strcpy(s[j+1].name,tname);
strcpy(tid,s[j].id);
strcpy(s[j].id,s[j+1].id);
strcpy(s[j+1].id,tid);
tcgpa=s[j].cgpa;
s[j].cgpa=s[j+1].cgpa;
s[j+1].cgpa=tcgpa;
for(k=0;k<5;k++)
{
tpref=s[j].pref[k];
s[j].pref[k]=s[j+1].pref[k];
s[j+1].pref[k]=tpref;
}
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<5;j++)
{
if(o[s[i].pref[j]].all==0)
{
s[i].flag=s[i].pref[j];
o[s[i].pref[j]].all=1;
break;
}
else
continue;
}
}
for(i=0;i<m;i++)
{
if(s[i].flag==-1)
printf("Student Name: %s\nStudent ID: %s\nStudent CGPA: %f\n No allotment made\n",s[i].name,s[i].id,s[i].cgpa);
else
printf("Student Name: %s\nStudent ID:%s\nStudent CGPA: %f\nProject allotted: %s\nTeacher incharge: %s\nCategory: %s\n",s[i].name,s[i].id,s[i].cgpa,o[s[i].flag].title,o[s[i].flag].teacher,o[s[i].flag].cat);
}
system("pause");
}


Hey guys!
This is a program I have written for basically allotting projects based on the gpa of a student and his preferences.
The problem lies in the scanning. I have tried all variations of the code, but it doesn't work. It skips scanning o[0].title and does not scan s[i].name at all.
I need to get this project done by tomorrow morning, so I really require some assistance.

Thanks!



Last edited on
Hi,

What is happening is that scanf is leaving behind a linefeed which is picked up
by the following gets() command

Ive added the following in main using the junk variable (you can rename it).

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
int	main()
{
	char	junk[5];         //THIS LINE ADDED
	int n,m,i,j,k,count;
	char temp[100];
	printf("Please enter the number of projects available\n");
	scanf("%d",&n);
	gets(junk);             //THIS LINE ADDED

	for(i=0;i<n;i++)
	{
		printf("Please enter project title\n");
		gets(o[i].title);
		printf("Please enter teacher in charge\n");
		gets(o[i].teacher);
		printf("Please enter department\n");
		gets(o[i].dept);
		printf("Please enter project category\n");
		gets(o[i].cat);
	}

	printf("Please enter the number of students for whom allotment should be made\n");
	scanf("%d",&m);

	for(i=0;i<m;i++)
	{
		gets(junk);         //THIS LINE ADDED

		printf("Please enter the name of the student\n");
		gets(s[i].name);
		printf("Please enter the ID of the student\n");
		gets(s[i].id);
		printf("Please enter CGPA\n");
		scanf("%f",&s[i].cgpa);
		printf("Please enter preferences\n");

		for(j=0;j<n;j++)
			printf("%d: %s\n",j,o[j].title);

		for(k=0;k<5;k++)
				scanf("%d",&s[i].pref[k]);
	}

Hope this helps
Shredded
Topic archived. No new replies allowed.