GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jan-2008, 21:19
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

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.

CPP / C++ / C Code:
//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;
}
  #2  
Old 08-Jan-2008, 00:43
pfanning pfanning is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 39
pfanning is on a distinguished road

Re: Char arry in a function with spaces


Hi all,

I figured it out. In case anyone is interested:

www.cplusplus.com
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer initialization causing program abend? emanresu C Programming Language 0 12-Dec-2006 11:36
Database Program goldfish C Programming Language 6 13-May-2006 14:12
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35
Compiling Errors ToddSAFM C++ Forum 22 18-Dec-2004 12:42
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:16.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.