Hello again.

For the current lab, we're working with strings and pointers. I'm trying to keep this one simple and get one step working right before I move on to the next. However, if I have subsequent issues with this lab, I'll just use this same thread rather and make more.

In the first part of this lab we are to have the user input a last name, first name, and middle name. That input needs to be put into an array as a string. This fixed array will be used later on for pointer and DMA play.

The code I have written for this first part compiles, and runs, but it skips the input for the last name.

Allow me to show you what I mean:

//=============================================================================
#include <iostream>		// for cin,cout
#include <cstring>             // for string manipulation

using namespace std;
//=============================================================================
//					CONSTANT DEFINITIONS
// none
//=============================================================================
//					EXTERNAL CLASS VARIABLES
// none
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************
int main()
{
               // declare local constant(s), variable(s), and array(s)
	char LName[32];
	char FName[16];
	char MName[16];
	int go;

	cout << "Enter 1 to Continue 0 to Quit" << endl;
	cin >> go;
		// loop to get things going
	 if (go > 0)
	 {
		cout << "\n\nEnter Last Name\n\n";
		cin.getline ( LName, 32, '\n');
		cout << "\n\nEnter First Name\n\n";
		cin.getline ( FName, 16, '\n');
		cout << "\n\nEnter Middle Name\n\n";
		cin.getline ( MName, 16, '\n');
       
		// bring it all together
		cout << "Hello " << FName << " " << MName << " " << LName << " >*^.^*< " <<endl;
		// do it again
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }
	 else 
		
	return 0;
}

/* 
 O U T P U T
Enter 1 to Continue 0 to Quit
1


Enter Last Name



Enter First Name

Bob


Enter Middle Name

David
Hello Bob David  >*^.^*<
Enter 1 to Continue 0 to Quit
*/

What is making it automatically skip the first input?

Recommended Answers

All 11 Replies

It just looks like it's getting skipped. There is a dangling newline on the input stream that is getting consumed by the input statement for the last name. To remove it so that your next input behaves properly you need to use a cin.ignore() immediately after your input statement for the user's choice.

It just looks like it's getting skipped. There is a dangling newline on the input stream that is getting consumed by the input statement for the last name. To remove it so that your next input behaves properly you need to use a cin.ignore() immediately after your input statement for the user's choice.

Done, though it doesn't seem to have helped matters. It's still losing something in the memory buffer. Should I put the cin.ignore somewhere else?

//=============================================================================
#include <iostream>		// for cin,cout
#include <cstring>     // for string manipulation

using namespace std;
//=============================================================================
//					CONSTANT DEFINITIONS
//
//=============================================================================
//					EXTERNAL CLASS VARIABLES
//
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************
int main()
{
		  // declare local constant(s), variable(s), and array(s)
	char LName[32];
	char FName[16];
	char MName[16];
	int go;

	cout << "Enter 1 to Continue 0 to Quit" << endl;
	cin >> go;
		// loop to get things going
	 if (go > 0)
	 {
		cout << "\n\nEnter Last Name\n\n";
		cin.getline ( LName, 32, '\n');
		cin.ignore();
		cout << "\n\nEnter First Name\n\n";
		cin.ignore();
		cin.getline ( FName, 16, '\n');
		cout << "\n\nEnter Middle Name\n\n";
		cin.ignore();
		cin.getline ( MName, 16, '\n');
        
		// bring it all together
		cout << "\n\nHello " << FName << " " << MName << " " << LName << " >*^.^*< \n\n" <<endl;
		// do it again
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }
	 else 
		
	return 0;
}

/*  O U T P U T
Enter 1 to Continue 0 to Quit
1


Enter Last Name

Smith


Enter First Name



Enter Middle Name

David


Hello ith avid  >*^.^*<


Enter 1 to Continue 0 to Quit
*/

You didn't put it in the correct spot.

Put it

immediately after your input statement for the user's choice

which would be after Line 25...

Member Avatar for embooglement

Also the second

cout << "Enter 1 to Continue 0 to Quit" << endl;
cin >> go;"

won't actually repeat when the user enters 1. To do that you need to change it from "if (go > 0)" to "while (go > 0)." That will cause the block to repeat until go is less than or equal to 0, whereas an if statement will only execute once.

Also the second

cout << "Enter 1 to Continue 0 to Quit" << endl;
cin >> go;"

won't actually repeat when the user enters 1. To do that you need to change it from "if (go > 0)" to "while (go > 0)." That will cause the block to repeat until go is less than or equal to 0, whereas an if statement will only execute once.

