loop and declaring variable question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cbrrr
    New Member
    • Sep 2008
    • 6

    loop and declaring variable question

    I have the variables declared twice where i think they should be. If I declare them global I get an uninitialized error, the other position gives me a declaration error. Im using visual C++ express 08 on MS.

    using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;

    char indicator = 'y';

    while( indicator == 'y' || indicator == 'Y')

    //declare global variables
    double mrate, payment, principal ,rate;
    int totalpayments, term;



    int main()
    {

    {
    //get user input principal
    cout << endl
    << "Enter principal amount in 100000 format: ";
    cin >> principal;

    // check principal data 1.0 to 999999999.9
    if (principal <= 0.00 || principal > 999999999.9)
    {
    cout << "************** *************** *************** *************** *******" << endl;
    cout << endl << "Principal - between 1 and 999999999.0" << endl;
    cout << "************** *************** *************** *************** *******" << endl;
    continue;
    }

    cout << endl
    //get user input intrest rate
    << "Enter the interest rate of the loan in .0575 format: ";
    cin >> rate;
    if (rate <= 0.0000 || rate > .5999)
    {
    cout << "************** *************** *************** *************** *******" << endl;
    cout << endl << "Interest Amount - between 1 and 31.99" << endl;
    cout << "************** *************** *************** *************** *******" << endl;
    continue;

    }
    cout << endl
    << "Enter the term on the loan in a 30 format: ";
    cin >> term;
    // check term input 1 -30
    if (term <= 0 || term > 30)
    {
    cout << "************** *************** *************** *************** *******" << endl;
    cout << endl << "Loan term years - between 1 and 30" << endl;
    cout << "************** *************** *************** *************** *******" << endl;
    continue;
    }



    }
    //redclare variables
    double mrate, payment, principal ,rate;
    int totalpayments, term;

    //mortgage formula
    payment = (principal * pow(mrate + 1, totalpayments) * mrate)/(pow(mrate + 1,totalpayments ) - 1);





    mrate = rate/12; //monthly interest rate
    totalpayments = term * 12; //amount of total payments


    // output
    cout << endl
    << "Your monthly payment is " << payment << " dollars a month."
    <<endl;


    cout << "Do you wish to calculate another loan(y or n)? ";
    cin >> indicator;


    return 0;


    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    That's weird, you put the main function inside of a loop. Put the loop inside of the main function. But aside from that, I think that Microsoft Visual C++ doesn't do scopes. Which means that it won't let you redeclare variables, even in a new scope. I don't see why you need to redeclare them here, though. Try putting the loop inside of the main function and not redeclaring the variables and see if it works. It does on my compiler, Dev-C++.
    Hope this helps.

    Comment

    • scruggsy
      New Member
      • Mar 2007
      • 147

      #3
      Originally posted by boxfish
      Hi,
      That's weird, you put the main function inside of a loop. Put the loop inside of the main function. But aside from that, I think that Microsoft Visual C++ doesn't do scopes. Which means that it won't let you redeclare variables, even in a new scope. I don't see why you need to redeclare them here, though. Try putting the loop inside of the main function and not redeclaring the variables and see if it works. It does on my compiler, Dev-C++.
      Hope this helps.
      MSVC++ does scopes; a local identifier with the same name as a global hides the global.

      OP, your code is a bit of a mess. As boxfish said your while loop belongs inside main() - or at least inside some function. The compiler should be complaining about that.

      That issue aside, if you indent your code you'll see that your four double variables are not declared globally; they are declared as local to the scope of your while loop.

      Comment

      Working...