Help with program and number equalities (==)

This is a discussion on Help with program and number equalities (==) within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having trouble with some code that I am using. No matter what number I press in the ...

  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
    21,746
    Code:
             if ( finoptype = 1 ) {
    This sets finoptype to 1, regardless of what the user entered.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    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, 09: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, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21