Hi.
It worked okay for me:
agw@homeesktop$ foo
Teste jetzt die Eingabe. Bitte Zahl eingeben: 80
Jetzt Text eingeben:
you have typed in: 80
--
Art Werschulz (agw STRUDEL comcast.net)
207 Stoughton Ave Cranford NJ 07016
(908) 272-1146
This is a discussion on reading text from cin fails after reading numbers. - Linux ; Hello, I want to use the library iostream with cin to read from standard input. Look at the followin code: #include using namespace std; int main(){ cout uint number; cin>>number; cout char text[81]; // cin.seekg(0L,std::ios::end); // cin.clear(); cin.getline(text,80); cout return ...
Hello,
I want to use the library iostream with cin to read from standard input.
Look at the followin code:
#include
using namespace std;
int main(){
cout<<"Teste jetzt die Eingabe. Bitte Zahl eingeben: ";
uint number;
cin>>number;
cout<<"Jetzt Text eingeben: ";
char text[81];
// cin.seekg(0L,std::ios::end);
// cin.clear();
cin.getline(text,80);
cout<<"\n you have typed in: "<return 0;
}
It doesn't work neither with nor without the //-lines. What is wrong?
Hi.
It worked okay for me:
agw@homeesktop$ foo
Teste jetzt die Eingabe. Bitte Zahl eingeben: 80
Jetzt Text eingeben:
you have typed in: 80
--
Art Werschulz (agw STRUDEL comcast.net)
207 Stoughton Ave Cranford NJ 07016
(908) 272-1146
Art Werschulz schrieb:
> Hi.
>
> It worked okay for me:
>
> agw@homeesktop$ foo
> Teste jetzt die Eingabe. Bitte Zahl eingeben: 80
> Jetzt Text eingeben:
> you have typed in: 80
>
or may be he wanted to input own text. Then he should eat the eol from
last input, like:
// cin.getline(text,80);
string s;
char eol;
std::cin.get(eol);
getline(cin,s);
cout<<"\n you have typed in: "<
Teste jetzt die Eingabe. Bitte Zahl eingeben: 123
Jetzt Text eingeben: hey you
you have typed in: 123hey you
Uwe Bosse wrote:
> I want to use the library iostream with cin to read from standard input.
As far as C++ is concerned, it is not a library but rather a builtin part of
C++.
> int main(){
> cout<<"Teste jetzt die Eingabe. Bitte Zahl eingeben: ";
> uint number;
What is 'uint'?
> cin>>number;
> cout<<"Jetzt Text eingeben: ";
> char text[81];
> cin.getline(text,80);
No, get rid of such strings.
#include
....
std::string text;
getline( std::cin, text);
> What is wrong?
You shouldn't mix line-based input with field-based input. Rather, read
std::cin linewise and afterwards parse it (using e.g. std::stringstream).
Alternatively, you can ignore spaces, newlines and tabs using std::ws or
discard input using std::istream's ignore() function, in order to skip the
newline that is still in the input buffer after extracting the number.
Uli