Question:
C++ problem -- segmentation fault?
2009-01-29 17:28:09 UTC
On my windows machine (with dev-c++), my output has weird characters before the string, and in linux, I just get a segmentation fault.

#include
#include
int main()
{
using namespace std;
char fname[25];
char lname[25];
char name[53];
cout << "Enter your first name: ";
cin >> fname;
cout << "Enter your last name: ";
cin >> lname;
strcat(name, lname);
strcat(name, ", ");
strcat(name, fname);
cout << "Here's the information in a single string: " << name;

}


I'm new to C++, this is a problem from a book called C++ Primer Plus that asked me to write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user's last name followed by a comma, a space, and a first name. Use char arrays and functions from the cstring header.

Not entirely sure where I went wrong. Any tips would be great.
Five answers:
videobobkart
2009-01-29 17:39:18 UTC
That first strcat needs to be a strcpy instead. You are asking it to put lname on the end of whatever is in name, which initially is garbage. If there is not a terminating null character in that garbage, the strcat will put lname outside the bounds of name (segmentation fault). If there is not a terminating null characters as the FIRST byte of that garbage, strcat will put lname after any garbage before the first terminating null character (garbage characters before the string). Bottom line, COPY lname to name, don't APPEND lname to name.



Those other two answers will not help with that problem (segfault).



EDIT: Jakko's solution will also work, by putting a terminating null character in the first byte of name so strcat does what strcpy would do anyway. But by using strcpy as I suggest, you eliminate the need to initialize (some of) the contents of name, which just gets overwritten by the first strcpy/strcat anyway.
Zeta1
2009-01-29 17:42:35 UTC
include

#include

int main()

{

using namespace std;

char fname[25];

char lname[25];

char name[53];

cout << "Enter your first name: ";

cin >> fname;

cout << "Enter your last name: ";

cin >> lname;



// you must initialize name to an empty string ...

name[0] = '\0';



strcat(name, lname);

strcat(name, ", ");

strcat(name, fname);

cout << "Here's the information in a single string: " << name;



}
2009-01-29 17:33:12 UTC
this line will go outside the main function: using namespace std;

like this



#include

#include

using namespace std;

int main()

{



char fname[25];

char lname[25];
?
2016-10-30 08:04:07 UTC
Segmentation faults are brought about by referencing memory outdoors your application. in all danger brought about by a some style of pointer. ensure you arent deleting information that have already been dereferenced and determine you arent pointing to someplace you shouldnt be. As for why you arent seeing the output. Is it pausing when you print it a approach or the different. it would desire to print yet you never see it using fact your application fails later on and it happens so quickly you never be conscious it print.
owsley's kid
2009-01-29 17:33:07 UTC
move the namespace line to right below the includes.

Put the following line before the last right bracket:

return 0;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...