Re: basic char and int array loading

From: zentara (zentara_at_highstream.net)
Date: 12/13/04


Date: Mon, 13 Dec 2004 07:21:16 -0500

On Sun, 12 Dec 2004 01:21:03 +0100, Ulrich Eckhardt <doomster@knuut.de>
wrote:

>
>This calls operator>>( istream&, int&). If you enter e.g. 'e', the operator
>will try to convert that input to a number and fail. The input remains in
>the stream's buffer and all subsequent input operations will fail, until
>you clear() the stream.
>
>If you want the byte-value of an entered char, you need to read it as such:
> char tmp;
> cin>>tmp;
> // convert to unsigned char first to suppress sign promotion
> unsigned value = static_cast<unsigned char>(tmp);
>
>BTW:
>- you can do more than 'i++' in the for statement, e.g. 'i++, j++'
>- in that loop, i always has the same value as j
>- you can declare and initialise vars in a for statement:
> for(int i(0); i!=5; ++i) { ... }
>- a char has always size 1, not only on your system. An int doesn't always
>have size 4 though, that's just your system.

Thanks for the insights. The explanation of the failed data remaining
in the stream's buffer now makes things understandable.

Comparing this to Perl, I don't worry about whether a piece of data
is string or numeric. So I was wondering, is there a generally used
way in C++, to load an array with a combination of strings and numerics,
so that it is all handled gracefully, or do I need to do all the testing
and clearing myself?

Do you just pull everything in as strings, and worry about converting
string to int or double later?

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html


Relevant Pages

  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you check EVERY return from a call that can fail, ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you check EVERY return from a call that can fail, and any file system call can fail, ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a 30-character ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Secure C library
    ... I read much of the new "security TR", and gee, I don't know. ... the buffer from the buffer size. ... It is not hard to design a better form of buffer and string handling. ... but this is just one example of how thoughtful interface design can ...
    (comp.std.c)
  • Re: Secure C library
    ... >> string functions don't make much sense once you add bounds-checking ... >> designing an interface just for the purpose of reducing the frequency ... > make buffer size decisions more visible, ... Bstrlib is also very interoperable with char *'s, ...
    (comp.std.c)