R
richardha241
I have problem sorting an array of books with variable char. Let assume I put three names in instead of books. The names Bill Clinton, George Bush, and Hillary Johnson. After the sort with my codes, the array is emptied. What is wrong? How do I sort with char array type instead of string? Here are the codes:
#include <cstdlib>
#include <iostream>
#include <iomanip>
//#include "mainMenu.h"
#include <string>
using namespace std;
void lookUpBook(char[][51]);
void addBook(char[][51]);
const int arrSize=20;
char bookTitle[arrSize][51];
int main(int argc, char *argv[])
{
addBook(bookTitle);
lookUpBook(bookTitle);
system("PAUSE");
return 0;
}
void lookUpBook(char bookTitle[][51]) //stub function
{
//char searchBook[51];
char searchBook[51];
cout<<"Enter book title to be searched:";
cin.getline(searchBook,51);
int startScan, minIndex;
char minValue[51];
for (startScan =0; startScan < arrSize-1; startScan++)
{
strcpy(bookTitle[startScan],minValue);
minIndex= startScan;
for (int index = startScan+1; index < arrSize; index++)
{
if (strcmp(bookTitle[index],minValue)<0)
{
strcpy(minValue,bookTitle[index]);
minIndex=index;
}
}
strcpy(bookTitle[minIndex],bookTitle[startScan]);
strcpy(bookTitle[startScan],minValue);
}
//display book titile array to see if the array sorted correctly
cout<<"The sorted book title are: "<<endl;
for (int count = 0; count < arrSize; count++)
cout<<"Book "<<count+1<<": "<<bookTitle[count]<<endl;
// starting binary search
int first =0, last=arrSize-1, middle, position=-1;
bool found = false;
while (!found && first <= last)
{
middle= (first+last)/2;
if (strcmp(bookTitle[middle],searchBook)==0)
{
found = true;
position=middle;
}
else if (strcmp(bookTitle[middle],searchBook)>0)
last=middle-1;
else
first=middle+1;
}
if (position==-1)
cout<<"The book does not exist in inventory.\n\n";
else
cout<<"The book is found record "<<position<<". \n\n";
}
void addBook(char bookTitle[][51])
{
char choice;
for (int count =0; count < arrSize; count++)
{
if (strcmp(bookTitle[count],"")==0) // if (bookTible[count]=="") won't work, got to use the comparing string function strcmp
{
cout<<"Enter the title of the book: ";
cin.getline(bookTitle[count],51);
cout<<"Do you want to enter another book? (Y/N):";
cin>>choice;
cin.ignore();
if (choice =='Y' || choice =='y');
else
break;
}
else if(bookTitle[arrSize-1]!="")
{
cout<<"No more book may be added to inventory.\n";
}
}
for (int count2=0; count2<arrSize; count2++)
cout<<"Book "<<count2+1<<": "<<bookTitle[count2]<<endl;
}
#include <cstdlib>
#include <iostream>
#include <iomanip>
//#include "mainMenu.h"
#include <string>
using namespace std;
void lookUpBook(char[][51]);
void addBook(char[][51]);
const int arrSize=20;
char bookTitle[arrSize][51];
int main(int argc, char *argv[])
{
addBook(bookTitle);
lookUpBook(bookTitle);
system("PAUSE");
return 0;
}
void lookUpBook(char bookTitle[][51]) //stub function
{
//char searchBook[51];
char searchBook[51];
cout<<"Enter book title to be searched:";
cin.getline(searchBook,51);
int startScan, minIndex;
char minValue[51];
for (startScan =0; startScan < arrSize-1; startScan++)
{
strcpy(bookTitle[startScan],minValue);
minIndex= startScan;
for (int index = startScan+1; index < arrSize; index++)
{
if (strcmp(bookTitle[index],minValue)<0)
{
strcpy(minValue,bookTitle[index]);
minIndex=index;
}
}
strcpy(bookTitle[minIndex],bookTitle[startScan]);
strcpy(bookTitle[startScan],minValue);
}
//display book titile array to see if the array sorted correctly
cout<<"The sorted book title are: "<<endl;
for (int count = 0; count < arrSize; count++)
cout<<"Book "<<count+1<<": "<<bookTitle[count]<<endl;
// starting binary search
int first =0, last=arrSize-1, middle, position=-1;
bool found = false;
while (!found && first <= last)
{
middle= (first+last)/2;
if (strcmp(bookTitle[middle],searchBook)==0)
{
found = true;
position=middle;
}
else if (strcmp(bookTitle[middle],searchBook)>0)
last=middle-1;
else
first=middle+1;
}
if (position==-1)
cout<<"The book does not exist in inventory.\n\n";
else
cout<<"The book is found record "<<position<<". \n\n";
}
void addBook(char bookTitle[][51])
{
char choice;
for (int count =0; count < arrSize; count++)
{
if (strcmp(bookTitle[count],"")==0) // if (bookTible[count]=="") won't work, got to use the comparing string function strcmp
{
cout<<"Enter the title of the book: ";
cin.getline(bookTitle[count],51);
cout<<"Do you want to enter another book? (Y/N):";
cin>>choice;
cin.ignore();
if (choice =='Y' || choice =='y');
else
break;
}
else if(bookTitle[arrSize-1]!="")
{
cout<<"No more book may be added to inventory.\n";
}
}
for (int count2=0; count2<arrSize; count2++)
cout<<"Book "<<count2+1<<": "<<bookTitle[count2]<<endl;
}