Hello,

I have seriously tried to work on this in different manners and now I am back to an infinite output. I have posted for the past 3 days and the responses have just been questions so I am not sure if this is something difficult but I really need help with this. I cannot get this string to store in the array appropriately.

My code is the following:

public:
	Tools(int = -1, string = "", int = 0, double = 0.0);	// default tools constructor
	// accessor functions for partNumber, toolName, inStock, unitPrice
	void setPartNumber(int);
	int getPartNumber() const;
	void validatePartNumber(int);

	void setToolName(string);
	string getToolName() const;

	void setInStock(int);
	int getInStock() const;

	void setUnitPrice(double);
	double getUnitPrice() const;

	void createAndInitializeTextFile();
	void enterRecords();
	bool test();
	void printHeader();

	void processChoice();
	int enterChoice();

	void printTextFile(fstream&);
	void updateRecord(fstream&);
	void newRecord( fstream& );
	void deleteRecord( fstream& );
	void outputLine( ostream&, const Tools & );
	int getPart( const char * const );

private:
	static const int size = 20;
	enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
	int partNumber;			// part id number (tool identifitcation number) is the record number (record key)
	char toolName[20];		// tool name
	int inStock;			// in stock
	double unitPrice;		// price per unit
}; // end class Tools

Member function definitions:

void Tools::setToolName(string toolNameString)
{
   // copy at most 20 characters for the tool name
    const char *toolNameValue = toolNameString.data();
    int length = int(toolNameString.size());
    length = (length < size ? length : 19);
    strncpy(toolName, toolNameValue, length);
    toolName[ length ] = '\0'; // append null character to lastName
} 




void Tools::enterRecords()
{
	 fstream outTools( "hardware.dat", ios::in | ios::out | ios::binary );

    // exit program if fstream cannot open file
    if ( !outTools )
    {
       cerr << "File could not be opened." << endl;
       exit( 1 );
    } // end if

	// require user to specify a tool identification number
	cout << "Enter tool identification number (1 to 100, 0 to end input): ";
	cin >> partNumber;
    
    Tools tool; // create the object

	// user enters information, which is copied into file
	while (partNumber != 0)
	{
		validatePartNumber(partNumber);
		string tempTool = "";

		// user enters tool name, quantity and unit price
		cout << "Enter the tool name: ";
		cin >> tempTool;
		setToolName(tempTool);
		
		//fflush(stdin);
		//cin.getline( toolName, size, '\n' );
		//cin.clear();
		
		//cin.get(toolName,size);
		
		//cin >> toolName;
		//setToolName(toolName);

			   
		cout << "Enter the quantity in stock: "; 
		cin >> inStock;
		cout << "Enter the unit price: ";
		cin >> unitPrice;

		// set the record for the part number, tool name, quantity in stock, and unit price
		tool.setPartNumber( partNumber );
		//tool.setToolName( toolName );
		tool.setInStock( inStock );
		tool.setUnitPrice( unitPrice );

		// seek position in file of user-specified record   
		outTools.seekp( ( tool.getPartNumber() - 1 ) * sizeof ( Tools ) );                         

		// write user-specified information in file                   
		outTools.write( reinterpret_cast < const char * >( &tool ),sizeof ( Tools) );     
                             
		//enable user to enter another account
		cout << "\nEnter tool identification number (1 to 100, 0 to end input): ";
		cin >> partNumber;

	} // end while loop

} // end enterRecords function

>I cannot get this string to store in the array appropriately.
exactly what do you mean by this ?? whats going wrong ? :-/

Just a few things I noticed here:

void Tools::setToolName(string toolNameString)
{
   // copy at most 20 characters for the tool name
    const char *toolNameValue = toolNameString.c_str();
    int length = int(toolNameString.length());
    strncpy(toolName, toolNameValue, length);
    toolName[ length ] = '\0'; // append null character to lastName
}

Shooting Yourself in the Foot story...

Suppose you have toolNameString.length() is equal to 40 (too long for toolName char[20] array). Now see your code with /// added comments:

// copy at most 20 characters for the tool name
    const char *toolNameValue = toolNameString.c_str();
    int length = int(toolNameString.length());    /// length = 40
    strncpy(toolName, toolNameValue, length); /// Welcome to overwrite my char array!
    toolName[ length ] = '\0'; // append null character to lastName /// last nail for the grave

Right code (if toolName is an array, of course):

strncpy(toolName,toolNameValue,sizeof toolName);
toolName[sizeof(toolName) - 1] = '\0';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.