syed shah
syed shah

Reputation: 678

getting error on run time when i give space between words

I am storing words on run time in array but when i give space between words the program don't ask for second input it give me an output directly without taking second input here is my coding .

#include<iostream>
#include<conio.h>

using namespace std;
int main(){
char a[50];
char b[50];
cout<<"please tell us what is your language\t";
cin>>a;
cout<<"please tell us what is your language\t";
cin>>b;
cout<<a<<b;
getch();
}

here is my output

Upvotes: 2

Views: 1389

Answers (1)

dyp
dyp

Reputation: 39101

#include<iostream>
//#include<conio.h>    // better don't use this, it's not portable
#include <string>

//using namespace std; // moving this inside the function
int main(){
    using namespace std;  // a bit more appropriate here

    string a;
    string b;

    cout<<"please tell us what is your language\t";
    getline(cin, a);  // `a` will automatically grow to fit the input
    cout<<"please tell us what is your language\t";
    getline(cin, b);
    cout<<a<<b;

    //getch();            // not portable, from conio.h
    // alternative to getch:
    cin.ignore();
}

A reference for std::getline (with an example at the bottom) and std::string.

Upvotes: 4

Related Questions