database problem

This is a discussion on database problem within the C Programming forums, part of the General Programming Boards category; Ok this program is supposed to small database for at least 5 people. I thought i had it right when ...

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

    database problem

    Ok this program is supposed to small database for at least 5 people.
    I thought i had it right when i compiled and built it had 0 errors, but when I ran it, it would skip over certain input fields and just go to the next. Then when it's supposed to print out the results in the end I get the wrong inputs.

    Any help is appreciated.
    TIA





    Code:
    #include <stdio.h>
    
    main(void)
    
    {
    	
    	int num;
    	int count;
    		
    	struct people 
    	{
    		char full_name[50];
    		int age;
    		float weight;
    		char state[2];
    		char occupation[50];
    	} person[50];
    	
    	printf("How many people would you like to include?\n");
    	scanf("%d", &num);
    	for(count = 0; count< num; count++)
    
    {
    
    	printf("Your full name\n");
    	gets(person[count].full_name);
    	
    	printf("Your age\n");
    	scanf("%d", &person[count].age);
    	
    	printf("Your weight\n");
    	scanf("%f", &person[count].weight);
    	
    	printf("Your state\n");
    	scanf("%c", &person[count].state);
    	
    	printf("Your occupation\n");
    	gets(person[count].occupation);
    	
    }
    	
    	printf("These are your results:\n");
    
    	for(count=0; count< num; count++)
    
    {
    		printf("%c\n", person[count].full_name);
    		printf("%d\n", person[count].age);
    		printf("%f\n", person[count].weight);
    		printf("%c\n", person[count].state);
    		printf("%c\n", person[count].occupation);
    }
    
    	return (0);
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    try not using gets ...

    search in the bord for why it is bad.


    use fgets with the file name "stdin"



    good luck,,,

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    getchar(variable) or scanf ("%[^\n]", variable) is your friend.

  4. #4
    FCF
    FCF is offline
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Also, use int main(void). Y? Search in the board.

    Your program skipped some input because it reads the previous "enter" key left. So, fflush(stdin) is your friend.

    if dun want fflush, DIY it.

    Code:
    #define FLUSH while(getc()!='\n')
    just call the FLUSH every time u want to clear the input stream.
    Hope this help...
    Life is difficult...

  5. #5
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    why so "ma fan"

    use stdlib.h
    with :
    fflush (stdin);

    ---------------

    or
    scanf ("\n%c", variable);

    the '\n' will be discarded saving into variable.
    ---------------

    or
    scanf (" %c", variable); (empty between the code)

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    thanks i didn't know about the fflush command

    and i changed the gets commands to scanf

    and i changed the %c to %s so it works

    thanks for the help

  7. #7
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by teknikal
    thanks i didn't know about the fflush command

    and i changed the gets commands to scanf

    and i changed the %c to %s so it works

    thanks for the help
    well, whenever you enter the input, input save into input stream. when '\n' detected, before the '\n' will be saved into the 'variable', so the ramaining '\n' still in input stream, you need flush the input, just like a flushing the toilet, so that your toilet is clean, right?

    why you wan to use fflush,

    whenever, the sys asked user enter again the 2nd input. '\n' is char, and then your 2nd of requested input is also char, so '\n' save into the 2nd variable. so the system will not allow user to enter the 2nd of the input, and then continue running the next step.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  3. Developing database management software
    By jdm in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 05:06 PM
  4. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 10:17 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

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