1

In the following code, I'm getting only string a as output on my mingw compiler but I get both strings as output on an online compiler. Why am I not getting appropriate output on my system?

#include<iostream>
#include<cstring>

using namespace std;

int main(){
    string a="Welcome";
    string b="HelloWorld";
    for(int i=0;i<=b.length();i++){
        a.push_back(b[i]);
        
    }
    cout<<a<<endl<<b;

     

    return 0;   
}    

Output- WelcomeHelloWorld

Nityunj
  • 11
  • 3

2 Answers2

2

one thing you can do is, you can use cout.flush(). If it does not work you can do a+b or you can use substr()

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    cout.flush();
    string a="Welcome";
    string b="HelloWorld";
    cout<<a+b<<endl<<b;
    // cout<<a+b.substr(0,9);
    return 0;   
}  
1

I think cout.flush() sometimes help when getting unwanted output. So, once you can try this also before printing the result.