Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

a simple question

R.Neeser
P: n/a
R.Neeser
Hello,

how do i get an keyboard input from the consol, WITH THE Space char?
scnaf and all the other function divide the input string on such a char and
give every part to a different variable. but i don't need that.

char buf[2000];
scanf("%s",char);
printf("%s",buf);

if i type "ha ha", it prints back "ha", but i want "ha ha".

Can please somebody help me
Jul 19 '05 #1

9 Replies



Ali R.
P: n/a
Ali R.

re: a simple question

you can either use gets() or the cin object.

Ali R.

"R.Neeser" <rneeser@gmx.ch> wrote in message
news:3fa94a02_1@news.tiscalinet.ch...[color=blue]
> Hello,
>
> how do i get an keyboard input from the consol, WITH THE Space char?
> scnaf and all the other function divide the input string on such a char[/color]
and[color=blue]
> give every part to a different variable. but i don't need that.
>
> char buf[2000];
> scanf("%s",char);
> printf("%s",buf);
>
> if i type "ha ha", it prints back "ha", but i want "ha ha".
>
> Can please somebody help me[/color]


Jul 19 '05 #2

Victor Bazarov
P: n/a
Victor Bazarov

re: a simple question

"R.Neeser" <rneeser@gmx.ch> wrote...[color=blue]
> how do i get an keyboard input from the consol, WITH THE Space char?
> scnaf and all the other function divide the input string on such a char[/color]
and[color=blue]
> give every part to a different variable. but i don't need that.
>
> char buf[2000];
> scanf("%s",char);
> printf("%s",buf);
>
> if i type "ha ha", it prints back "ha", but i want "ha ha".[/color]

Try using 'getline' member function:

char buf[2000] = {0};
std::cin.getline(buf, 1999);

Victor



Jul 19 '05 #3

R.Neeser
P: n/a
R.Neeser

re: a simple question

> Try using 'getline' member function:[color=blue]
>
> char buf[2000] = {0};
> std::cin.getline(buf, 1999);
>
> Victor[/color]

Thank you, works very well
Jul 19 '05 #4

Jack Klein
P: n/a
Jack Klein

re: a simple question

On Wed, 05 Nov 2003 19:15:00 GMT, "Ali R." <nospam@company.com> wrote
in comp.lang.c++:

1. Don't top post.
[color=blue]
> you can either use gets() or the cin object.[/color]

2. Don't post dangerous rubbish. If you do not know enough not to
recommend or ever use gets(), the single most dangerous function in
the entire C or C++ standard library, you are totally unqualified to
give advice to anyone about programming in either of those languages.

3. Don't top post.
[color=blue]
>
> Ali R.
>
> "R.Neeser" <rneeser@gmx.ch> wrote in message
> news:3fa94a02_1@news.tiscalinet.ch...[color=green]
> > Hello,
> >
> > how do i get an keyboard input from the consol, WITH THE Space char?
> > scnaf and all the other function divide the input string on such a char[/color]
> and[color=green]
> > give every part to a different variable. but i don't need that.
> >
> > char buf[2000];
> > scanf("%s",char);
> > printf("%s",buf);
> >
> > if i type "ha ha", it prints back "ha", but i want "ha ha".
> >
> > Can please somebody help me[/color]
>[/color]

Jul 19 '05 #5

Ali R.
P: n/a
Ali R.

re: a simple question

First of all I didn't top post!!! Sue me if my server takes a while to post
the message. You are so fucking rude.
What's wrong with gets(). How is gets any more dangarous then the scanf that
the guy was using? You can't pass a buffer size to scanf either. If the
question doesn't involve c++ why confuse the guy with a c++ class. If you
have noticed I said "or use the cin object"! If he knows what cin is then he
would use it if he doesn't then he will ask what the heck is cin and then I
would explane how to use it and how to include it!
I have 13 years of C++, and I teach C++.

Ali R.

"Jack Klein" <jackklein@spamcop.net> wrote in message
news:m9djqvcad6t4rlvbeib6bsrld17egoom5i@4ax.com...[color=blue]
> On Wed, 05 Nov 2003 19:15:00 GMT, "Ali R." <nospam@company.com> wrote
> in comp.lang.c++:
>
> 1. Don't top post.
>[color=green]
> > you can either use gets() or the cin object.[/color]
>
> 2. Don't post dangerous rubbish. If you do not know enough not to
> recommend or ever use gets(), the single most dangerous function in
> the entire C or C++ standard library, you are totally unqualified to
> give advice to anyone about programming in either of those languages.
>
> 3. Don't top post.
>[color=green]
> >
> > Ali R.
> >
> > "R.Neeser" <rneeser@gmx.ch> wrote in message
> > news:3fa94a02_1@news.tiscalinet.ch...[color=darkred]
> > > Hello,
> > >
> > > how do i get an keyboard input from the consol, WITH THE Space char?
> > > scnaf and all the other function divide the input string on such a[/color][/color][/color]
char[color=blue][color=green]
> > and[color=darkred]
> > > give every part to a different variable. but i don't need that.
> > >
> > > char buf[2000];
> > > scanf("%s",char);
> > > printf("%s",buf);
> > >
> > > if i type "ha ha", it prints back "ha", but i want "ha ha".
> > >
> > > Can please somebody help me[/color]
> >[/color]
>[/color]


Jul 19 '05 #6

