Undeclared Identifier... but why?

K

Kensai

I get these two errors on VC++ EE.

(13): error C2065: 'ratio' : undeclared identifier
(19): error C2065: 'ratio' : undeclared identifier

in one of my first exercises (Chapter 3, Programming by Stroustrup):

#include "../../std_lib_facilities.h"
// two values comparison
int main()
{
cout << "Please enter two integer values.\n";
int val1; int val2;
cin >> val1 >> val2;
if (val1>val2)
double ratio = val1/val2;
cout << "The first value is larger than the second. Their sum is " <<
val1+val2 << ". "
<< "Their difference is " << val1-val2 << ". "
<< "Their product is " << val1*val2 << ". "
<< "The ratio of these values is " << ratio << ".\n";
if (val1<val2)
double ratio = val2/val1;
cout << "The second value is larger than the first. Their sum is " <<
val1+val2 << ". "
<< "Their difference is " << val2-val1 << ". "
<< "Their product is " << val1*val2 << ". "
<< "The ratio of these values is " << ratio << ".\n";
if (val1==val2)
cout << "The first value is equal to the second. Their sum is " <<
val1+val2 << ". "
<< "Their difference is 0. "
<< "Their product is " << val1*val2 << ". "
<< "The ratio of these values is 1.\n";
keep_window_open(); // wait for a character to be entered
return 0;
}

Why? I just want to do the division using a double type to have a better
ratio. Why is the compiler complaining?
 
G

Gert-Jan de Vos

I get these two errors on VC++ EE.

(13): error C2065: 'ratio' : undeclared identifier
(19): error C2065: 'ratio' : undeclared identifier

in one of my first exercises (Chapter 3, Programming by Stroustrup):

#include "../../std_lib_facilities.h"
// two values comparison
int main()
{
        cout << "Please enter two integer values.\n";
        int val1; int val2;
        cin >> val1 >> val2;
        if (val1>val2)
                double ratio = val1/val2;
                cout << "The first value is larger than the second. Their sum is " <<
val1+val2 << ". "
                << "Their difference is " << val1-val2 << ". "
                << "Their product is " << val1*val2 << ". "
                << "The ratio of these values is " << ratio << ".\n";
Why? I just want to do the division using a double type to have a better
ratio. Why is the compiler complaining?

You extended the single statement condition block "cout << ..." with a
second statement:
the definition of ratio. Conditional blocks are by default single
statements. If you
want to include more statements inside the conditional block, add {}
to make the block
explicit. This is why some advise to use explicit {} blocks always.
 
K

Kensai

Gert-Jan de Vos said:
>

Gert-Jan de Vos wrote:




Ok, I'm totally newbie so please bear with me. I try to follow the
elements-of-style as taught by Stroustrup himself in his book for
beginners. So I guess I was following the right style.

I don't quite understand why the compiler doesn't just read serially
what I want it to read.

cout << "Please enter two values.\n";
int val1; int val2;
cin >> val1 >> val2;
if (val1>val2)
double ratio = val1/val2;
cout << "The first value is larger than the second. Their sum is " <<
val1+val2 << ". "
<< "Their difference is " << val1-val2 << ". "
<< "Their product is " << val1*val2 << ". "
<< "The ratio of these values is " << ratio << ".\n";


--------
Here's my thinking, please correct me where I'm wrong:

I give a prompt to the user, I define two integers and ask the user to
enter them. Then I say IF the first is larger than the second do all the
following (serially)... define a floating-point variable named "ratio"
and do that division which is more accurate than an integer division and
print out those silly operations.

Now, what would have been the correct code?




Damn. The Scope Chapter is later. I can't understand you. :(
How should the indentation be? I thought I was following Stroustrup's style.
 
V

Victor Bazarov

Kensai said:
Ok, I'm totally newbie so please bear with me. I try to follow the
elements-of-style as taught by Stroustrup himself in his book for
beginners. So I guess I was following the right style.

Indentation (whitespace) has no effect on the meaning of the code
*except* when it separates tokens. C++ is not Python.
I don't quite understand why the compiler doesn't just read serially
what I want it to read.

It does. That's the whole point.

if (a>b)
dosomething();
blah();

is exactly the same as

if(a>b)dosomething();blah();

and as

if (a > b) dosomething ( ); blah ( );

and as

if (a > b)
dosomething();
blah();

(the latter is the most conventional style, if we're talking styles).
cout << "Please enter two values.\n";
int val1; int val2;
cin >> val1 >> val2;
if (val1>val2)

Can it be you're not seeing a curly brace after the closing parenthesis
on that line? Like this:

if (val1>val2) {

....
double ratio = val1/val2;
cout << "The first value is larger than the second. Their sum is
" << val1+val2 << ". "
<< "Their difference is " << val1-val2 << ". "
<< "Their product is " << val1*val2 << ". "
<< "The ratio of these values is " << ratio << ".\n";


--------
Here's my thinking, please correct me where I'm wrong:

I give a prompt to the user, I define two integers and ask the user to
enter them. Then I say IF the first is larger than the second do all the
following (serially)... define a floating-point variable named "ratio"
and do that division which is more accurate than an integer division and
print out those silly operations.

Now, what would have been the correct code?

Damn. The Scope Chapter is later. I can't understand you. :(
How should the indentation be? I thought I was following Stroustrup's
style.

....whatever that might be. Please understand this: the style does NOT
matter. Tokens matter. If you don't have curly braces, only one
statement will be considered the conditional statement's control
statement. Curly braces create a compound statement (the most important
use of the '{' and '}', AFA I'm concerned.

V
 
K

Kensai

Victor said:
Please understand this: the style does NOT
matter. Tokens matter. If you don't have curly braces, only one
statement will be considered the conditional statement's control
statement. Curly braces create a compound statement (the most important
use of the '{' and '}', AFA I'm concerned.

V

Thank you!! This is what I wanted to see. "Curly braces create a
compound statement." This wasn't clear to me.

But while I was playing to find a solution I came to another surprise.
Even as a compound statement it wouldn't work since:

int var1;
int var2;
cin >> var1 >> var2;
double ratio = var1 / var 2;

gives a new variable named "ratio" but the result will always be an
integer division since the operation is done BEFORE it gives out the new
value assignment.

Is comp.lang.c++ good for newbie questions like these or is there a more
appropraite channel?

Thank you all,
Constantine
 
V

Victor Bazarov

Kensai said:
Thank you!! This is what I wanted to see. "Curly braces create a
compound statement." This wasn't clear to me.

But while I was playing to find a solution I came to another surprise.
Even as a compound statement it wouldn't work since:

int var1;
int var2;
cin >> var1 >> var2;
double ratio = var1 / var 2;

gives a new variable named "ratio" but the result will always be an
integer division since the operation is done BEFORE it gives out the new
value assignment.

Yes. The expression 'var1 / var2' (no space before '2' BTW) has the
type 'int' since only two variables of the type 'int' are involved. You
can avoid this by casting one of them to double:

double ratio = double(var1) / var2;
Is comp.lang.c++ good for newbie questions like these or is there a more
appropraite channel?

It should be fine. There is also 'alt.comp.lang.learn.c-c++' where I
would suspect to find more folks who are new to the language than here,
but I've not gone there in years, so I wouldn't really know.

Visit http://www.catb.org/~esr/faqs/smart-questions.html . And the FAQ
which you can find here: http://www.parashift.com/c++-faq-lite/ .

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top