Citruscodus
Citruscodus

Reputation: 1

Why am I getting Warning: C4244?

So I've been getting: Warning C4244 '=': conversion from 'double' to 'long', possible loss of data line 158. From what I understand is that 'pow' and 'long int result' are somehow connected to this, I have been messing around with it and changed 'long int result' to 'double result' and got rid of the warning. I have a solution to get rid of the warning, but it won't matter since this program need long int to handle more data otherwise it will overflow if I use double.

If I decided to keep this warning in the program will there be potenial issues with it? Can I somehow get rid of the warning and still be able to use 'long int result' or at least be able to handle more data some other way?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    char menu, choice; 
    int numberYears = 0;
    long int deposit, withdraw;
    long int result = 0;
    long int annualDeposit = 0;
    float interestRate = 0.0;   
    long int balance = 0;
    cout << "------------------------------\n" << "Welcome to Bank Simulator 3000" << "\n------------------------------\n\n"; 

    while (true) 
    {
        cout << "XXXXXXX[MENU]XXXXXXXX\n";
        cout << "[D]eposit\n";
        cout << "[W]ithdrawal\n";
        cout << "[B]alance\n";
        cout << "[I]nterest payment\n";
        cout << "[E]xit\n";
        cout << "XXXXXXXXXXXXXXXXXXXXX\n";
        cin >> menu;



        switch (menu) 
        {
        case'd':
        case'D':
            cout << "\n[DEPOSIT]\n";
            cout << "Do you want to make a deposit?\n" << "Y/N\n";
            cin >> choice;
            if (choice == 'Y' || choice == 'y') 
            {
                cout << "\nHow much do you want to deposit?\n" << "\n:";
            }
            else
            {
                cout << "\nReturning to menu.\n\n";
                continue;
            }
            cin >> deposit;
            balance += deposit; 
            cout << "\n" << deposit << " Kr has been added to balance.\n\n";
            continue; 
        case'w':
        case'W':
            cout << "\n[WITHDRAWAL]\n" << "Do you want to make a withdrawal?\n" << "Y/N\n";
            cin >> choice;
            if (choice == 'Y' || choice == 'y')
            {
                cout << "\nHow much do you want to withdraw?\n" << "\n:";
            }
            else
            {
                cout << "\nReturning to menu.\n\n";
                continue;
            }
            cin >> withdraw;
            if (withdraw == 0)
            {
                cout << "\nYou withdrew 0 amount. No changes will apply.\n\n";
                continue;
            }
            balance -= withdraw; 
            cout << "\n" << withdraw << " Kr has been drawn from balance.\n\n";
            continue;
        case'b':
        case'B':
            cout << "\n[BALANCE]\n";
            cout << balance << " Kr\n\n"; 
            continue;
        case'i':
        case'I':
            cout << "\n[INTEREST PAYMENT]\n";
            cout << "Do you want to calculate your interest payment?\n" << "Y/N\n";
            cin >> choice;
            if (choice == 'Y' || choice == 'y')
            {
                cout << "What's your annual deposit?\n" << ":";
            }
            else
            {
                cout << "\nReturning to menu.\n\n";
                continue;
            }

            cin >> annualDeposit;
            if (annualDeposit == 0) 
            {
                cout << "You typed 0 in your annual deposits, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
                cin >> choice;
                if (choice == 'Y' || choice == 'y'); 
                    
                else
                {
                cout << "Returning to menu.\n\n";
                continue;
                }

            }
            cout << "What's Your Interest Rate?\n" << ":";
            cin >> interestRate;
            if (interestRate == 0) 
            {
                cout << "You typed 0 in your interest rate, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
                cin >> choice;
                if (choice == 'Y' || choice == 'y');
                    
                else
                {
                    cout << "Returning to menu.\n\n";
                    continue;
                }

            }
            cout << "How many years do you want to save to?\n" << ":";
            cin >> numberYears;
            if (numberYears <= 0)
            {
                cout << "You typed 0 or less in number of years, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
                cin >> choice;
                if (choice == 'Y' || choice == 'y');
                    
                else
                {
                    cout << "Returning to menu.\n\n";
                    continue;
                }

            }
            result = annualDeposit * pow(1 + interestRate / 100, numberYears); 
            cout << "Your Interest Payment Will Be: " << result << " Kr In " << numberYears << " Years\n\n";
            continue;


        default:
            cout << "\nPlease use the following example\n" << "D = Deposit | W = Withdrawal | B = Balance | I = Interest payment | E = Exit\n\n";
            continue;

        case'e':
        case'E':
            cout << "Thanks for using Bank Simulator 3000!\n";
            cout << "Press any key to close";
            system("pause>0");
            break;
        }
        break;
    }
    return(0);
}


Upvotes: 0

Views: 3059

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

In typical environment, a double variable can store a floating-point number upto about 10^300 (assuming IEEE754 64-bit is used).

On the other hand, a long int can store an integer only upto about 10^9 (32-bit) or 10^18 (64-bit).

Therefore, the maximum value to handle by long int is much smaller than one for double. This is why conversion from double to long int can cause loss of data.

You can add an explicit cast to suppress the warning.

result = static_cast<long int>(annualDeposit * pow(1 + interestRate / 100, numberYears));

Upvotes: 1

Related Questions