-1

I recently learnt the cstring arrays and wanted to try out a basic operation of adding two strings together, in a normal string using as header you can add string1+string2=string3. But I tried doing this for cstrings and I get an error when I follow this format which is probably not right. Here is the code, the code is just for printing my first and last name as 1 name in a string.

#include<iostream>
#include<cstring>


using namespace std;

 int main() {
 char fname[100], lname[100], full_name[100];
 int i, j;
 i = 0;j = 0; // i is index of fname and j is index for lname
 cout << "Enter your first name: ";
 cin.getline(fname, 100);
 cout << "Enter your last name: ";
 cin.getline(lname, 100);
 for (i;fname[i] != '\0';i++) {
    full_name[i] = fname[i];
  }
  std::string(fname +" "+ lname);
  cout << "i =" << i;
  full_name[i] = ' ';
  i = i + 1;
  for (i, j;lname[j] != '\0';i++, j++) {
    full_name[i] = lname[j];
    }
    cout << "Your full name is: " << full_name << endl;
    system("pause");
    return 0;
}
Jos
  • 65
  • 7
  • 1
    `std::string(fname +" "+ lname);` -> `std::string(fname) +" "+ lname;` ? – MikeCAT May 06 '20 at 12:20
  • You forgot to null-terminate `full_name`. – MikeCAT May 06 '20 at 12:22
  • `strcpy()` and `strcat()` (or safer `strncpy()` and `strncat()`) are usefult to manipulate cstrings. – MikeCAT May 06 '20 at 12:25
  • How do I null-terminate a string? – Jos May 06 '20 at 12:26
  • By adding `'\0'` to the end of string: add `full_name[i] = '\0';` before `cout << "Your full name is: " << full_name << endl;` in this case. – MikeCAT May 06 '20 at 12:27
  • Ok I removed the statment and added the null-termination – Jos May 06 '20 at 12:30
  • If you're using C++ there is almost no reason to do any string work with old C style string arrays. The old `strcpy`, `strcat` functions, etc, are dangerous and obsolete. – Zan Lynx May 06 '20 at 12:34
  • Sir, Why are they dangerous? often classified as illegal sometimes... – Jos May 06 '20 at 12:37
  • @JohnnyJoestar Because none of the C string functions know the size of the buffers they are working with. It is incredibly easy to write string code that allocates the wrong size buffer, copies characters past the buffer end, or randomly truncates strings without warning. The C++ string may not be perfect, but it is loads better than C's. – Zan Lynx May 09 '20 at 06:23

1 Answers1

1

First remove this line from code

std::string(fname +" "+ lname);

Second you forgot to add '\0' after you end the string take a look at this code:

int main() {
    char fname[100], lname[100], full_name[100];
    int i, j;
    i = 0; j = 0; // i is index of fname and j is index for lname
    cout << "Enter your first name: ";
    cin.getline(fname, 100);
    cout << "Enter your last name: ";
    cin.getline(lname, 100);
    for (i; fname[i] != '\0'; i++) {
        full_name[i] = fname[i];
    }

    cout << "i =" << i;
    full_name[i] = ' ';
    i = i + 1;
    for (i, j; lname[j] != '\0'; i++, j++) {
        full_name[i] = lname[j];
    }
    full_name[i] = '\0';
    cout << "Your full name is: " << full_name << endl;
    system("pause");
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
dt170
  • 417
  • 2
  • 12
  • Alright your answer fixed the code, thank you and thank you @MikeCAT but do you mind explaining why adding full_name[i]='\0'; make a huge difference? Shouldn't the null already be declared within the string? – Jos May 06 '20 at 12:35
  • The initial contents of local `char full_name[100];` is indeterinate. It may be zero, but it is too dangerous to bet to that. Another way is to initialize the array to zero by wrinting `char full_name[100] = "";` – MikeCAT May 06 '20 at 12:40
  • @JohnnyJoestar `adding full_name[i]='\0'` marks the end of string. for detailed answer take a look here: https://stackoverflow.com/questions/40821419/when-why-is-0-necessary-to-mark-end-of-an-char-array – dt170 May 06 '20 at 13:32
  • Thank you both for your answers, I cleared the confusion now, thank you again. – Jos May 06 '20 at 16:29