I have a c++ assignment I'm trying to finish but I keep getting no results for options 2,3,4 of my code and when I use (4) the sort feature I'm only getting a partial display of my contacts. And when I try to search, the program runs on a repetitious loop displaying the menu over and over again. I didn't think limits applied here but I am at a loss for where I need to set boundaries and why my blocks of code are not running at all of 2 and 3. Can anyone shed some light on this? Thanks!
#include <iostream>
#include <string>
#include <cstring>
#include <ios>
#include <limits>
using namespace std;
// define two parallel arrays
string name[3];
long long phoneNumber[3];
int arraySize = 3;
void inputData(){
//Get user inputs
for(int i=0; i<arraySize; i++){
cout<<"\nEnter the "<<i+1<<" person name: ";
getline(cin, name[i]);
cout<<"Enter the "<<i+1<<" person number: ";
cin>>phoneNumber[i];
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
}
}
void ascending(){
//Sort data ascending
string tmp;
long long tmpn;
for(int i=0;i<arraySize;i++)
{
for(int j=i+1;j<arraySize;j++)
{
if(name[i]>name[j])
{
tmp=name[i];
name[i]=name[j];
name[j]=tmp;
tmpn=phoneNumber[i];
phoneNumber[i]=phoneNumber[j];
phoneNumber[j]=tmpn;
}
}
}
}
void descending(){
//sort data in descending order
string tmp;
long long tmpn;
for(int i=0;i<arraySize;i++)
{
for(int j=i+1;j<arraySize;j++)
{
if(name[i]<name[j])
{
tmp=name[i];
name[i]=name[j];
name[j]=tmp;
tmpn=phoneNumber[i];
phoneNumber[i]=phoneNumber[j];
phoneNumber[j]=tmpn;
}
}
}
}
void printData(){
//print the name and number after sort
for(int j=0; j<arraySize; j++){
cout<<name[j]<<'\t'<<phoneNumber[j]<<endl;
}
}
void search(){
//search for an individual
string sear;
cout<<"Enter the record to Search - ";
cin>>sear;
for(int i=0;i<arraySize;i++)
{
if (sear==name[i]){
cout<<name[i]<<" "<<"phoneNumber[i]";
return;
}
}
cout<<"Record Not Fount";
}
int main()
{
int choice;
while(1){
cout<<"\t Welcome to your contact manager\t\t\t"<<endl;
cout<<"Menu Options: "<<endl;
cout<<"Press 1 to input data "<<endl;
cout<<"Press 2 to sort in ascending order "<<endl;
cout<<"Press 3 to sort in descending order "<<endl;
cout<<"Press 4 to print the data "<<endl;
cout<<"Press 5 to search the data "<<endl;
cout<<"Press 6 to end the program "<<endl;
cin>>choice;
switch(choice)
{
case 1:
cin.ignore();
inputData();
break;
case 2:
// sort in ascending order
cin.ignore();
ascending();
break;
case 3:
//sort in descending order
cin.ignore();
descending();
break;
case 4:
printData();
break;
case 5:
// search the data
cin.ignore();
search();
break;
case 6:
return 0;
break;
default:
;
}
}
return 0;
}