prog problem

This is a discussion on prog problem within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    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?

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    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.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Post the structure declaration because it contains the variable declarations.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    5
    typedef struct
    {
    char regnum[4];
    int cred;
    char fname[20],sname[20];
    float cost [75];
    float fee [95];



    there you go.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Simple calculator prog
    By Stiks in forum C Programming
    Replies: 3
    Last Post: 10-20-2005, 11:00 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21