Thread: infinite loop-having trouble debugging

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop-having trouble debugging

    This is a final project due on Tuesday...I am having trouble debugging it. the general scope of the project is to invite the user to input a list of numbers to be loaded into an array, then display the array, and perform multiple operations on the numbers from a menu. For some reason, I never get the array displayed and skip right to the menu display. I would be grateful for any assistance.

    code:
    ---------
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip> //for setprecision
    #include <cctype> //for toupper

    using namespace std;

    void LoadArray(float[], int&);
    void DisplayArray(float[], int);
    void GetChoice (int&);
    void ProcessChoice(int, float[], int&);
    void Sort(float[], int);
    void Insert(float[], int);
    void Delete(float[], int);
    void BinSearch(float[],float, bool&, int&, int&);

    bool SeqSearch(float[], float, bool&, int&);
    bool IsPresent(float[], int, bool&);
    float Average(float[], int);
    int Between(float[], int);
    float Smallest(float[], int);
    float Largest(float[], int);

    bool sorted;
    bool found;
    const int MAX=100;
    int position;
    ofstream outData; //output file

    int main()
    {
    int length=0; //length of array
    float list[MAX]; //array
    ifstream inData; //input file

    int choice; //menu choice for operation
    float average=0;
    float largest=0;
    float smallest=0;



    outData.open("A:\\listResults.dat");

    cout<<"This program will perform multiple operations on a list of numbers."<<endl;
    outData<<"This program will perform multiple operations on a list of numbers."<<endl;

    LoadArray(list,length);
    DisplayArray(list,length);

    do
    {
    GetChoice(choice);
    ProcessChoice(choice, list, length);
    }while (choice != 13);

    return 0;
    }


    //************************************************** ***
    //Functions

    void GetChoice(int& choice)


    {


    cout<<"The following is the menu."<<endl;
    cout<<"A code is provided for a respective operation."<<endl;
    cout<<"PLEASE NOTE: You must LOAD the array to enter another "<<endl;
    cout<<"data list."<<endl;
    cout<<endl;
    cout<<"1 --------------- LOAD the array"<<endl;
    cout<<"2 --------------- DISPLAY the array"<<endl;
    cout<<"3 --------------- SORT the array (ascending)"<<endl;
    cout<<"4 --------------- AVERAGE the array"<<endl;
    cout<<"5 --------------- LARGEST number display"<<endl;
    cout<<"6 --------------- SMALLEST number display"<<endl;
    cout<<"7 --------------- SEARCH the display for a number"<<endl;
    cout<<"8 --------------- BETWEEN two numbers display existing numbers"<<endl;
    cout<<"9 --------------- INSERT a number into array"<<endl;
    cout<<"10 --------------- DELETE a number in the array"<<endl;
    cout<<"13 --------------- QUIT"<<endl;
    cout<<endl;
    cout<<"Enter your choice"<<endl;
    cin>>choice;

    cout<<choice<<endl;

    }
    //************************************************** ************************

    void ProcessChoice(int choice,
    float list[],
    int& length)
    {



    switch (choice)
    {
    case '1': LoadArray(list, length);
    break;
    case '2': DisplayArray(list,length);
    break;
    case '3': Sort(list,length);
    break;
    case '4': Average(list,length);
    break;
    case '5': Largest(list,length);
    break;
    case '6': Smallest(list,length);
    break;
    case '7': IsPresent(list,length,found);
    break;
    case '8': Between(list,length);
    break;
    // case '9': Insert(list,length);
    break;
    // case '10': Delete(list,length);
    break;
    case '13': ;
    break;
    default: cout<<choice<<" is not a valid choice."<<endl;

    }
    }

    //************************************************** *************

    void LoadArray(/* out */ float list[],
    /* out */ int& length)



    //Reads the first list
    //and counts the number of values in the list

    //Precondition:
    // inData has been sucessfully opened for input or data entered via keyboard
    // && the number of values in the list <=MAX
    //
    //Postcondition:
    // length == number of input values in the list
    // && list[0....length-1] contain the input values

    {
    int counter=0; //loop control variable
    char answ; //'k' or 'f' answer
    string fileName; //input file name
    ifstream inData; //for input file stream

    cout<<"Would you prefer to input from the keyboard or a file?"<< endl;
    cout<< "Press 'k' or 'f'"<< endl;
    cin>>answ;
    if(toupper (answ)=='K')
    {
    cout<<"Enter all data at once,leaving spaces in between, then press Enter, Ctrl+D, Enter"<< endl;
    while (cin)
    cin>>list[counter];
    counter++;
    }
    else
    {
    cout<<"Input file name"<<endl;
    cin>>fileName;
    inData.open(fileName.c_str());
    //cout<<"Press Ctrl+D, Enter"<<endl;

    }
    while (inData)
    {
    inData>>list[counter];
    counter++;
    }

    length = counter-1;
    sorted = false;
    DisplayArray(list,length);

    }

    //************************************************** *

    void DisplayArray(/* in */ float list[],
    /* in */ int length)
    {
    int index;

    for(index = 0; index < length; index++)
    {
    cout<<setprecision(1)<<fixed<<showpoint;
    outData<<setprecision(1)<<fixed<<showpoint;
    cout<<index<< "." <<" "<<list[index]<<endl;

    }
    }

    //************************************************** **

    void Sort(float list[],
    int length)
    {
    float temp;
    int passCount;
    int searchIndex;
    int minIndex;

    for(passCount = 0; passCount<length-1; passCount++)
    {
    minIndex=passCount;
    for(searchIndex=passCount+1; searchIndex<length;searchIndex++)
    {
    if(list[searchIndex] < list[minIndex])
    minIndex = searchIndex;

    temp = list[minIndex];
    list[minIndex] = list[passCount];
    list[passCount] = temp;
    }
    }
    sorted = true;
    DisplayArray(list, length);
    }

    //**************************************************

    float Average(float list[],
    int length)

    {
    int index;
    float average;
    float sum=0.0;

    for (index = 0; index < length; index++)
    {
    sum=sum + list[index];
    }
    average=sum/length;

    cout<< endl<<" The average of the array is: "<<average<<endl;
    //outData<< " The average of the array is: "<<average<<endl;
    return average;

    }

    //**************************************************

    float Largest(float list[],
    int length)

    {
    int index=0;
    float largest;

    largest = list[0];

    for (index = 1; index < length; index++)
    {
    if(largest < list[index])
    largest = list[index];
    }

    cout<<endl<<"The largest number in the array is: "<<largest<<endl;
    //outData<<"The largest number in the array is: "<<largest<<endl;
    return largest;
    }

    //***********************************************

    float Smallest(float list[],
    int length)

    {
    int index=0;
    float smallest;

    smallest = list[0];

    for (index = 1; index < length; index++)
    {
    if(smallest > list[index])
    smallest = list[index];
    }
    cout<<"The smallest number in the array is: "<<smallest<<endl;
    //outData<<"The smallest number in the array is: "<<smallest<<endl;
    return smallest;
    }

    //**********************************************

    bool IsPresent(float list[],
    int length,
    bool& found)

    {
    float item;
    int index=0;

    cout<<"Enter the number you are searching for"<<endl;
    //outData<<"Enter the number you are searching for"<<endl;

    cin>>item;

    if(sorted)
    {
    BinSearch(list, item,found,length, position);
    }

    else
    {
    SeqSearch(list, item, found, length);
    }
    if(found)
    {
    cout<< "The number "<<item<<" is present in the array"<<endl;

    }
    else
    {
    cout<<"The number "<<item <<" is not present in the array"<<endl;

    }
    return found;
    }

    //***********************************************

    int Between(float list[],
    int length)

    {
    float value1;
    float value2;
    int counter=0;
    int index =0;

    cout<<"To find the array elements between to numbers, enter the two numbers"<<endl;
    cin>>value1;
    cin>>value2;

    if (value1<value2)
    {
    for(index=0; index < length; index++)
    if(list[index] > value1 && list[index] < value2)
    counter++;
    }
    else
    {
    for(index = 0; index < length; index++)
    if(list[index] > value2 && list[index] < value1)
    counter++;
    }
    return counter;
    }

    //************************************************** ***********
    void Delete(float list[], bool found, bool sorted, int& length)

    {
    float item;
    int index;
    int position;

    cout<<"Enter the number you would like to delete"<<endl;
    cin>>item;

    if(!sorted)
    {
    cout<<"The list was not previously sorted, now it has been sorted"<<endl;
    Sort(list, length);
    }
    BinSearch(list, item, found, length, position);

    if (found)
    {
    for (index=position; index<length-1; index++)
    list[index] = list[index+1];
    length--;
    DisplayArray(list, length);
    }
    else
    {
    cout<<item<<" is not in the array"<<endl;
    }
    }



    //************************************************** **********
    void BinSearch(float list[],float item, bool& found, int& length, int& position)
    {
    int first=0;
    int last=length-1;
    int middle;

    found=false;
    while (last >= first && !found)
    {
    middle=(first+last)/2;
    if (item < list[middle])
    last=middle-1;
    else if (item > list[middle])
    first=middle+1;
    else
    found=true;
    }
    if(found)
    position=middle;
    }




    //************************************************** ********
    void Insert(float list[], float item, int& length)

    {
    int index;

    index=length-1;

    cout<<"Enter the number you want to insert"<<endl;
    cin>>item;


    if(length<MAX)
    {
    while(index>= 0 && item < list[index])
    {
    list[index+1] = list[index];
    index--;
    }
    list[index+1] = item;
    length++;
    DisplayArray(list, length);
    }
    else
    cout<<"The list is already full, insertion is not possible"<<endl;
    }



    //************************************************** ******
    bool SeqSearch(float list[], float item, bool& found, int& length)

    {
    int index = 0;
    while (index < length && item != list[index])
    index++;

    if(index<length)
    found=true;
    else
    found=false;

    return found;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I didn't have any trouble getting the array displayed. I loaded the array from a file instead of the keyboard. One change I made was to your ProcessChoice() function:
    Code:
    void ProcessChoice(int choice, 
    float list[], 
    int& length) 
    { 
    
    switch (choice) 
    { 
    case 1: LoadArray(list, length); 
    break; 
    case 2: DisplayArray(list,length); 
    break; 
    case 3: Sort(list,length); 
    break; 
    case 4: Average(list,length); 
    break; 
    case 5: Largest(list,length); 
    break; 
    case 6: Smallest(list,length); 
    break; 
    case 7: IsPresent(list,length,found); 
    break; 
    case 8: Between(list,length); 
    break; 
    // case 9: Insert(list,length); 
    break; 
    // case 10: Delete(list,length); 
    break; 
    case 13: ;
    break; 
    default: cout<<choice<<" is not a valid choice."<<endl; 
    
    } 
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    Thanks, did you try to enter via keyboard? That is when all the problems started. I implemented your suggested changes: (looked like you took the single quotes off the case scenarios) All of a sudden I have 35 warnings, but I am working on them. I will try it again as soon as I get through them.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I did not try to enter via keyboard. I can't figure out how to get it to recognize EOF. <ctrl-d> does not work on my computer.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    Boy, do I sound like a first year student or what!!!! But the text says to try Ctrl+D or Ctrl+Z depending on the computer.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The Ctrl-D worked. I have the same problem. It just starts looping endlessly. The only way I was able to fix it was to add a cin.clear() and cin.seekg() in function GetChoice.

    Code:
    void GetChoice(int& choice)
    {
       char temp[10]; 
    
    
       cout<<"The following is the menu."<<endl; 
       cout<<"A code is provided for a respective operation."<<endl; 
       cout<<"PLEASE NOTE: You must LOAD the array to enter another "<<endl; 
       cout<<"data list."<<endl; 
       cout<<endl; 
       cout<<"1 --------------- LOAD the array"<<endl; 
       cout<<"2 --------------- DISPLAY the array"<<endl; 
       cout<<"3 --------------- SORT the array (ascending)"<<endl; 
       cout<<"4 --------------- AVERAGE the array"<<endl; 
       cout<<"5 --------------- LARGEST number display"<<endl; 
       cout<<"6 --------------- SMALLEST number display"<<endl; 
       cout<<"7 --------------- SEARCH the display for a number"<<endl; 
       cout<<"8 --------------- BETWEEN two numbers display existing numbers"<<endl; 
       cout<<"9 --------------- INSERT a number into array"<<endl; 
       cout<<"10 --------------- DELETE a number in the array"<<endl; 
       cout<<"13 --------------- QUIT"<<endl; 
       cout<<endl; 
       cout<<"Enter your choice"<<endl;
       cin.clear();
       cin.seekg(0);
       if (cin.good())
          cout << "GOOD." << endl;
       //cin.getline(temp,10);
       //choice = atoi(temp);
       cin >> choice;
    
       cout<<choice<<endl;
    
    }
    Also, I saw another problem. You need some brackets here:
    cout<<"Enter all data at once,leaving spaces in between, then press Enter, Ctrl+D, Enter"<< endl;
    while (cin)
    {//HERE
    cin>>list[counter];
    counter++;
    }//HERE

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    Thanks again for your tenacity. Were you able to get it to work with those changes? I was able to display the loaded array, but now it just gets stuck in the process choice.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yes, the menu part seems to work good with the changes. I am able to choose LARGEST number, SMALLEST number, etc. Here is my GetChoice(), as I left some extra stuff in the first one.
    Code:
    void	GetChoice(int& choice)
    	
    
    		{
    			
    			
    			cout<<"The following is the menu."<<endl;
    			cout<<"A code is provided for a respective operation."<<endl;
    			cout<<"PLEASE NOTE: You must LOAD the array to enter another "<<endl;
    			cout<<"data list."<<endl;
    			cout<<endl;
    			cout<<"1  --------------- LOAD the array"<<endl;
    			cout<<"2  --------------- DISPLAY the array"<<endl;
    			cout<<"3  --------------- SORT the array (ascending)"<<endl;
    			cout<<"4  --------------- AVERAGE the array"<<endl;
    			cout<<"5  --------------- LARGEST number display"<<endl;
    			cout<<"6  --------------- SMALLEST number display"<<endl;
    			cout<<"7  --------------- SEARCH the display for a number"<<endl;
    			cout<<"8  --------------- BETWEEN two numbers display existing numbers"<<endl;
    			cout<<"9  --------------- INSERT a number into array"<<endl;
    			cout<<"10 --------------- DELETE a number in the array"<<endl;
    			cout<<"13 --------------- QUIT"<<endl;
    			cout<<endl;
    			cout<<"Enter your choice"<<endl;
    
    			cin.clear();
    			cin.seekg(0);
       			cin>>choice;
    
    			cout<<choice<<endl;
    		
    		}
    I did notice however that I got a 0 at the end of the array for one of the numbers, even though I didn't enter a 0. This seems to work better for the input loop:
    Code:
    	if(toupper (answ)=='K')
    	{
    		cout<<"Enter all data at once,leaving spaces in between, then press Enter, Ctrl+D, Enter"<< endl;
    		while (cin>>list[counter])
    		{
    			counter++;
    		}
    	}
    I'm using a Windows computer, so the cin.clear() and cin.seekg() might not work right on another computer. If this doesn't work, maybe try two clears:

    cin.clear();
    cin.seekg(0);
    cin.clear();

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    I am getting bleary-eyed...I have made a lot of changes to my original code that I posted that I have lost track ...can you repost the whole thing so I can cut and paste into my workspace and try it with your changes?

    Thanks again...believe it or not, I actually didn't leave this to the last minute but the loop has gotten to me.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'll post it. If this doesn't work, I have a better idea about how to end the input loop. Read the float values in as characters, then convert them to float. To exit the loop, enter some letter like 'q'. I'll post an example in the next post.
    Code:
    //**********************************************************
    //
    // Heidi Hewlett   CPSC 111
    //
    //This program will peform multiple operations on a list of numbers.
    //
    //
    //************************************************************
    
    #include	<iostream>
    #include	<fstream>
    #include	<string>
    #include	<iomanip>		//for setprecision
    #include	<cctype>		//for toupper
    
    using namespace std;
    
    void	LoadArray(float[], int&);
    void	DisplayArray(float[], int);
    void	GetChoice (int&);
    void	ProcessChoice(int, float[], int&);
    void	Sort(float[], int);
    void	Insert(float[], int);
    void	Delete(float[], int);
    void	BinSearch(float[],float, bool&, int&, int&);
    
    bool	SeqSearch(float[], float, bool&, int&);
    bool	IsPresent(float[], int, bool&);
    float	Average(float[], int);
    int		Between(float[], int);
    float	Smallest(float[], int);
    float	Largest(float[], int);
    
    bool	sorted;
    bool	found;
    const int	MAX=100;
    int		position;
    ofstream	outData;    //output file
    
    int main()
    {
    	int			length=0;		//length of array
    	float		list[MAX];  //array
    	ifstream	inData;		//input file
    	
    	int			choice;		//menu choice for operation
    	float		average=0;
    	float		largest=0;
    	float		smallest=0;
    		
    	
    
    	outData.open("A:listResults.dat");
    	
    
    	cout<<"This program will perform multiple operations on a list of numbers."<<endl;
    	
    
    	LoadArray(list,length);
    	DisplayArray(list,length);
    	
    	do
    		{
    			GetChoice(choice);
    			ProcessChoice(choice, list, length);
    		}while (choice != 13);
    		
    return 0;	
    }
    	
    
    //*****************************************************
    //Functions
    
    void	GetChoice(int& choice)
    	
    
    		{
    			
    			
    			cout<<"The following is the menu."<<endl;
    			cout<<"A code is provided for a respective operation."<<endl;
    			cout<<"PLEASE NOTE: You must LOAD the array to enter another "<<endl;
    			cout<<"data list."<<endl;
    			cout<<endl;
    			cout<<"1  --------------- LOAD the array"<<endl;
    			cout<<"2  --------------- DISPLAY the array"<<endl;
    			cout<<"3  --------------- SORT the array (ascending)"<<endl;
    			cout<<"4  --------------- AVERAGE the array"<<endl;
    			cout<<"5  --------------- LARGEST number display"<<endl;
    			cout<<"6  --------------- SMALLEST number display"<<endl;
    			cout<<"7  --------------- SEARCH the display for a number"<<endl;
    			cout<<"8  --------------- BETWEEN two numbers display existing numbers"<<endl;
    			cout<<"9  --------------- INSERT a number into array"<<endl;
    			cout<<"10 --------------- DELETE a number in the array"<<endl;
    			cout<<"13 --------------- QUIT"<<endl;
    			cout<<endl;
    			cout<<"Enter your choice"<<endl;
    
    			cin.clear();
    			cin.seekg(0);
    			cin.clear();
       			cin>>choice;
    
    			cout<<choice<<endl;
    		
    		}
    //**************************************************************************
    
    void	ProcessChoice(int choice, 
    					  float list[], 
    					  int& length)
    {
    	
    	
    	
    	switch (choice)
    	{
    	case 1:	LoadArray(list, length);
    				break;
    	case 2:	DisplayArray(list,length);
    				break;
    	case 3:	Sort(list,length);
    				break;
    	case 4:	Average(list,length);
    				break;
    	case 5:	Largest(list,length);
    				break;
    	case 6:	Smallest(list,length);
    				break;
    	case 7:	IsPresent(list,length,found);
    				break;
    	case 8:	Between(list,length);
    				break;
    //	case 9:	Insert(list,length);
    				break;
    //	case 10:	Delete(list,length);
    				break;
    	case 13:	;
    				break;
    	default:	cout<<choice<<" is not a valid choice."<<endl;
    				
    				}
    	}
    
    //***************************************************************
    
    void	LoadArray(/* out */ float list[], 
    				  /* out */ int& length) 
    				  
    
    
    	//Reads the first list 
    	//and counts the number of values in the list
    
    	//Precondition:
    	//	inData has been sucessfully opened for input or data entered via keyboard
    	// && the number of values in the list <=MAX
    	//
    	//Postcondition:
    	// length == number of input values in the list
    	// && list[0....length-1] contain the input values
    
    {
    	int counter=0;		//loop control variable
    	char	answ;		//'k' or 'f' answer
    	string	fileName;	//input file name
    	ifstream	inData;	//for input file stream
    
    	cout<<"Would you prefer to input from the keyboard or a file?"<< endl;
    	cout<< "Press 'k' or 'f'"<< endl;
    	cin>>answ;
    	if(toupper (answ)=='K')
    	{
    		cout<<"Enter all data at once,leaving spaces in between, then press Enter, Ctrl+D, Enter"<< endl;
    		while (cin>>list[counter])
    		{
    			//cin>>list[counter];
    			counter++;
    		}
    	}
    	else
    	{
    		cout<<"Input file name"<<endl;
    		cin>>fileName;
    		inData.open(fileName.c_str());
    		//cout<<"Press Ctrl+D, Enter"<<endl;
    	
    	}
    	while (inData)
    	{
    		inData>>list[counter];
    		counter++;
    	}
    		
    	length = counter-1;
    	sorted = false;
    	DisplayArray(list,length);
    	
    }
    
    //***************************************************
    
    void DisplayArray(/* in */ float list[],
    				  /* in */ int length)
    {
    	int index;
    
    	for(index = 0; index < length; index++)
    	{
    		cout<<setprecision(1)<<fixed<<showpoint;
    		outData<<setprecision(1)<<fixed<<showpoint;
    		cout<<index<< "." <<"   "<<list[index]<<endl;
    
    	}
    }
    
    //****************************************************
    
    void Sort(float list[],
    		  int length)
    {
    	float	temp;
    	int		passCount;
    	int		searchIndex;
    	int		minIndex;
    
    	for(passCount = 0; passCount<length-1; passCount++)
    	{
    		minIndex=passCount;
    		for(searchIndex=passCount+1; searchIndex<length;searchIndex++)
    		{
    			if(list[searchIndex] < list[minIndex])
    				minIndex = searchIndex;
    
    			temp = list[minIndex];
    			list[minIndex] = list[passCount];
    			list[passCount] = temp;
    		}
    	}
    	sorted = true;
    	DisplayArray(list, length);
    }
    
    //**************************************************
    
    float Average(float list[],
    			 int length)
    
    {
    	int	index;
    	float average;
    	float sum=0.0;
    
    	for (index = 0; index < length; index++)
    	{
    		sum=sum + list[index];
    	}
    	average=sum/length;
    	
    	cout<< endl<<" The average of the array is: "<<average<<endl;
    	//outData<< " The average of the array is: "<<average<<endl;
    	return average;
    	
    }
    
    //**************************************************
    
    float Largest(float list[],
    			  int length)
    
    {
    	int index=0;
    	float largest;
    
    	largest = list[0];
    
    	for (index = 1; index < length; index++)
    	{
    		if(largest < list[index])
    			largest = list[index];
    	}
    
    	cout<<endl<<"The largest number in the array is: "<<largest<<endl;
    	//outData<<"The largest number in the array is: "<<largest<<endl;
    	return	largest;
    }
    
    //***********************************************
    
    float Smallest(float list[],
    			  int length)
    
    {
    	int index=0;
    	float smallest;
    
    	smallest = list[0];
    
    	for (index = 1; index < length; index++)
    	{
    		if(smallest > list[index])
    			smallest = list[index];
    	}
    	cout<<"The smallest number in the array is: "<<smallest<<endl;
    	//outData<<"The smallest number in the array is: "<<smallest<<endl;
    	return	smallest;
    }
    
    //**********************************************
    
    bool IsPresent(float list[], 
    			   int length,
    			   bool& found)
    
    {
    	float	item;
    	int index=0;
    	
    	cout<<"Enter the number you are searching for"<<endl;
    	//outData<<"Enter the number you are searching for"<<endl;
    
    	cin>>item;
    	
    	if(sorted)
    	{
    		BinSearch(list, item,found,length, position);
    	}
    		
    	else
    	{	
    		SeqSearch(list, item, found, length); 
    	}
    	if(found)
    	{
    		cout<< "The number "<<item<<" is present in the array"<<endl;
    	
    	}
    	else
    	{
    		cout<<"The number "<<item <<" is not present in the array"<<endl;
    	
    	}
    	return	found;
    }
    
    //***********************************************
    
    int	Between(float list[], 
    			int length)
    
    {
    	float	value1;
    	float	value2;
    	int		counter=0;
    	int		index =0;
    
    	cout<<"To find the array elements between to numbers, enter the two numbers"<<endl;
    	cin>>value1;
    	cin>>value2;
    
    	if (value1<value2)
    	{
    		for(index=0; index < length; index++)
    			if(list[index] > value1 && list[index] < value2)
    				counter++;
    	}
    	else
    	{
    		for(index = 0; index < length; index++)
    			if(list[index] > value2 && list[index] < value1)
    				counter++;
    	}
    	return counter;
    }
    
    //*************************************************************
    void Delete(float list[], bool found, bool sorted, int& length)
    
    {
    	float item;
    	int index;
    	int position;
    
    	cout<<"Enter the number you would like to delete"<<endl;
    	cin>>item;
    
    	if(!sorted)
    	{
    		cout<<"The list was not previously sorted, now it has been sorted"<<endl;
    		Sort(list, length); 
    	}
    		BinSearch(list, item, found, length, position);
    
    	if (found)
    	{
    		for (index=position; index<length-1; index++)
    			list[index] = list[index+1];
    		length--;
    		DisplayArray(list, length);
    	}
    	else
    	{
    		cout<<item<<" is not in the array"<<endl;
    	}
    }
    
    
    
    //************************************************************
    void BinSearch(float list[],float item, bool& found, int& length, int& position)
    {
    	int first=0;
    	int last=length-1;
    	int middle;
    	
    	found=false;
    	while (last >= first && !found)
    	{
    		middle=(first+last)/2;
    		if (item < list[middle])
    			last=middle-1;
    		else if (item > list[middle])
    			first=middle+1;
    		else
    			found=true;
    	}
    	if(found)
    	position=middle;	
    }
    
    
    
    
    //**********************************************************
    void Insert(float list[], float item, int& length)
    
    {
    	int index;
    
    	index=length-1;
    
    	cout<<"Enter the number you want to insert"<<endl;
    	cin>>item;
    	
    
    	if(length<MAX)
    	{
    		while(index>= 0 && item < list[index])
    		{
    			list[index+1] = list[index];
    			index--;
    		}
    		list[index+1] = item;
    		length++;
    		DisplayArray(list, length);
    	}
    	else
    		cout<<"The list is already full, insertion is not possible"<<endl;
    }
    
    
    
    //********************************************************
    bool SeqSearch(float list[], float item, bool& found, int& length)
    
    {
    	int index = 0;
    	while (index < length && item != list[index])
    		index++;
    
    	if(index<length)
    		found=true;
    	else
    		found=false;
    
    	return found;
    }
    Last edited by swoopy; 12-11-2001 at 12:18 AM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I just added some code tags to that last post, that way the indentation is left intact.

    Here is another way to do the input loop:
    Code:
    	if(toupper (answ)=='K')
    	{
    		char cfloat[25];
    		cout<<"Enter all data at once,leaving spaces in between, then press Q then Enter"<< endl;
    		cin >> cfloat;
    		while (toupper(cfloat[0]) != 'Q')
    		{
    			list[counter] = (float) atof(cfloat);
    			counter++;
    			cin >> cfloat;
    		}
    	}
    Using this loop, I believe the cin.clear() and cin.seekg() is no longer needed.
    Last edited by swoopy; 12-11-2001 at 12:32 AM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    which input method do you recommend? The other way seems to be working but which way is more practical?

    Thank you! for your help, I can finally see what the other functions are doing...there is a light at the end of the tunnel

    The between function is not working...it is supposed to show the numbers in the array that are between any two numbers that the user inputs...ex: in an array:1,2,3,4,5,6 if the user inputs 2 and 6 the display should say "the array contains 3 values between the numbers you entered. Should I put this cout in the Between function? if I do, should I make the between function a void function?

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would leave the Between() function as it is, and do this in your switch() statement:

    case 8: cout << endl << "The array contains " << Between(list,length)
    << " values between the numbers you entered." << endl;
    break;

    As for the input method, if the other way is working, then probably just leave it that way. And just keep the other method in mind in case you ever need it.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    OK, I am trying it completely through now, don't go to sleep yet!
    If I still lived in GA, I would buy you a beer, but I've migrated north.

    I'm going to try it now. I could probably do a lot of those couts through the switch statements, is there any preference on how that is done?

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    infinite loop

    1. 4.0
    2. 8.0
    3. 9.0
    0. 1.0
    1. 4.0
    2. 8.0
    3. 9.0

    this is the display I get after I first enter the numbers,hit enter, ctrl+D, enter. How do I get rid of that duplicate display? It doesn't do that on the second time through?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  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 signal chaining
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 04-18-2008, 10:14 AM
  4. infinite loop error
    By tsubasa in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 02:37 PM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM