initialization help on variables

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

    initialization help on variables

    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;

    }
    Last edited by cbrrr; Sep 23 '08, 09:36 PM. Reason: tried a change to answer my own question
  • TamusJRoyce
    New Member
    • Apr 2008
    • 108

    #2
    Code:
    int main()
    {
    //declare variables
    double mrate, payment, principal ,rate;
    int totalpayments, term;
    // 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.
    Code:
    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);
    // 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?
    Code:
    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;
    
    }
    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.

    Comment

    • cbrrr
      New Member
      • Sep 2008
      • 6

      #3
      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;


      }
      Last edited by cbrrr; Sep 24 '08, 12:25 AM. Reason: reordered/ input restictions, input values,

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by cbrrr
        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?

        Comment

        • TamusJRoyce
          New Member
          • Apr 2008
          • 108

          #5
          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...
          Code:
          int principle = -1;
          while ((principle < 0) || (principle > 999999999))
          {
            cout << "Please enter a principle between 0 and 999999999." << endl;
            cout << "[integer] > ";
            cin >> principle;
          } // End while
          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

          Comment

          • cbrrr
            New Member
            • Sep 2008
            • 6

            #6
            yea I figured it out last night thanks

            Comment

            Working...