473,394 Members | 1,787 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,394 software developers and data experts.

initialization help on variables

6
I get an error that my variables are not initializing but I thought I set the program up to accept user input for the calculation. The warnings I get are on the principal, rate, and term. the only other thing I can think of is the program needs some starting point for the variables like principal = 200000, rate = .0575, and term = 30 But I was under the impression that would set the variables to those values. I tried to put those values in but now I just get a black box when it runs that does not take user input, although there are no errors.




int main()
{
//declare variables
double mrate, payment, principal ,rate;
int totalpayments, term;






mrate = rate/12; //monthly interest rate
totalpayments = term * 12; //amount of total payments
char indicator = 'y';
while( indicator == 'y' || indicator == 'Y')


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

while( indicator == 'y' || indicator == 'Y')
{
//get user input
cout << endl
<< "Enter principal amount in 100000 format: ";
cin >> principal;
cout << endl
<< "Enter the interest rate of the loan in .0575 format: ";
cin >> rate;
cout << endl
<< "Enter the term on the loan in a 30 format: ";
cin >> term;
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 23 '08 #1
5 1322
TamusJRoyce
110 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4. //declare variables
  5. double mrate, payment, principal ,rate;
  6. int totalpayments, term;
  7.  
// Shouldn't you ask for user input here instead of below?
// Then do the calculations you need on that gathered data?

// This block below is having calculations done before
// you have grabbed the users input.
Expand|Select|Wrap|Line Numbers
  1. mrate = rate/12; //monthly interest rate
  2. totalpayments = term * 12; //amount of total payments
  3. char indicator = 'y';
  4. while( indicator == 'y' || indicator == 'Y')
  5.  
  6. //mortgage formula
  7. payment = (principal * pow(mrate + 1, totalpayments) * mrate)/(pow(mrate + 1,totalpayments) - 1);
  8.  
// Maybe your calculations above needs to be done within the loop?
// I'm not sure, but if calculations are done below the
// loop, then you would only be able to do your above
// calculations once on the last input the user entered,
// and not each time the user inputs data?
Expand|Select|Wrap|Line Numbers
  1. while( indicator == 'y' || indicator == 'Y')
  2. {
  3. //get user input
  4. cout << endl
  5. << "Enter principal amount in 100000 format: ";
  6. cin >> principal;
  7. cout << endl
  8. << "Enter the interest rate of the loan in .0575 format: ";
  9. cin >> rate;
  10. cout << endl
  11. << "Enter the term on the loan in a 30 format: ";
  12. cin >> term;
  13. cout << endl
  14. << "Your monthly payment is " << payment << " dollars a month."
  15. <<endl;
  16. cout << "Do you wish to calculate another loan(y or n)? ";
  17. cin >> indicator;
  18.  
  19. return 0;
  20.  
  21.  
And where is your ending bracket for your while loop? It looks like your code is ok, but the order is messed up a bit.

reply back your code once you change it around a bit... This isn't a solution, but merely some hints.
Sep 23 '08 #2
cbrrr
6
thanks so far, i figured it out after I posted. I had the equation in the wrong place so nothing was happening(like you pointed out). Now I have it down to illegal continue errors.

int principal, term;
double rate;

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.00" << 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;
}



}
//declare 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
char indicator = 'y';

// 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 23 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
int main()
{
//declare variables
double mrate, payment, principal ,rate;
int totalpayments, term;






mrate = rate/12; //monthly interest rate
etc.........
So what is the value in rate?
Sep 24 '08 #4
TamusJRoyce
110 100+
Principle is an integer and cannot have decimal places...

I"m also not sure if 999999999 is bigger than an integer can hold. A short is 15 bits with the 16'th bit for being signed; and 2^15 is 32767. An integer is 31 bits with the 32'th bit for being signed; and 2 ^31 is 2147483647. So 999999999 is smaller than 2000000000. That's ok.

Below will loop until a valid number is entered...
Expand|Select|Wrap|Line Numbers
  1. int principle = -1;
  2. while ((principle < 0) || (principle > 999999999))
  3. {
  4.   cout << "Please enter a principle between 0 and 999999999." << endl;
  5.   cout << "[integer] > ";
  6.   cin >> principle;
  7. } // End while
  8.  
You may try doing something like this for each input you need. But my suggestion to getting everything working is to make a new program, and copy section by section, compiling/debugging/fixing between each section added until you have the whole program both compilable, and doing what you want.

Writing the program down on paper also helps. It's a bit long & may have arrows & fixes going here and there, but only a small amount of code can fit on one page. So it makes you focus on only the parts you need to get working. You can also submit your notes with your homework, showing the teacher you did all the work yourself (got me browny points in college) if this is homework you are doing...

Please post more as you work on it more : )

Hopefully helpful,

TamusJRoyce
Sep 24 '08 #5
cbrrr
6
yea I figured it out last night thanks
Sep 25 '08 #6

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

Similar topics

3
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
12
by: Christoph Zwerschke | last post by:
Usually, you initialize class variables like that: class A: sum = 45 But what is the proper way to initialize class variables if they are the result of some computation or processing as in...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
4
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.