473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

loop and declaring variable question

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
2 2285
boxfish
469 Expert 256MB
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
scruggsy
147 100+
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

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

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
5
by: Chris | last post by:
Is there any difference (memory/speed wise) between these two snipits. I always like to write in snipit 2 style, but was just curious. I'm scared I'd declare a thousand pointers when I don't need...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
4
by: Newbie | last post by:
I need to iterate through all the cells in an HTML-like table, e.g. <for-each select="tr"> <for-each select="td"> Get coordinates of cell here </for-each> </for-each> How can I access my...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.