Re: Newbie questions (conditional operator, cin >> n...)
On Jun 28, 4:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Army1987 wrote:
I am a newbie to C++ (altough I already know some C). I've tried to
implement the TPK algorithm.
#include <iostream>
#include <ostream>
#include <cmath>
const int size = 11;
const double max = 400.0;
double f(const double& t);
int main(void)
{
double array[size];
for (int i = 0; i < size; ++i)
std::cin >> array[i];
for (int i = size - 1; i >= 0; --i) {
double y = f(array[i]);
// std::cout << i + 1 << '\t' << (y > max ? "TOO LARGE" : y )
// << std::endl;
// doesn't compile, because "TOO LARGE" and y are different types.
It's not that they have different types, it's that they can't be
converted to the same type.
Just a nit (and irrelevant here), but the requirement for ?: is
(very roughly) that one can be converted to the type of the
other. If both can be converted to some common third type, it's
not enough. (This situation commonly occurs in things like:
Base* pBase = condition ? new Derived1 : new Derived2 ;
Both Derived1* and Derived2* can be converted to Base*, which in
addition is what we want. But the expression as it stands is
illegal.)
[...]
How can I check if it succeeds?
if (cin.fail()) // last operation ended in 'failbit' set.
if ( ! cin )
or
if ( cin )
is more idiomatic. (I'm not particularly fond of the idiom
myself, but it's what one expects to see. And conforming to
reader expectations is a good thing.)
What does your favourite C++ book say?
Since the cascading
style can be used,
"Cascading style"? I guess I'm getting it confused with HTML or smth.
I think he means something like "cin >> var1 >> var2", where you
"cascade" several >> in one expression. This works for exactly
the reason you mentionned above: the error is "sticky", and
won't go away until explicitly cleared, and operations invoked
when the stream is in an error state are no-ops. So if you
write something like:
if ( cin >> var1 >> var2 ) {
// All OK, use var1 and var2...
} else {
// Error...
}
You can't tell whether the error occured on var1 or on var2, but
the error never gets lost.
For anything but the most trivial use, it's usual to provide
some means of resynchrnonizing. The simplest is when input is
line oriented; just read line by line using getline, then
process each line using an istringstream. In case of an error
when reading the istringstream, the main source is still in a
non-error state, and synchronized ready to read the next line.
If you're converting directly from the input stream, on the
other hand, you need to 1) clear the error state (istream.clear()),
and 2) move ahead to the next synchronization point (consider
istream::ignore(), but if you need to synchronize on more than a
single character, it will require actively reading the file).
[...]
Also, is there a good free online C++ tutorial, especially one
which points out differences with C?
I think that "good" and "free" in the same expression make an oxymoron.
It depends. G++ is one of the better compilers around, and it
is "free" in most senses of the word. (In the strictest sense,
no software is ever totally free, since it costs time and effort
to install it, learn it, etc., etc. G++ doesn't cost any more
than most commercial offerings in this regard, however, and the
purchase price is considerably lower than most.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34