logical errors when i compile.

This is a discussion on logical errors when i compile. within the C++ Programming forums, part of the General Programming Boards category; when i compile the code i get the console screen ( which is a good thing sicne there is no ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    logical errors when i compile.

    when i compile the code i get the console screen ( which is a good thing sicne there is no syntax errors) i have a problem with the code not understand when i type in 6000 it suppose 180 since the tax is 3%.



    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <math.h>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    double GetIncome (double Income, double tax)
    {
        double NEWI;
        NEWI = Income * (tax/100) ;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
        return NEWI;
    
    }
    int main ()
    {
    
    int Income;
    double tax;
    double NEWI;
    cout << "Mini Program 5.1:" << endl;
    cout << "Enter the income: " << endl;
    cin >> Income;
         if (Income <= 5000)
            {tax = 0;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=10000)
            {tax = 3; 
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=20000)
            {tax = 5.5;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=40000)
            {tax = 10.8;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else
            {tax = 23.7;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
            
            
        system("pause");
    return 0;
    }

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Teehee, i've done this before, its in your if statements...

    Code:
         if (Income <= 5000)
            {tax = 0;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=10000)
            {tax = 3; 
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=20000)
            {tax = 5.5;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (Income <=40000)
            {tax = 10.8;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else
            {tax = 23.7;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
    You're saying if income <= 5000, well isnt that <= 10000? 20000? etc? etc?

    teehee, every single one of those if statements are coming up true.

    I believe your output is using the 10.8% tax

    This setup would be fine if you did this

    Code:
    cin >> Income;
         if ((Income <= 5000) && (Income >= 0))
            {tax = 0;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if ((Income <=10000) && (Income >= 5001))
            {tax = 3; 
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if ((Income <=20000) && (Income >= 10001)))
            {tax = 5.5;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (((Income <=40000) && (Income >= 20001)))
            {tax = 10.8;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else
            {tax = 23.7;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
    Last edited by Shamino; 11-17-2005 at 04:39 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    well thats fixed i suppose but i still get that logical error.



    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <math.h>
    
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    double GetIncome (double Income, double tax)
    {
        double NEWI;
        NEWI = Income * (tax/100) ;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
        return NEWI;
    
    }
    int main ()
    {
    
    int Income;
    double tax;
    double NEWI;
    cout << "Mini Program 5.1:" << endl;
    cout << "Enter the income: " << endl;
    cin >> Income;
         if ((Income <= 5000) && (Income >= 0))
            {tax = 0;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if ((Income <=10000) && (Income >= 5001))
            {tax = 3; 
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (((Income <=20000) && (Income >= 10001)))
            {tax = 5.5;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else if (((Income <=40000) && (Income >= 20001)))
            {tax = 10.8;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
         else
            {tax = 23.7;
            cout << "You have entered an income of  $"<< Income << endl;
            cout << "The income tax owed is  $"<< NEWI<< endl;}
        system("pause");
        
        
    return 0;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <math.h>
    That last one should be <cmath>, and the include statements don't add the libraries to the program, they only make the function prototypes available to the compiler for those functions mentioned in the headers so that the compiler can do its job typechecking any function arguments and return values. It is the linkers job to bring the relevant library code for those actual functions that you use into the program.


    Code:
    system("pause");
    Technically you should have included <cstdlib> for the system function. However, you should replace that with a couple calls to the cin.get() function.

    Code:
    if (Income <= 5000)
    {
        tax = 0;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
    }
    else if (Income <=10000)
    {
        tax = 3; 
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
    }
    else if (Income <=20000)
    {
        tax = 5.5;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
    }
    else if (Income <=40000)
    {
        tax = 10.8;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
    }
    else
    {
        tax = 23.7;
        cout << "You have entered an income of  $"<< Income << endl;
        cout << "The income tax owed is  $"<< NEWI<< endl;
    }
    This way of testing the level of income is fine, you shouldn't need to do it the other way that was suggested. The real problem is that you aren't calling your GetIncome function anywhere:
    Code:
    // The following cout was common to all blocks of code below so I moved it out here
    cout << "You have entered an income of  $"<< Income << endl;
    if (Income <= 5000)
    {
        tax = 0;
        cout << "The income tax owed is  $"<< GetIncome(Income,tax)   << endl;
    }
    else if (Income <=10000)
    {
        tax = 3; 
        cout << "The income tax owed is  $"<< GetIncome(Income,tax)  << endl;
    }
    else if (Income <=20000)
    {
        tax = 5.5;
        cout << "The income tax owed is  $"<< GetIncome(Income,tax)  << endl;
    }
    else if (Income <=40000)
    {
        tax = 10.8;
        cout << "The income tax owed is  $"<< GetIncome(Income,tax)  << endl;
    }
    else
    {
        tax = 23.7;
        cout << "The income tax owed is  $"<< GetIncome(Income,tax)  << endl;
    }
    The above way of doing things also eliminates the need for the NEWI variable in the main function. One other thing to mention, calling the function GetIncome seems a bit misleading, it seems it should be called GetTax instead.
    Last edited by hk_mp5kpdw; 11-17-2005 at 07:00 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    o ok thanks a lot man. i actually understand that. all i really needed to do was call the functions. i completely forgot all about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 05:37 PM
  2. DirectX9 compile errors
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 01-01-2006, 10:33 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

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