tags:

views:

377

answers:

7

I am a newbie to C++ and learning from the MSDN C++ Beginner's Guide.

While trying the strcat function it works but I get three strange characters at the beginning.

Here is my code

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    char first_name[40],last_name[40],full_name[80],space[1];
    space[0] = ' ';
    cout << "Enter your first name: ";
    gets(first_name);
    cout << "Enter your last name: ";
    gets(last_name);
    strcat(full_name,first_name);
    strcat(full_name,space);
    strcat(full_name,last_name);
    cout << "Your name is: " << full_name;
    return 0;
}

And here is the output

Enter your first name: Taher
Enter your last name: Abouzeid
Your name is: Y}@Taher Abouzeid

I wonder why Y}@ appear before my name ?

+7  A: 

You aren't initializing full_name by setting the first character to '\0' so there are garbage characters in it and when you strcat you are adding your new data after the garbage characters.

John
+1  A: 

You can not see any problem in your test, but your space string is also not null-terminated after initializing its only character with ' '.

Timbo
+2  A: 

The variable full_name isn't being initialized before being appended to.

Change this:

strcat(full_name,first_name);

to this:

strcpy(full_name,first_name);
Spire
Also, `space` isn't null terminated.
Greg Hewgill
True. The `space` array needs to be defined with a size of 2 and initialized with the string `" "` instead of the char `' '`.
Spire
A: 

The array that you are creating is full of random data. C++ will allocate the space for the data but does not initialize the array with known data. The strcat will attach the data to the end of the string (the first '\0') as the array of characters has not been initialized (and is full of random data) this will not be the first character.

This could be corrected by replacing

char first_name[40],last_name[40],full_name[80],space[1];

with

char first_name[40] = {0};
char last_name[40] = {0};
char full_name[80] = {0};
char space[2] = {0};

the = {0} will set the first element to '\0' which is the string terminator symbol, and c++ will automatically fill all non specified elements with '\0' (provided that at least one element is specified).

Mekboy
That works with slight modification.I replaced char space; with char space[2] = {0}; and that works perfect.Change it in the answer please so that I can accept it.
ta.abouzeid
Updated as requested
Mekboy
A: 

As others have said, you must initialize the data, but have you ever thought about learning the standard c++ library? It is more intuitive sometimes, and probably more efficient.

With it would be:

string full_name=first_name+" "+last_name;

and you won't have to bother with terminating null characters. For a reference go to cplusplus

Oh and a full working example so you could understand better (from operator+=):

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string name ("John");
  string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  cout << name;
  return 0;
}
csiz
A: 
Thomas Matthews
A: 

Strings are null-terminated in C and C++ (the strcat function is a legacy of C). This means that when you point to a random memory address (new char[] variables point to a stack address with random content that does not get initialized), the compiler will interpret everything up to the first \0 (null) character as a string (and will go beyond the allocated size if you use pointer arithmetic).

This can lead to very obscure bugs, security issues (buffer overflow exploits) and very unreadable and unmaintainable code. Modern compilers have features that can help with the detection of such issues.

Here is a good summary of your options.

cdonner