a simple question

R

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
 
V

Victor Bazarov

R.Neeser said:
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".

Try using 'getline' member function:

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

Victor
 
J

Jack Klein

in comp.lang.c++:

1. Don't top post.
you can either use gets() or the cin object.

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.
Ali R.

R.Neeser said:
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
 
A

Ali R.

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 said:
in comp.lang.c++:

1. Don't top post.
you can either use gets() or the cin object.

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.
Ali R.

R.Neeser said:
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
 
K

Karl Heinz Buchegger

Ali R. said:
First of all I didn't top post!!!

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.
Sue me if my server takes a while to post
the message. You are so fucking rude.

This has nothing to do with top posting.
What's wrong with gets().

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.
How is gets any more dangarous then the scanf that
the guy was using? You can't pass a buffer size to scanf either.

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.
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

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.
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++.

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

Karl Heinz Buchegger

Karl said:
[snip]
What's wrong with gets().

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.

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

[snip]
 
J

Jerry Coffin

First of all I didn't top post!!!

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.

[ ... ]
What's wrong with gets().

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

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.
If the
question doesn't involve c++ why confuse the guy with a c++ class.

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.
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!

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.
I have 13 years of C++, and I teach C++.

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

Jerry Coffin

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

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top