Undeclared Identifier... but why?

Discussion in 'C++' started by Kensai, Dec 18, 2009.

  1. Kensai

    Kensai Guest

    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?
     
    Kensai, Dec 18, 2009
    #1
    1. Advertising

  2. On Dec 18, 2:09 pm, Kensai <> wrote:
    > 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.
     
    Gert-Jan de Vos, Dec 18, 2009
    #2
    1. Advertising

  3. Kensai

    Kensai Guest

    Gert-Jan de Vos wrote:

    >
    >
    > Gert-Jan de Vos wrote:
    >
    >
    >> 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.





    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?


    Christian Hackl wrote:

    > Christian Hackl wrote:
    >
    >> The indentation of your code is misleading. What is actually understood
    >> here by the compiler is:
    >>
    >> if (val1>val2)
    >> {
    >> double ratio = val1/val2;
    >> }
    >> cout << "The first value is larger than the second. (etc.)";
    >>
    >>
    >> Do you see the problem? Your "ratio" variable goes immediately out of
    >> scope.



    Damn. The Scope Chapter is later. I can't understand you. :(
    How should the indentation be? I thought I was following Stroustrup's style.
     
    Kensai, Dec 18, 2009
    #3
  4. Kensai wrote:
    >
    >
    > Gert-Jan de Vos wrote:
    >
    > >
    >>
    >> Gert-Jan de Vos wrote:
    >>
    >>
    >>> 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.

    >
    >
    >
    >
    > 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?


    <shrug> There is always more than one way to write correct code.

    >
    >
    > Christian Hackl wrote:
    >
    >> Christian Hackl wrote:
    >>
    >>> The indentation of your code is misleading. What is actually
    >>> understood here by the compiler is:
    >>>
    >>> if (val1>val2)
    >>> {
    >>> double ratio = val1/val2;
    >>> }
    >>> cout << "The first value is larger than the second. (etc.)";
    >>>
    >>>
    >>> Do you see the problem? Your "ratio" variable goes immediately out of
    >>> scope.

    >
    >
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 18, 2009
    #4
  5. Kensai

    Kensai Guest

    Victor Bazarov wrote:

    > 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
     
    Kensai, Dec 18, 2009
    #5
  6. Kensai

    Jonathan Lee Guest

    On Dec 18, 4:07 pm, Kensai <> wrote:
    > Is comp.lang.c++ good for newbie questions like these or is there a more
    > appropraite channel?


    I'm sure people here will usually step up and help, but it's probably
    better to direct "newbie" questions to alt.comp.lang.learn.c-c++

    There's also groups mentioned in the FAQ for specific issues
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9



    --Jonathan
     
    Jonathan Lee, Dec 18, 2009
    #6
  7. Kensai wrote:
    > Victor Bazarov wrote:
    >
    >> 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.


    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 18, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Natalie

    Re: undeclared identifier?

    Ron Natalie, Jul 21, 2003, in forum: C++
    Replies:
    0
    Views:
    437
    Ron Natalie
    Jul 21, 2003
  2. Victor Bazarov

    Re: undeclared identifier?

    Victor Bazarov, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    2,699
    rajiv04
    Jul 22, 2003
  3. yttrium
    Replies:
    12
    Views:
    1,480
    Jonathan Bromley
    Jan 5, 2007
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,217
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    2
    Views:
    1,844
Loading...

Share This Page