Thanks. I've spent a little more time working on this. I re-structured it a bit to give it a little more clarity, but it's still getting me lost. Here's how it looks now. It compiles, but need help. Please.

//=============================================================================
// Program Requirements
// 1. Prompt User for Last, 1st, and Middle Name
// 2. Place each entry into separate fixed size array
// 3. Dynamically allocate an array to store all name parts.
// 4. Copy name parts in order into new array.
// Note: Be sure to have a space between names.
// 5. Pass new array into function to display name
// 6. Deallocate the array.
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************

#include <iostream>		// for cin,cout
#include <cstring>     // for string manipulation
 
using namespace std;
// =============================================================================

int main()
{
		  // declare local constant(s), variable(s), and array(s)
	char LName[32];		// fixed size array
	char FName[16];		// fixed size array
	char MName[16];		// fixed size array
	int go;				// input to start
	char* name = NULL;	// pointer to char, initilized to nothing
	char n;				// size needed for new array
	
 
		// Start program
	cout << "Enter 1 to Continue 0 to Quit" << endl;		
	cin >> go;
	cin.ignore();

		// Prompt User for Last, 1st, and Middle Name
	 while (go > 0)
	 {
		cout << "\n\nEnter Last Name\n\n";
		cin.getline ( LName, 32, '\n');		// set 1st array
		cout << "\n\nEnter First Name\n\n";		
		cin.getline ( FName, 16, '\n');		// set 2nd array
		cout << "\n\nEnter Middle Name\n\n";
		cin.getline ( MName, 16, '\n');		// set 3rd array

		//Dynamically allocate an array to store all name parts.

		name = new char[n];			// Allocate n array and save ptr in name
		for (int i =0; i<n; i++)
		{
			name[i] = 0;			// all elements set to zero
		}

		delete []name;				// free memory pointed to by name
		name = NULL;				// clear buffer

		// Copy name parts in order into new array.
		strcpy(name, LName);

		// Option to Continue
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }

		
 
	return 0;
}

Troubleshooting as I go, I see that I've got the de-allocation placed in too early. That should come after I set up the new array.

Regarding that though, once I strcopy into the new array, do I append to place the space between the parts? Where does concatenation come into play here? On the middle and last names? Do I use strlen before to make sure that the new array will be large enough to hold the copied parts?

name = new char[n];

You are allocating memory for a new char array but you are using a variable with data type char for the size declaration. n is a char type and should not be used to declare the size of the array and it is never initialized or given a value.

As for your other questions...

once I strcopy into the new array, do I append to place the space between the parts?

Personally, because you are using char arrays instead of string objects, I would append a space to the end of fName before you concatenate. If you were using strings it would be a little easier.

Do I use strlen before to make sure that the new array will be large enough to hold the copied parts?

Yes, always perform bounds checking. something like:

name = new char[strlen(fName) + strlen(lName) + 1] // Extra 1 is for null terminator

As for your other questions...

Personally, because you are using char arrays instead of string objects, I would append a space to the end of fName before you concatenate. If you were using strings it would be a little easier.

Yes, always perform bounds checking. something like:

name = new char[strlen(fName) + strlen(lName) + 1] // Extra 1 is for null terminator

Thank you so much for your help. It's immensely appreciated.

I think I have the memory allocated correctly, but the copy/cat doesn't like something.

Once I finish these parts, I'll then need to pass the created array to a function void DisplayName(char * Name) which will display the full name.

Does this look right so far?
Updated Code:

//=============================================================================
// Program Requirements
// 1. Prompt User for Last, 1st, and Middle Name
// 2. Place each entry into separate fixed size array
// 3. Dynamically allocate an array to store all name parts.
// 4. Copy name parts in order into new array.
// Note: Be sure to have a space between names.
// 5. Pass new array into function to display name
// 6. Deallocate the array.
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************

#include <iostream>		// for cin,cout
#include <cstring>     // for string manipulation
 
using namespace std;
// =============================================================================

