I am having some issues. I was trying to use my setToolName function to copy the string into the array – this would check if the string was too long. But then it does not capture 2 words, say if the string is “lawn mower”. So I started to use getline and this works. The issue is it does not truncate the string if it is too long for the array. Below are my 2 functions and I was wondering how I could fix them to get this to work and to work efficiently:

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);

		// user enters tool name, quantity and unit price
		cout << "Enter the tool name: ";
		fflush(stdin);
		cin.getline( toolName, 20, '\n' );
	   
		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

Specifically within this function:

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

And then my setToolName function:

// set the tool name
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 < 20 ? length : 19);
    strncpy(toolName, toolNameValue, length);
    //toolName[ length ] = '\0'; // append null character to lastName
}

Recommended Answers

All 8 Replies

you are doing too much work!

int main()
{
    char name[10] = {0};

   strncpy(name,"How Now Brown Cow", sizeof(name));
   name[sizeof(name)-1] = 0;
   cout << name << "\n";

}

you are doing too much work!

int main()
{
    char name[10] = {0};

   strncpy(name,"How Now Brown Cow", sizeof(name));
   name[sizeof(name)-1] = 0;
   cout << name << "\n";

}

No I cannot do this because then even record can have its own size and it will not be consistent to print the records and align them.

you are doing too much work!

int main()
{
    char name[10] = {0};

   strncpy(name,"How Now Brown Cow", sizeof(name));
   name[sizeof(name)-1] = 0;
   cout << name << "\n";

}

So now I am trying to use the following code so that getline can get all words typed in and then the setToolName function can get the string, count the length and then truncate it if need be. My code is the following but it does not work because apparently getline only works with arrays? How can I create a variable to input all works and not stop at the spaces in between the words?

// user enters tool name, quantity and unit price
		cout << "Enter the tool name: ";
		fflush(stdin);
		cin.getline( tempToolName);
		setToolName(tempToolName);

If I stay to the following code then I have the issue where it keeps previous input and tags it on to the end of the array. I am not sure how to clear this:

cout << "Enter the tool name: ";
		fflush(stdin);
		cin.getline( toolName, 20, '\n' );

>>fflush(stdin);
Nonstandard. fflush() is only for use with output streams. Some compilers has added support for input streams, but such implementations are non standard. Read this and this too

>>No I cannot do this because then even record can have its own size and it will not be consistent to print the records and align them.
Huh? But what I posted was just what you were trying to do. All I did was delete the need to calculate the length -- strnlen() will do that.

>>My code is the following but it does not work because apparently getline only works with arrays?
There are two forms of getline: one for std::string and the other for character arrays.

std::string name;
getline(cin, name);

or
char name[20];
cin.getline(name, sizeof(name));

>>If I stay to the following code then I have the issue where it keeps previous input and tags it on to the end of the array
If you are experience that problem then there is something else wrong because getline() doesn't work that way.

>>fflush(stdin);
Nonstandard. fflush() is only for use with output streams. Some compilers has added support for input streams, but such implementations are non standard. Read this and this too

>>No I cannot do this because then even record can have its own size and it will not be consistent to print the records and align them.
Huh? But what I posted was just what you were trying to do. All I did was delete the need to calculate the length -- strnlen() will do that.

>>My code is the following but it does not work because apparently getline only works with arrays?
There are two forms of getline: one for std::string and the other for character arrays.

std::string name;
getline(cin, name);

or
char name[20];
cin.getline(name, sizeof(name));

>>If I stay to the following code then I have the issue where it keeps previous input and tags it on to the end of the array
If you are experience that problem then there is something else wrong because getline() doesn't work that way.

Ok well I am still frustrated with this problem. And I can't let this issue in working with strings go because I know it must be simple but for some reason I can't figure it out. Anyhow this is the code that I decide to go with. In my header file I have char toolName[20]; declared as private.

So now within my enterRecords( ) function in the .cpp file I have the following code. The issue is that it does not let me enter a tool name. It jumps from requesting a tool name to requesting the number in stock. So what do I need to say in between this to give the user time to enter the tool name?

// user enters tool name, quantity and unit price
		cout << "Enter the tool name: ";
		cin.getline(toolName, sizeof(toolName));
				   
		cout << "Enter the quantity in stock: "; 
		cin >> inStock;
		cout << "Enter the unit price: ";
		cin >> unitPrice;

>>It jumps from requesting a tool name to requesting the number in stock
That is the typical behavior of some previous cin for entering a number because when you enter a number the program leaves the '\n' (Enter key) in the keyboard buffer, then when entering a string cin thinks the Enter key is pressed. To solve that problem you need to clear the keyboard buffer of all its contents -- please read this thread about how to do that. You should put that code snipped after numeric data input.

That's because there's something, probably a single new line char, left in the input stream buffer because you are mixing getline() and >> in the same program. Place a line like this just before the call to getline().

cin.ignore(1086);

That should clear the input stream buffer. It isn't perfect, but it isn't so convoluted you need an instruction booklet either.

That's because there's something, probably a single new line char, left in the input stream buffer because you are mixing getline() and >> in the same program. Place a line like this just before the call to getline().

cin.ignore(1086);

That should clear the input stream buffer. It isn't perfect, but it isn't so convoluted you need an instruction booklet either.

Thank you AncientDragon :) I got it to work. Now I am trying to figure out my last problem. Not sure if you can help, but if possible, that would be great ;-D

http://www.daniweb.com/forums/thread138061.html

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.