infinite loop

This is a discussion on infinite loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been working on this program for a few hours and i'm getting very frustrated. It will work and ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    infinite loop

    Hi,

    I've been working on this program for a few hours and i'm getting very frustrated. It will work and then i'll try to do the next part of it, and it will get screwed up again.

    The part that i'm having trouble with right now:

    the assignment asks to read in a name, phone#, and id#, and to then have it sort all of the information by the id #.

    i'm trying to use a bubble sort but it is causing an infinite loop somewhere

    at one point, the ids were getting sorted but the rest of the information wasn't. Now, i screwed it up and nothing is getting sorted

    this is the file, "student.txt" that i was having sorted:

    Lee, Bruce 876-543-9876 78905
    Deris, Andi 987-654-3214 54321
    Kwestel, Morty 678-543-7465 45678

    if anyone could look over the code and help me out, i would greatly appreciate it.

    - Thank you
    bruce
    Attached Files Attached Files

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    *shrug* Works fine for me. What compiler are you using?
    The id's are only getting sorted because you're only exchanging the id numbers and not the actual names of the people. You want to pass exchange() two nodes to exchange, not two id numbers.

    Code:
    #include "stdafx.h"
    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>			//set width to output phone #
    #include<stdlib.h>
    
    #define IN ("student.txt")
    const int SIZE = 3;
    
    struct node
    {
    	char lastname[20];
    	char firstname[20];
    	char phonenum[12];
    /*
    	struct phonenum
    	{
    		int areacode;
    		int num1;
    		int num2;
    */
    
    	double id;
    };
    
    void readfile();
    void sort(node[], int);
    void exchange(node &, node &);
    //void search();
    void display(node[], int);
    
    int main()
    {
    	readfile();   
        
    	return 0;
    }
    
    void readfile()
    {
    
    	ifstream fin(IN);
    	int size = 0;
    	node info[SIZE];					// 10?
    
    	int i =0;
    
    	while (!fin.eof())
    	{	
    		if (i < SIZE)
    		{	
    					
    			fin >> info[i].lastname;
    			fin >> info[i].firstname;
    			fin >> info[i].phonenum;
    			fin >> info[i].id;
    			
    					
    			i++;
    			size++;
    			
    		}
    		sort(info, size);
    	}
    	display(info, size);
    }
    
    void display(node info[], int size)
    {
        for (int i=0; i < size; i++)
    		{
    			cout << info[i].lastname << " " << info[i].firstname << "\t" 
    				 << info[i].phonenum << "     " << info[i].id<<"\n";
    			
    		}
    	
    }
    
    void sort (node info[], int size)
    {
    	int top;
    	bool sorted = 0;
    
    	while (!sorted)
    	{
    		sorted = 1;
    		for (top = 0; top < size -1; top++)
    		{
    			if (info[top].id > info[top +1].id)
    			{
    				exchange(info[top], info[top+1]);
    				sorted = 0;
    			}
    		}
    	}
    }
    
    void exchange (node &x, node &y)
    {
    	node temp = x;
    	x = y;
    	y = temp;
    
    }
    That WORKS. It will NOT work if there is a new line at the end of your students.txt file. It happened to me when I added another element to test. So, my guess is you have an extra new line at the end of your data file.
    Last edited by Eibro; 11-03-2002 at 04:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

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