Question

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 ?

Was it helpful?

Solution

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).

OTHER TIPS

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.

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);

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

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;
}

The problem is with your space text.

The strcat function requires a C-style string, which is zero or more characters followed by a null, terminating, character. So when allocating arrays for C-style strings, you need to allocate one extra character for the terminating null character.

So, your space array needs to be of length 2, one for the space character and one for the null character.

Since space is constant, you can use a string literal instead of an array:

const char space[] = " ";

Also, since you are a newbie, here are some tips:
1. Declare one variable per line.
This will be easier to modify and change variable types.

2. Either flush std::cout, use std::endl, or include a '\n'.
This will flush the buffers and display any remaining text.

3. Read the C++ language FAQ.
Click here for the C++ language Frequently Asked Questions (FAQ)

4. You can avoid C-style string problems by using std::string


5. Invest in Scott Myers Effective C++ and More Effective C++ books.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top