int main()
{
		  // declare local constant(s), variable(s), and array(s)
	char LName[32];								// fixed size array
	char FName[16];								// fixed size array
	char MName[16];								// fixed size array
	int go;										// input to start
	char *namePtr;  							// pointer
	char fullname;								// new array for full name
	char n;										// size needed for new array
	
 
		// Start program
	cout << "Enter 1 to Continue 0 to Quit" << endl;		
	cin >> go;
	cin.ignore();								// clear input buffer

		// Prompt User for Last, 1st, and Middle Name
	 while (go > 0)
	 {
		cout << "\n\nEnter Last Name\n\n";
		cin.getline ( LName, 32, '\n');			// set 1st array
		cout << "\n\nEnter First Name\n\n";		
		cin.getline ( FName, 16, '\n');			// set 2nd array
		cout << "\n\nEnter Middle Name\n\n";
		cin.getline ( MName, 16, '\n');			// set 3rd array

		//Dynamically allocate an array to store all name parts.
												// determine size of new array
		namePtr = new char [strlen( FName )+ strlen ( MName )+ strlen ( LName )+1];

		
		// Copy name parts in order into new array.
		strcpy (fullname, FName);
		strcat (fullname, MName);
		strcat (fullname, LName);

                // Function to Display Name
                // To Be Completed

		// De-allocate array
		delete []namePtr;							// free memory pointed to by name


		// Option to Continue
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }

	return 0;
}
Member Avatar for embooglement

This entire piece of code:

name = new char[n];			// Allocate n array and save ptr in name
for (int i =0; i<n; i++)
{
	name[i] = 0;			// all elements set to zero
}
delete []name;				// free memory pointed to by name
name = NULL;				// clear buffer

Creates an array of characters, sets each character to '\0', and then deletes all of it. That means when you do "strcpy(name, LName);", you don't have any space allocated to push LName into. You should delete your dynamically allocated memory at the end of the program, at least in this case.

This entire piece of code:

name = new char[n];			// Allocate n array and save ptr in name
for (int i =0; i<n; i++)
{
	name[i] = 0;			// all elements set to zero
}
delete []name;				// free memory pointed to by name
name = NULL;				// clear buffer

Creates an array of characters, sets each character to '\0', and then deletes all of it. That means when you do "strcpy(name, LName);", you don't have any space allocated to push LName into. You should delete your dynamically allocated memory at the end of the program, at least in this case.

*nods* Got it. Thank you.

This is looking much better. Need to get the spaces into the string and then pass them into the DisplayName function, and I'll be done.

Here's my improved code (thanks so much everyone)

//=============================================================================
// Program Requirements
// 1. Prompt User for Last, 1st, and Middle Name
// 2. Place each entry into separate fixed size array
// 3. Dynamically allocate an array to store all name parts.
// 4. Copy name parts in order into new array.
// Note: Be sure to have a space between names.
// 5. Pass new array into function to display name
// 6. Deallocate the array.
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************

#include <iostream>					// for cin,cout
#include <cstring>					// for string manipulation
 
using namespace std;
// =============================================================================

int main()
{
		  // declare local constant(s), variable(s), and array(s)
	char LName[32];					// fixed size array
	char FName[16];					// fixed size array
	char MName[16];					// fixed size array
	int go;						// input to start
	char *namePtr;          		        // pointer
	char fullname;					// new array for full name


	
		// Start program
	cout << "Enter 1 to Continue 0 to Quit" << endl;		
	cin >> go;
	cin.ignore();					// clear input buffer

		// Prompt User for Last, 1st, and Middle Name
	 while (go > 0)
	 {
		cout << "\n\nEnter Last Name\n\n";
		cin.getline ( LName, 32, '\n');			// set 1st array
		cout << "\n\nEnter First Name\n\n";		
		cin.getline ( FName, 16, '\n');			// set 2nd array
		cout << "\n\nEnter Middle Name\n\n";
		cin.getline ( MName, 16, '\n');			// set 3rd array

		//Dynamically allocate an array to store all name parts.
												// determine size of new array
		namePtr = new char [strlen( FName )+ strlen ( MName )+ strlen ( LName )+1];

		
		// Copy name parts in order into new array.
		strcpy (namePtr, FName);
		strcat (namePtr, MName);
		strcat (namePtr, LName);

		// Function to Display Name
		cout<<"Hello "<< namePtr <<endl;		// not a function but it'll do for now

		// De-allocate array
		delete []namePtr;						// free memory pointed to by name


		// Option to Continue
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }

		
 
	return 0;
}

/*
OUTPUT
Enter 1 to Continue 0 to Quit
1


Enter Last Name

Smith


Enter First Name

Elizabeth


Enter Middle Name

Jean
Hello ElizabethJeanSmith
Enter 1 to Continue 0 to Quit
*/
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.