473,387 Members | 1,516 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,387 software developers and data experts.

Formatting stream input.

How does one rewrite the following piece of code using std::cin?

int n;
scanf( "n=%d", &n );

Jul 23 '05 #1
8 1510
BigMan wrote:
How does one rewrite the following piece of code using std::cin?

int n;
scanf( "n=%d", &n );


Something like

char enn, eqsign;
std::cin >> enn >> eqsign >> n;

V
Jul 23 '05 #2
Depends on how closely you want the behavior to match. This is as close
to the behavior as I can get:

char prefix[2];
cin.get(&prefix[0], 3);
if(strcmp(prefix,"n=") == 0)
cin >> n;
else
cin >> dummy;

This reads in two characters, makes sure they are n=, then reads in n.
If the first two characters aren't n=, it leaves n untouched and reads
the next input into a dummy variable. I'm not sure what scanf does in
these cases though, whether it leaves n untouched or puts crap in it,
so don't know if it's the same.

Victor's won't have exactly the same behavior in exceptional conditions
because stuff like "n = 4", which won't work with scanf, will. Whether
this is acceptable or desirable is up to you of course, but it is
different.

Jul 23 '05 #3
Evan wrote:
Depends on how closely you want the behavior to match. This is as
close to the behavior as I can get:

char prefix[2];
cin.get(&prefix[0], 3);
I think '3' is either a typo or a serious mistake.
if(strcmp(prefix,"n=") == 0)
cin >> n;
else
cin >> dummy;

This reads in two characters, makes sure they are n=, then reads in n.
If the first two characters aren't n=, it leaves n untouched and reads
the next input into a dummy variable. I'm not sure what scanf does in
these cases though, whether it leaves n untouched or puts crap in it,
so don't know if it's the same.

Victor's won't have exactly the same behavior in exceptional
conditions because stuff like "n = 4", which won't work with scanf,
will. Whether this is acceptable or desirable is up to you of course,
but it is different.

Jul 23 '05 #4
"BigMan" <Bi****@abv.bg> wrote in message news:<11*********************@o13g2000cwo.googlegr oups.com>...
How does one rewrite the following piece of code using std::cin?

int n;
scanf( "n=%d", &n );

Hey man! IGNORE THE PREVIOUS POST (mad) !!!!! IT actually SUCKs for
NOTHING-ness.(Sound like my arse)
Something like this:
int n;
std::cout<<"n=";
std::cin>>n;


Just that!
Jul 23 '05 #5
> I think '3' is either a typo or a serious mistake.

Ah, it's a serious mistake. Though actually it's the 2 that is the
dimensions of the prefix array that is wrong, not the 3 in get.

I didn't think about the null termination character that get adds at
the end and only gave space for the 'n' and '=' characters. Turning on
MSVC's stack frame checking showed that the get call was stomping over
other parts of the stack; changing the dimension to 3 fixed it.

Corrected code is as follows:

char prefix[3];
cin.get(&prefix[0], 3);

Thanks for the correction.

(You know, when my subconscious goes "that's not right -- why are the
numbers different", I really ought to look into it more...)

Jul 23 '05 #6
Evan wrote:
I think '3' is either a typo or a serious mistake.
Ah, it's a serious mistake. Though actually it's the 2 that is the
dimensions of the prefix array that is wrong, not the 3 in get.

I didn't think about the null termination character that get adds at
the end and only gave space for the 'n' and '=' characters.


Are you sure 'get' adds anything?
Turning on
MSVC's stack frame checking showed that the get call was stomping over
other parts of the stack; changing the dimension to 3 fixed it.

Corrected code is as follows:

char prefix[3];
cin.get(&prefix[0], 3);

Thanks for the correction.
Actually I still think you should be only reading 2 characters from the
input stream. If you read 3, a subsequent read will be missing the leading
digit. I am too lazy to check now, but RTFM on istream::get.
(You know, when my subconscious goes "that's not right -- why are the
numbers different", I really ought to look into it more...)


That's not a bad idea.

V
Jul 23 '05 #7
> Actually I still think you should be only reading 2 characters from
the
input stream. If you read 3, a subsequent read will be missing the leading digit. I am too lazy to check now, but RTFM on istream::get.


get( buffer, count ) reads count-1 characters from the string, then
tags on a terminating 0.

"The three-argument s.get(p,n,term) reads at most n-1 characters into
p[0]..p[n-2]. A call of get() will always place a 0 at the end of the
characters (if any) it placed in p[], so p must point to an array of at
least n characters." (3rd edition of Strostrup, p. 618-619)

cin.read is available if you don't want the null termination added.

Jul 23 '05 #8
Evan wrote:
Actually I still think you should be only reading 2 characters from
the input stream. If you read 3, a subsequent read will be missing
the leading digit. I am too lazy to check now, but RTFM on
istream::get.


get( buffer, count ) reads count-1 characters from the string, then
tags on a terminating 0.

"The three-argument s.get(p,n,term) reads at most n-1 characters into
p[0]..p[n-2]. A call of get() will always place a 0 at the end of the
characters (if any) it placed in p[], so p must point to an array of
at least n characters." (3rd edition of Strostrup, p. 618-619)

cin.read is available if you don't want the null termination added.


You're absolutely correct. I overcame my lazyness and looked it up in
the Standard. Lo and behold, count-1 characters are stored and the null
character is stuffed into the buffer. I must have confused it with the
'read' member. Rats! :-)
Jul 23 '05 #9

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

Similar topics

4
by: Dan Weeb | last post by:
Hi All, I have struggled through this far with help from many of you so thanks. I am stuck again. I am really new to this so don't be harsh :-) There are a few problems. You can run the script...
1
by: Doc Wally | last post by:
Dear Colleagues: To begin with I am not a programmer. In fact, I just got all excited because I was able to create my very first .vbs script that actually worked - not that I actually coded...
6
by: suresh_C# | last post by:
Dear All, I want work on “XSL Formatting Objects (XSL-FO)” using .NET(C#). I found classes for working on XSLT in .NET class library but couldn’t find any sample code for XSL-FO. ...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
1
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.