vendredi 31 mars 2017

Mixing String and Numeric Input in c++

I came across this program listing in c++primer plus

// numstr.cpp -- following number input with line input
#include <iostream>
int main()
{
using namespace std;
cout << "What year was your house built?\n";
int year;
cin >> year;
cout << "What is its street address?\n";
char address[80];
cin.getline(address, 80);
cout << "Year built: " << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
return 0;
}

Due to the carriage return after first cin the cin.getline doesn't work but when I replace cin.getline with cin>>address; the address is input why is it happening ? Please also tell how the input and output buffer work with cin and cout. Thanks

Aucun commentaire:

Enregistrer un commentaire