problem with gets and bubble sort

This is a discussion on problem with gets and bubble sort within the C Programming forums, part of the General Programming Boards category; Why is it that when I run this, I it will ask for the name the first time but when ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    15

    problem with gets and bubble sort

    Why is it that when I run this, I it will ask for the name the first time but when it loops around it will skip asking for name and go straight to asking for the account number??
    I just cant seem to get it to work. Also my bubble sort will not sort the names, only acct # and the balance. Please help!
    Code:
    	struct info{char name[30]; int account_num; float balance;
    	 };
    	struct info people[25];
    	int x,a,y,temp=0;
    
    //loop to collect info*/
    
    
    		for(x = 1; x <= 3; ++x)
    		{	 
      			 printf("Enter Name: ");
    		     gets(people[x].name);
    	             
                 printf("Enter Account Number: ");			 
    		     scanf("%i",&people[x].account_num);
    		     
                 printf("Enter Balance: ");		 
    		     scanf("%f",&people[x].balance);			 
    			 
    		     printf("\n");
    		}
    
    //bubble sort
    		for (a=1; a<x-1;++a)
    		{		
    		for (x=1; x<3;++x)
    		{
    		if (people[x].account_num>people[x+1].account_num)
    		{
    			temp = people[x+1].account_num;
    		 	people[x+1].account_num = people[x].account_num;
                people[x].account_num = temp;					
    			    
    			temp2 = people[x+1].name[30];
    		 	people[x+1].name[30] = people[x].name[30];
                people[x].name[30] = temp2;
    					 
    				
    			temp = people[x].balance;
    			people[x].balance = people[x+1].balance;
                people[x+1].balance = temp;
                 
    		}/*end if*/
    		}/*end nested for*/
    		}/*end for loop*/
    	char temp2=0;

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    scanf leaves new line in the buffer while gets read it as a line

    there is a lot of samples on the forum - how to clear the stream after the scanf before reading with fgets (that should be used instead of gets - read FAQ)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Arrays start at zero, not one.
    This means that there is no name[30], i.e. that's a buffer overrun. Besides, when swapping the items around you want to swap the entire string, not just one character.
    You're the second person in a week to be unnecesarily trying to swap items by swapping every individual part of the structure. You don't need to do that in this case. This will do the whole swap for you just fine:
    Code:
    info temp = people[x+1];
    people[x+1] = people[x];
    people[x] =  temp;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe the reason is that K&R and most other C programming books, barely mention it.

    BTW, I found the combsort you mentioned iMalc, and combsort 11 is a keeper, beating out Shellsort. and giving Qsort a run for it's money.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Adak View Post
    I believe the reason is that K&R and most other C programming books, barely mention it.

    BTW, I found the combsort you mentioned iMalc, and combsort 11 is a keeper, beating out Shellsort. and giving Qsort a run for it's money.
    Yeah I've recommended it a few times because it's so simple and easy to understand once you get BubbleSort, yet good enough to compete with some of the more advanced algorithms.
    A good QuickSort still beats it most of the time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

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