By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,543 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,543 IT Pros & Developers. It's quick & easy.

loop and declaring variable question

P: 6
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;


}
Sep 24 '08 #1
Share this Question
Share on Google+
2 Replies


boxfish
Expert 100+
P: 469
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.
Sep 24 '08 #2

100+
P: 147
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.
Sep 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.