How does one rewrite the following piece of code using std::cin?
int n;
scanf( "n=%d", &n ); 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
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.
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.
"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!
> 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...)
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
> 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.
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! :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
...
|
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...
|
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...
|
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?
...
|
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:
//...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |