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;
}