Thread: Integer variable not working

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Integer variable not working

    This is a fairly simple program and everything was fine until i was asked to declare the variable "relays" as an integer after doing so the code does not give me the proper output but if i declare the variable as double it works just fine. what am i missing?

    Code:
    #include <iostream> //standard library for i/o#include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    
    int main()
    {
        string user_response = "y";
        string cell_num;
        double net_cost;
        double call_tax;
        double total_cost;
        double tax_rate;
        int relays = 0;
        int call_length;
    
    
        while (user_response == "y" || user_response == "Y")
        {
            //User Input
            cout << "Please input a cell phone number: ";
            cin >> cell_num;
    
    
            cout << "Please input the number of relay stations used: ";
            cin >> relays;
    
    
            cout << "\nPlease input the length in minutes of the call: ";
            cin >> call_length;
    
    
            
            
            //tax rate based on number of relays
            if ((relays >= 1) && (relays <= 5))
                tax_rate = .01;
            else if ((relays >= 6) && (relays <= 11))
                tax_rate = .03;
            else if ((relays >= 12) && (relays <= 20))
                tax_rate = .05;
            else if ((relays > 21) && (relays <= 50))
                tax_rate = .08;
            else if (relays > 50)
                tax_rate = .12;
    
    
            // callculations
            net_cost = ((relays / 50) * 0.40) * call_length;
            call_tax = net_cost * tax_rate;
            total_cost = net_cost + call_tax;
    
    
            //Output
            cout << "\nCell Phone:               " << cell_num;
    
    
            cout << "\nNumber of Relay Stations: " << relays;
    
    
            cout << "\nMinutes Used:             " << call_length; 
    
    
            // display cost with a 2 decimal places precision
            cout << setprecision(2) << fixed << "\nNet Cost:                 " << net_cost; 
    
    
            cout << setprecision(2) << fixed << "\nCall Tax:                 " << call_tax;
    
    
            cout << setprecision(2) << fixed << "\nTotal Cost of Call:       " << total_cost;
            
            cout << "\n\nWould you like to do another calculation (Y or N): \n" << endl;
            cin >> user_response;
        }
    
    
        return  0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your problem is due to "integer division" where the fractional part of the result of a division is discarded. E.g., 27 / 50 is 0 whereas 27.0 / 50.0 is 0.54.

    You can leave relays as an int and fix your problem like this:
    Code:
            net_cost = ((relays / 50.0) * 0.40) * call_length;
    Changing 50 to 50.0 forces the division to be done in floating point instead of integers (relays will be automatically converted to a floating point for the calculation).

    Your if/else if's can be simplified like this:
    Code:
            if (relays <= 5)
                tax_rate = .01;
            else if (relays <= 11)
                tax_rate = .03;
            else if (relays <= 20)
                tax_rate = .05;
            else if (relays <= 50)
                tax_rate = .08;
            else
                tax_rate = .12;
    BTW, unless "callculations" is a pun, you have an extra letter 'l' there!

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Thanks for the help and the explanation it will surely help me in the future also not a pun just really tired and didnt noticed :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String in integer variable
    By briangriffin in forum C Programming
    Replies: 2
    Last Post: 11-06-2011, 12:06 PM
  2. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  3. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  4. Check if variable is integer
    By Adoro in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2008, 09:26 AM
  5. pointer variable to integer?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 05:06 PM

Tags for this Thread