|
Char arry in a function with spaces
Hi,
I am trying to figure out why when entering 'aTitle', a char array in the function titleEntry, goes into an infinite loop when a space is added. aTitle It works fine in main(). So, I think it must have something to do with the way I am passing it. I also cut a bunch of code from the program below to save space. If it is unclear as a result, I will post it in its entirety.
//Test project for CS 540 Pierce project
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Book : public InventoryItem{
private:
char title[100];
char author[100];
char description[1000];
int type;
int numberInStock;
float cost;
float totalCost;
public:
Book (char atitle[], char theAuthor[], char anDescription[]){
strcpy(title, atitle);
strcpy(author, theAuthor);
strcpy(description, anDescription);
type = 0;
numberInStock = 0;
cost = 0.0;
totalCost = 0.0;
}
};
class Inventory{
private:
InventoryItem *pI[1000];
int n;
public:
Inventory() : n(0){}
~Inventory(){
cout << "In Inventory destructor with n = " << n << endl;
for (int i = 0; i < n; i++) {
cout << "deleting pI[" << i << "]" << endl;
delete pI[i];
}
cout << "Leaving Inventory destructor" << endl;
}
void createBook(char title[], char author[], char description[])
{
pI[n] = new Book(title, author, description);
++n;
}
};
Inventory I1;
int titleEntry (int option, char aTitle[], int& mediaType, int check);
int main()
{
char aTitle[100], aAuthor[100], aDescription[1000];
int mediaType, choice = 0, index = -1, newUnits = 0, toSell, check = 10;
float cost = 0.0;
ofstream echo;
while ( choice != 6){
cout<<"\nPlease enter option 1 - 6:"<<endl;
cout<<"1 to create a title"<<endl;
cout<<"2 to receive an order"<<endl;
cout<<"3 to calculate the price of an item"<<endl;
cout<<"4 to sell an item"<<endl;
cout<<"5 to generate a report"<<endl;
cout<<"6 to quit the application"<<endl;
cout<<"\nPlease enter your choice: ";
cin>>choice;
cin.ignore();
cout<< fixed <<showpoint; //set output to fixed decimal format
cout<< setprecision(2); //limit output to 2 digits after decimal point
echo.open("c:\\temp\\echoIventory.txt"); //open the echo .txt file
if (choice ==1){
check = titleEntry(choice, aTitle, mediaType, check);
if ( check > -400 )
{
cout<< "\nAuthor: ";
cin.getline(aAuthor, sizeof(aAuthor));
cout<< "\nDescription: ";
cin.getline(aDescription, sizeof(aDescription));
if (mediaType == 0)
I1.createBook(aTitle, aAuthor, aDescription);
}
check = 10;
}
return (0);
}
int titleEntry (int option, char aTitle[], int& mediaType, int check)
{
char recover[4] = {'X', 'X', 'X', '\0'};
cout<<"Please enter a title or XXX to return to the main menu"<<endl;
cout<< "Title: ";
cin.getline(aTitle, sizeof(aTitle));//************ goes into an infinite loop
check = strcmp(aTitle, recover);
if (check ==0)
{
check = -500;
}
if ( check > -400 ){
cout<< "Enter media type, '0' for a book, '1' for a CD ";
cout<< "or '2' for a tape: ";
cin>> mediaType;
cin.ignore();
check = I1.lookup(aTitle, mediaType);
}
if (check > 0 && option == 1)
{
cout<<"\nThat title already exists"<<endl;
}
if ( ( check < 0) && ( ( option == 2 ) || (option == 3) || (option == 4) ) )
{
cout<< "\nSorry, title not found. Please try again."<<endl;
}
while (( check > 0 && option == 1 ) || ( ( check < 0 ) && ( option == 2 || option == 3 || option == 4 ) ))
{
cout<<"inside while check= "<<check<<endl;
cout<<"\nPlease reenter the title or 'XXX' to return to main menu: ";
cin.getline(aTitle, sizeof(aTitle));
check = strcmp(aTitle, recover);
if ( check != 0 )
{
check = 1;
cout<< "Reenter media type, '0' for a book, '1' for a CD ";
cout<< "or '2' for a tape: ";
cin>> mediaType;
cin.ignore();
}
if ( check > 0 ){
check = I1.lookup(aTitle, mediaType);
}
} // end while loop
if ( (check >= 1) && option ==2 )
{
return (check - 1);
}
return check-1;
}
|