If else /else if problem

This is a discussion on If else /else if problem within the C++ Programming forums, part of the General Programming Boards category; Ok so, I feared this would happen when I compiled and ran, and surely enough it did. I'm getting a ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    If else /else if problem

    Ok so, I feared this would happen when I compiled and ran, and surely enough it did.
    I'm getting a logical error within this program and i cant figure out how to fix it. I want to make it so that when I enter any of the numbers between 0 - 19, 20 - 39, 40 - 59, or 60+, it will only do the calculation of the entered number by the specific decimal value assigned to that group of numbers. Take a look at my code and you'll understand what im talking about

    Code:
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	
    
    	double checkAmount;
    	int monthBankCharge = 10;
    	cout << "Enter the amount of checks you've written in the past month\n"; 
    
    	cin >> checkAmount;
    
    	if (checkAmount < 20)
    	{ cout << checkAmount * 0.10 << endl;}
    
    	else if (checkAmount >= 20)
    	{ cout << checkAmount * 0.08 << endl;}
    
    	else if (checkAmount >= 40)
    	{ cout << checkAmount * 0.06 << endl;}
    
    	else if (checkAmount >= 60)
    	{ cout << checkAmount * 0.04 << endl;}
    
    
     
     system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    If checkAmount == 43 then the first if(checkAmount < 20) will not execute but since 43 > 20 the next if statement will execute and the rest of the statements will be skipped. You may want to rearrange your else if clauses. Maybe switch the >= 20 with >= 60.

    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Consider input such that checkAmount = 70. Then, trace the flow of control of the program, line by line. What do you observe?
    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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    The question says:

    A bank charges $10 per month plus the following check fees for a commercial checking account:
    $0.10 each for fewer than 20 checks
    $0.08 each for 20-39 checks
    $0.06 each for 40-59 checks
    $0.04 each for 60 or more checks

    write a program that asks for the number of checks written during the past month, then computes and displays the bank's fees for the month.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    never mind I finally figured it out. Thanks to all that helped (:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

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