Strange characters appear when using strcat function in C++ -


I'm a newbie for C ++ and learning from the MSDN C ++ Beginner's Guide.

This works but I get three funny characters in the beginning.

This is my code

  #include & lt; Iostream & gt; # Include & lt; Cstdio & gt; # Include & lt; Cstring & gt; using namespace std; Int main () {char first_name [40], lastname [40], fullname [80], location [1]; Location [0] = ''; Cout & lt; & Lt; "enter your first name: "; Gets (first_name); Cout & lt; & Lt; "Enter your last name:"; Gets (last_name); Strcat (FULL_NAME, first_name); Strcat (FULL_NAME, space); Strcat (FULL_NAME, last_name); Cout & lt; & Lt; "Your name is:" & lt; & Lt; Full Name; Return 0; }  

And here is the output

  Enter your first name: Enter your last name: Above your name is: Y} @ Tehair Auzid < Code>  

I wonder why Y} @ appears before my name?

The array that you are creating is filled with random data. C ++ will allocate space for data but will not start the array with known data. The data will be attached at the end of the strat string (first '\ 0') because the array of characters has not been started (and it is filled with random data) it will not be the first letter.

It can be corrected by changing firstname [40], last_name [40], fullname [80], location [1]; With

  char first_name [40] = {0}; Four last names [40] = {0}; Char full_name [80] = {0}; Four space [2] = {0};  

= {0} will set the first element to '\ 0' which is a string terminator emblem, and C ++ is automatically all non-specific Fill the elements '\ 0' (provided that at least one element is specified).


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -