Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 10 years ago

Why is the C++ complier complaining?

I wrote a program that includes a function that converts Celsius to Kelvin. When the function looks like this, it compiles and works fine:

double ctok(double c) //converts Celsius to Kelvin

{

double k = c + 273.15;

return k;

}

But when I add an error check, the compiler says:

hw2pr2.cpp: In function `double ctok(double)':

hw2pr2.cpp:13: error: `k' was not declared in this scope

hw2pr2.cpp: In function `double ktoc(double)':

hw2pr2.cpp:22: error: `c' was not declared in this scope

Here is the program with the error check; what did I do wrong? (the comments are just required for class..sorry):

#include "std_lib_facilities.h"

class Below_abs_zero{};

double ctok(double c) //converts Celsius to Kelvin

{

if(c < -273.15)

throw Below_abs_zero();

else

double k = c + 273.15;

return k;

}

double ktoc(double k) //converts Kelvin to Celsius

{

if(k < 0)

throw Below_abs_zero();

else

double c = k - 273.15;

return c;

}

int main()

try{

cout << "\nThis program will convert a temperature from Celsius to Kelvin\n";

cout << "or from Kelvin to Celsius. Please enter a temperature and its current\n";

cout << "unit. Sample input: 34c (this will be converted to Kelvin)\n\n";

cout << "Enter temperature and unit: ";

double t; //input variable

char unit; //Celsius or Kelvin

cin >> t >> unit; //retrieve temperature and unit to input variable

double c=0;

double k=0;

if(unit == 'c'){

c=t;

double k = ctok(c); //convert temperature

cout << k << "K" << endl;}

else if(unit == 'k'){

k=t;

double c = ktoc(k); //convert temperature

cout << c << "C" << endl;}

else throw exception();

return 0;

}

catch(Below_abs_zero){

cout << "\nerror: Temperatures below absolute zero not allowed!\n";

}

catch(exception& e){

cerr << "error: " << e.what() << '\n';

keep_window_open();

return 2;

}

catch(...){

cerr << "Oops: unknown exception!\n";

keep_window_open();

return 3;

}

5 Answers

Relevance
  • 10 years ago
    Favorite Answer

    You are getting bit by scope rules. For example, your ctok function:

    double ctok(double c) //converts Celsius to Kelvin

    {

    if(c < -273.15)

    throw Below_abs_zero();

    else

    double k = c + 273.15;

    return k;

    }

    actually looks like this, if you add the braces explicitly:

    double ctok(double c) //converts Celsius to Kelvin

    {

    if(c < -273.15)

    {

    throw Below_abs_zero();

    }

    else

    {

    double k = c + 273.15; // creates variable k on the stack

    }// variable k no longer exists

    return k;

    }

    Note that the "double k" is defined within the scope of the else block. When you try to return k, the compiler doesn't know what k is because it doesn't exist in the scope that the return statement is in.

    To fix it, you can just declare the variable in the scope that the return statement is in:

    double ctok(double c) //converts Celsius to Kelvin

    {

    double k = 0.0; // we define k here so its accessible to the return statement

    if(c < -273.15)

    {

    throw Below_abs_zero();

    }

    else

    {

    k = c + 273.15; // creates variable k on the stack

    }

    return k; // this is fine since k is still in scope

    }

    The same issue got you with ktoc.

    Good luck!

  • John
    Lv 5
    10 years ago

    Because the c and k in your error checked functions are declared in the else clause of the if statements. That means they're only in scope during the else. It goes out of scope (it's gone) before the return statements.

    You can fix it by declaring the variable before the if statement:

    double ktoc(double k) //converts Kelvin to Celsius

    {

    double c;

    if(k < 0)

    throw Below_abs_zero();

    else

    c = k - 273.15;

    return c;

    }

    or if you like,

    double ktoc(double k) //converts Kelvin to Celsius

    {

    if(k < 0)

    throw Below_abs_zero();

    else

    return k - 273.15;

    }

  • 10 years ago

    it's because the k variable goes out of scope as soon as it has assigned the value to k.

    you can do this

    double ctok(double c) //converts Celsius to Kelvin

    {

    double k = 0;

    if(c < -273.15)

    throw Below_abs_zero();

    else

    k = c + 273.15;

    return k;

    }

    or just not use the variable

    double ctok(double c) //converts Celsius to Kelvin

    {

    if(c < -273.15)

    throw Below_abs_zero();

    return (c + 273.15);

    }

  • 10 years ago

    The scope of k (and c) is limited to the else part of your if statement. As soon as your else statement finishes, k doesn't exist anymore.

    Think about what would happen if your if part didn't throw an exception, but just printed an error message. What would return k mean then? Wouldn't make any sense, would it? The fact that you throw an exception doesn't change that.

    Move definition of k outside of the else statement. Or do the return from inside the else statement.

  • How do you think about the answers? You can sign in to vote the answer.
  • Anonymous
    5 years ago

    For Windows, Visual Studio C++ is really the best. The express edition is free. For standard C/C++ programs, gcc could be the best, but for various graphical and system design specific for Windows, Visual Studio outperforms other compilers.

Still have questions? Get your answers by asking now.