Click here to Skip to main content
15,940,246 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a C++ program that declares four character arrays with following name.
First_Name of size M
Middle_Name of size N
Last_Name of size P
Full_Name of size M+N+P
After declaring memories read data into first three arrays. Copy data from three arrays into fourth array. Display the fourth array.  	
Exammple:
Enter First Name: Jamal
Enter Middle Name: Ahmad
Enter Last Name: Khan
Full Name is Jamal Ahmad Khan


What I have tried:

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
void main()
{
	int m, n, p,i;
	cout << "Size of arrays ";
	cin >> m >> n >> p;
	int k = m + n + p;
	char* a = new char[m];
	char* b = new char[n];
	char* c = new char[p];
	char* d = new char[k];
	cout << "Name = ";
	cin.getline(a,m );
	cin.getline(b, n);
	cin.getline(c, p);
	for ( i = 0; i < m; i++)
		d[i] = a[i];
Posted
Updated 4-Jul-21 23:59pm
Comments
Richard MacCutchan 5-Jul-21 5:13am    
You forgot to allow for the null character at the end of each string, and the spaces between each part of the full name. Also if you are supposed to be writing C++ code then use C++ library features such as std::string.
Greg Utas 5-Jul-21 6:38am    
I would prompt for the size of each array, and each name, separately. Not doing so assumes the user knows details of how the code works and what it expects. Once the user enters the first item in either list, it will appear as if the program isn't responding. That's because it still wants the second, and then the third, input, so it should request them explicitly.

And the question is bizarre because it says "Write a C++ program" but then specifies the use of arrays to hold strings. That is very C, not C++.

1 solution

The forth array is the complete name and I would a space (" ") between the parts. Use strcat.

The forth array has the length of m + n + k + 2 + 1 (2 Spaces and terminator)

Dont ferget to use delete when done.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900