Karl Heinz Buchegger
P: n/a
Karl Heinz Buchegger

re: a simple question



"Ali R." wrote:[color=blue]
>
> First of all I didn't top post!!![/color]

You did it again!
Seems like you don't know what top posting is.

Top posting means: you put your reply at the top of the thing
you are replying to.

Please don't do it. Put your reply beneath the text you are replying
to, if necc. embedd your reply in the text you are replying to, just
as I am doing it now.
[color=blue]
> Sue me if my server takes a while to post
> the message. You are so fucking rude.[/color]

This has nothing to do with top posting.
[color=blue]
> What's wrong with gets().[/color]

There is no way to use it in a safe manner.
Look at the arguments to gets(). You have no way to tell gets()
how large the buffer is, you are passing to gets(). Eg. you
pass gets() a buffer which can hold 20 characters, but your user
enters 40 characters. Kaboom.

fget() on the other hand doesn't have that problem.
[color=blue]
> How is gets any more dangarous then the scanf that
> the guy was using? You can't pass a buffer size to scanf either.[/color]

Right

If you want to recommend something better then gets or scanf, then start
with a combination of fgets() and sscanf(). Since the OP wanted to read
an enitre line of input, fgets() alone would be the choice.
[color=blue]
> If the
> question doesn't involve c++ why confuse the guy with a c++ class. If you
> have noticed I said "or use the cin object"! If he knows what cin is then he
> would use[/color]

How?
What the user wanted to do with cin, is call the function getline. Either
the member function version or (in combination with std::string) the
standalone version. In any way, just mentioning 'cin' hasn't helped him
in any way. Just like saying: Use stdin will not help him in any way
if he is programming in C.
[color=blue]
> it if he doesn't then he will ask what the heck is cin and then I
> would explane how to use it and how to include it!
> I have 13 years of C++, and I teach C++.[/color]

But then you shouldn't recommend gets(). Never!

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Jul 19 '05 #7

Karl Heinz Buchegger
P: n/a
Karl Heinz Buchegger

re: a simple question



Karl Heinz Buchegger wrote:[color=blue]
>[/color]

[snip]
[color=blue][color=green]
> > What's wrong with gets().[/color]
>
> There is no way to use it in a safe manner.
> Look at the arguments to gets(). You have no way to tell gets()
> how large the buffer is, you are passing to gets(). Eg. you
> pass gets() a buffer which can hold 20 characters, but your user
> enters 40 characters. Kaboom.
>
> fget() on the other hand doesn't have that problem.[/color]

obvious typo, must read: fgets() on the other ...

[snip]

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Jul 19 '05 #8

Jerry Coffin
P: n/a
Jerry Coffin

re: a simple question

In article <Tyuqb.2036$293.287@newssvr22.news.prodigy.com>,
nospam@company.com says...[color=blue]
> First of all I didn't top post!!![/color]

Yes, you did. Top-posting is when you put your reply before what you're
following up to. When your text comes before that quoted text of the
post to which you're following up, that's top-posting.

[ ... ]
[color=blue]
> What's wrong with gets().[/color]

It's dangerous because you can't limit the amount of text it will read
in.
[color=blue]
> How is gets any more dangarous then the scanf that
> the guy was using? You can't pass a buffer size to scanf either.[/color]

Quite the contrary -- you most certainly CAN pass a buffer size of
scanf. A typical, reasonably safe use of scanf would look something
like this:

char buffer[50];

scanf("%49[^\n] ", buffer);

Of course, you can use similar capabilities (including the buffer-size
limit) with fscanf, sscanf, etc.). I'd also note that while the use of
scanf above is quite safe, it's possible that its effects will confuse
the user -- depending on the situation, something like this:

scanf("%49[^\n]%*c", buffer);

might be preferable.
[color=blue]
> If the
> question doesn't involve c++ why confuse the guy with a c++ class.[/color]

If it had been specified that the desired language was C, that would
rule out use of cin. I see no such limitation in the subject or
original post. In any case, you were the one who originally brought up
the possibility of using cin.
[color=blue]
> If you
> have noticed I said "or use the cin object"! If he knows what cin is then he
> would use it if he doesn't then he will ask what the heck is cin and then I
> would explane how to use it and how to include it![/color]

Saying "you can use the cin object" is NOT really an answer to the
original question at all. Use of cin vs. stdin is orthogonal to the
original question.
[color=blue]
> I have 13 years of C++, and I teach C++.[/color]

What exactly does "I have 13 years of C++" mean?

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9

Jerry Coffin
P: n/a
Jerry Coffin

re: a simple question

In article <3fa94a02_1@news.tiscalinet.ch>, rneeser@gmx.ch says...[color=blue]
> Hello,
>
> how do i get an keyboard input from the consol, WITH THE Space char?
> scnaf and all the other function divide the input string on such a char[/color]

If you use scanf with the %s conversion, THEN it splits input at
whitespace, but if you use it with %[^\n], it will read up to a newline
in the input. Alternatively, you might want to use fgets, which is
tailored specifically for reading complete lines of text.

Note that if you use fgets to read a line of text, the newline that
terminates the input line will normally be retained in the string that's
read in (if it's not there, it means the line contained more text than
the maximum you told fgets to read).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #10

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your C / C++ question on Bytes

You can also browse similar questions: C / C++

Get C / C++ Help

Get C / C++ help from a network of professionals.

Post your Question » Over 331,212 Members | 3378 Online