473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

a simple question

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 3392
you can either use gets() or the cin object.

Ali R.

"R.Neeser" <rn*****@gmx.ch> wrote in message
news:3f********@news.tiscalinet.ch...
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 #2
"R.Neeser" <rn*****@gmx.ch> wrote...
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

Jul 19 '05 #3
> Try using 'getline' member function:

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

Victor


Thank you, works very well
Jul 19 '05 #4
On Wed, 05 Nov 2003 19:15:00 GMT, "Ali R." <no****@company.com> wrote
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" <rn*****@gmx.ch> wrote in message
news:3f********@news.tiscalinet.ch...
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 #5
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" <ja*******@spamcop.net> wrote in message
news:m9********************************@4ax.com...
On Wed, 05 Nov 2003 19:15:00 GMT, "Ali R." <no****@company.com> wrote
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" <rn*****@gmx.ch> wrote in message
news:3f********@news.tiscalinet.ch...
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 #6


"Ali R." wrote:

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!

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


Karl Heinz Buchegger wrote:


[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]

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #8
In article <Ty****************@newssvr22.news.prodigy.com>,
no****@company.com says...
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?

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9
In article <3f********@news.tiscalinet.ch>, rn*****@gmx.ch says...
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).

--
Later,
Jerry.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
1
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help...
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.