A
arnuld
i have this code. it works but its behaviour is strange:
------------------------------------------------------------
#include <iostream>
#include <string>
int main() {
std::string s1;
std::string s2;
std::cout << "Please enter your name: ";
std::cin >> s1;
std::cout << "hello " << s1 << "\n";
std::cout << "now using GETLINE(): ";
getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}
OUTPUT (with one word as input):
[arnuld@localhost cpp]$ ./a.out
Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)
OUTPUT (with 2 words as input):
Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$
-----------------------------------------------------------------
i expected:
1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.
2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.
but behaviour is not like this. can somebody explain?
i am using "g++ version 4.1.1" on "BLAG Linux 50002".
thanks
-- arnuld
http://arnuld.blogspot.com
------------------------------------------------------------
#include <iostream>
#include <string>
int main() {
std::string s1;
std::string s2;
std::cout << "Please enter your name: ";
std::cin >> s1;
std::cout << "hello " << s1 << "\n";
std::cout << "now using GETLINE(): ";
getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}
OUTPUT (with one word as input):
[arnuld@localhost cpp]$ ./a.out
Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)
OUTPUT (with 2 words as input):
Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$
-----------------------------------------------------------------
i expected:
1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.
2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.
but behaviour is not like this. can somebody explain?
i am using "g++ version 4.1.1" on "BLAG Linux 50002".
thanks
-- arnuld
http://arnuld.blogspot.com