0

I'm trying to concatenate 2 strings. One of the string is defined whereas the other string's length is not fixed. Whenever I input the second string as suppose 'to my world', it does not print the entire string on concatenation. I'm new to programming, so please help me out.

#include <iostream>
using namespace std;

int main() {
    string s = "Welcome";
    string t="",k;
    cin>>t;
    k=s+t;
    cout<<k;
return 0; }
S Mahajan
  • 113
  • 2
  • 15
  • I do not intend to use string header file – S Mahajan Jun 27 '16 at 19:23
  • What's your concrete problem? Not including but using type string? Cannot make much sense of all that. – Eiko Jun 27 '16 at 20:03
  • Basically I want to print suppose 'Welcome to hello world' without using string header file. So when I compile my program, i'll input string t as 'to hello world'. But when it diplays the final string it displays it as just 'Welcometo' instead of 'Welcome to hello world'. – S Mahajan Jun 28 '16 at 10:10
  • What is so wrong with including ? Is this a homework requirement? This question is tagged c++, and I'd consider string to be one of its key features. Of course you could fall back to the awkward c style with scanf and printf, but why c++ then? – Eiko Jun 28 '16 at 10:21

1 Answers1

0

Use std::getline instead of the operator >> that allows to enter only one word delimited by a white-space character. For example

#include <iostream>
#include <string>

int main() 
{
    std::string s = "Welcome";
    std::string t, k;

    std::getline( std::cin, t );
    k = s + ' ' + t;

    std::cout << k << std::endl;

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Sorry but I intend to do it without the usage of string header file. So I believe std::getline could not be used without string header file. – S Mahajan Jun 27 '16 at 18:59
  • @SMahajan If you are using standard class std::string then in any case you should include header – Vlad from Moscow Jun 28 '16 at 12:51
  • But when I compiled in Dev-C++. It ran without header – S Mahajan Jul 03 '16 at 17:39
  • @SMahajan It is implementation define whether some other header includes header . If you want that your program compiles using any compiler you should explicitly include the header. Moreover it is selfdocumented and users of the program will know that it uses class std::string. – Vlad from Moscow Jul 04 '16 at 11:44