-
prog problem
I'm having a prob with the input of this program.
printf("Enter registration number ");
scanf("%c%c",&studdetails[i].regnum,&dummy);
printf("Enter surname ");
gets(studdetails[i].sname);
printf("Enter forename ");
gets(studdetails[i].fname);
printf("Enter credits being undertaken ");
scanf("%d",&studdetails[i].cred);
whenever i try to input everything it skips past the surname part. could i please have some help?
-
Post the structure declaration. I think your problem has to do with the combination of library functions you are using to get user input. C functions such as scanf can leave character in the standard input buffer that can destroy subsequent attempts to get user input. It's better to just use one type of function that you know well and that is safe such as fgets. The use of gets is not safe. So post the structure declaration at least for me, because I like to reference it.
-
argh!!!!
ok, i thought about what you said, I changed the scanf to a gets and took out the **** like the 2 %c's and the &dummy but it still not working, I hate xmas holidays, mademe orget about what i had learned before.
-
Post the structure declaration because it contains the variable declarations.
-
typedef struct
{
char regnum[4];
int cred;
char fname[20],sname[20];
float cost [75];
float fee [95];
there you go.
-
Try this code.
Code:
#include <stdio.h>
struct Stype
{
char regnum[4];
int cred;
char fname[20],sname[20];
float cost [75];
float fee [95];
};
int main()
{
struct Stype studdetails;
printf("Enter registration number ");
fgets(studdetails.regnum,4,stdin);
while(getchar() != '\n');
printf("Enter surname ");
fgets(studdetails.sname,20,stdin);
printf("Enter forename ");
fgets(studdetails.fname,20,stdin);
printf("Enter credits being undertaken ");
scanf("%d",&studdetails.cred);
while(getchar() != '\n');
printf("Your surname is: %s", studdetails.sname);
return 0;
}