Thread: Help with program and number equalities (==)

  1. #1
    Pet Undertaker
    Join Date
    Jul 2005
    Posts
    2

    Question Help with program and number equalities (==)

    Hello,

    I am having trouble with some code that I am using. No matter what number I press in the following code when I run it, It always thinks that ( finoptype == 1 ):

    Code:
             {
             float finoptype;
             cout<<"\nWelcome to the finace calculator group.  Currently the Math Master can calculate the total cost of a loan, or calculate the intrest money will earn in a savings account/CD in a period of time that you specify.  Please choose either the loan calculator(1) or the savings interest calculator(2): ";
             cin>> finoptype;
             cin.ignore();
             if ( finoptype = 1 ) {
                  float finloantot;
                  float finloaninttype;
                  float finloanlength;
                  cout<<"\nPlease enter the amount of money the loan will be for (Please no dollar signs): ";
                  cin>> finloantot;
                  cin.ignore();
                  cout<<"\nPlease enter the total length in years of the loan: ";
                  cin>> finloanlength;
                  cin.ignore();
                  cout<<"\nPlease select the loan interest rate type, either monthly(1) or yearly(2): ";
                  cin>> finloaninttype;
                  cin.ignore();
                  if ( finloaninttype == 1 ) {
                       float finloanintrate1;
                       float finloanyerrate1;
                       float finloantottot11;
                       float finloantottot12;
                       float finloantottot13;
                       cout<<"\nPlease enter loan monthly interest rate in decimal form (if using a percentage, move the decimal point back two places from the end of the percentage): ";
                       cin>> finloanintrate1;
                       cin.ignore();
                       finloanyerrate1 = finloanintrate1 * 12;
                       finloantottot11 = finloanyerrate1 * finloantot;
                       finloantottot12 = finloantottot11 * finloanlength;
                       finloantottot13 = finloantottot12 + finloantot;
                       cout<<"\nThe total cost of your loan is: "<< finloantottot13 <<" dollars.\n";
                  }
                  else if ( finloaninttype == 2 ) {
                       float finloanintrate2;
                       float finloantottot21;
                       float finloantottot22;
                       float finloantottot23;
                       cout<<"\nPlease enter loan yearly interest rate in decimal form (if using a percentage, move the decimal point back two places from the end of the percentage): ";
                       cin>> finloanintrate2;
                       cin.ignore();
                       finloantottot21 = finloanintrate2 * finloantot;
                       finloantottot22 = finloantottot21 * finloanlength;
                       finloantottot23 = finloantottot22 + finloantot;
                       cout<<"\nThe total cost of your loan is: "<< finloantottot23 <<" dollars.\n";
                  }
                  else {
                       cout<<"That is not a valid option.  Please run the program again and enter a valid option.";
                  }
             }
    Any help that you can give me would be great.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
             if ( finoptype = 1 ) {
    This sets finoptype to 1, regardless of what the user entered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Pet Undertaker
    Join Date
    Jul 2005
    Posts
    2
    Ugh, I can't believe I didn't notice that! I looked through this program countless times and I could not find it!

    Thanks for being a great help to me. I appreciate your assistance.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use g++ as your compiler
    Then things like
    g++ -W -Wall -ansi prog.cpp
    will show up all sorts of odd things like that particular mistake.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your finoptype variable is a float. It should be an int. Be careful when comparing floating point types. It probably won't matter if you are just using integer values, but still.
    http://www.parashift.com/c++-faq-lit...html#faq-29.17

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM