Do anyone here know how to fix this without using loop ? There is this syntax er
ID: 3770065 • Letter: D
Question
Do anyone here know how to fix this without using loop ? There is this syntax error
Error 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
I just want to get result like this Last name, First name Middle name
Thanks
# include <iostream>
using namespace std;
int main()
{
const int SIZE = 30;
char last[SIZE];
char first[SIZE];
char middle[SIZE];
char full[SIZE * 3];
cout << "Please enter your last name no more than 30 characters: " << endl;
cin.getline(last, SIZE-1);
cout << "Please enter your middle name: " << endl;
cin.getline(middle, SIZE-1);
cout << "Please enter your first name: " << endl;
cin.getline(first, SIZE-1);
strcpy(full, last);
strcat(full, ", ");
strcat(full, first);
strcat(full, " ");
strcat(full, middle);
cout << " YOur last name , first name and middle name is " << endl;
cout << full << endl;
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
void Display (char * );
void main ()
{
const int SIZE = 30;
char Last [SIZE]; //Array for last name
char First [SIZE]; //Array for first name
char Middle [SIZE]; //Array for middle name
int FNLength, MNLength, LNLength, Length; // properties
int LoopControl=1; //Loop control variable to terminat loop
do
{
cout << "Please enter your last name: ";
cin.ignore(cin.rdbuf()->in_avail(),' ');
cin.getline(Last , SIZE, ' ');
cout << "Please enter your first name: ";
cin.getline(First , SIZE, ' ');
cout << "Please enter your middle name: ";
cin.getline(Middle , SIZE, ' ');
FNLength = strlen(First );
MNLength = strlen(Middle );
LNLength = strlen(Last );
Length = FNLength + MNLength + LNLength + 3;
char *full = new char[ Length];
if (full == 0)
{
cout << "DMA not allocated!" << endl;
exit(1); //Terminate program
}
strcpy(full , First ); //Copy First to full
strcat(full , " "); //Add Space to full
strcat(full , Middle ); //Add Middle to full
strcat(full , " "); //Add Space to full
strcat(full , Last ); //Add Last and NULL to full
Display (full ); //Run Display function to display full array
delete [] full ; //Free up memory
//Prompt user to repeat program or exit
cout << endl;
cout << "Enter in 1 to quit." << endl;
cout << "Enter in any other number to run again." << endl;
cout << "> ";
cin >> LoopControl;
}
//If the user enters 1 the program exits, otherwise the program repeats
while ( LoopControl != 1 );
}
void Display (char * )
{
//Display text with array contents
cout << " YOur last name , first name and middle name is " << endl;
cout << full << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.