-1
#include <iostream>
#include <cstring> //which one should I use...out of these three header files
#include <string>  //whats difference in each of them
#include <string.h>
int main()
{
    std::string first_name;
    std::string second_name;

    std::cout << "\n First name : ";
    std::cin >> first_name;

    std::cout << "\n Second name : ";
    std::cin >> second_name;

    strcat(first_name," ");         //not working
    strcat(first_name,second_name); //not working

    std::cout << first_name;
    return 0;
}

I've done programs on c++ strcat (string catenation) earlier. I followed new tutorials and the new IDE compilers(which contains new data type viz. 'string' ). When I tried the same using this, then it is giving me errors.

ERROR:- ||=== Build: Debug in string1 (compiler: GNU GCC Compiler) ===| C:\Users\Admin\Desktop\c++ projects\string1\main.cpp||In function 'int main()':|
C:\Users\Admin\Desktop\c++ projects\string1\main.cpp|16|error: cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'char* strcat(char*, const char*)'|
C:\Users\Admin\Desktop\c++ projects\string1\main.cpp|17|error: cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'char* strcat(char*, const char*)'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Nitish Prajapati
  • 139
  • 1
  • 11

1 Answers1

1

strcat is the old-style of working with string(with char* of course I mean).

Now just #include<string> and use std::string easily:

std::string name = "Foo";
std::string lastName = "Bar";
std::string fullname = name+" "+lastName;
std::cout << fullname ; // <- "Foo Bar"

More: (@michael-krelin-hacker)

<string> and <string.h> are two different headers:

  • <string> is for c++ std::string class
  • <string.h> is for c string functions (like strlen(), etc.), which should be <cstring> for c++ project (this is the third, you didn't know of).

More2: If you prefer to use C style, try this:

std::string name = "Foo";
std::string lastName = "Bar";
///
int len = name.length();
char* fullname = new char[len+1];
strncpy(fullname, name.c_str(), name.length());
fullname[len]='\0';
///
strncat(fullname," ", 1);
strncat(fullname,lastName.c_str(), lastName.length());
///
std::cout<<fullname;
Emadpres
  • 3,466
  • 2
  • 29
  • 44
  • I think one of his main confusion is that c++ string is a different thing entirely from null-terminating C-style string – bolov Jan 11 '15 at 07:45
  • @Emadpres so you mean that ' ' is also old-style of using strings? And what does ' std::string ' class do (I mean how can ' std::string ' be a **class** if it is a **data-type** ).Confusing :/ – Nitish Prajapati Jan 11 '15 at 07:52
  • if is old concept ,so please provide me with those functions(of the new header file ) to do those functions such as strlen() strcpy() and many more..... – Nitish Prajapati Jan 11 '15 at 07:55
  • @bolov please provide more detail – Nitish Prajapati Jan 11 '15 at 07:58
  • 3
    @NitishPrajapati it looks like you need to start a [C++ basic tutorial](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – bolov Jan 11 '15 at 08:04