简体   繁体   中英

Having trouble with simple c++ program using decimals.

//Purpose: To calculate the price to pay an author
//Programmer: Brandon C Ballard
//Last Updated: 2/20/2014

#include <iostream>
#include <cstdlib>

using namespace std;

//function prototype
float TotalPay(int numWords, char Level);

int main()
{
    float TotalPay;
    int numWords;
     char Level;
    int amtToPay;

    cout << "Please enter number of words: ";
    cin >> numWords;
    cout << endl;
    cout << "Please enter a Level, A,B, or C: ";
    cin >> Level; cout << endl << endl;

    //calculate price per word
    if (numWords < 7500)
    amtToPay = numWords * .08;
    else if (numWords >= 7500 && numWords < 8000)
    amtToPay = 600;
    else if (numWords >= 8000 && numWords < 17500)
    amtToPay = numWords * .075;
    else if (numWords >= 17500 && numWords < 19000)
    amtToPay = 1313;
    else 
    amtToPay = numWords *.07;

    //calculate the Level of the author
    if (Level == 'C' or Level == 'c')
    Level = 1;
    else if (Level == 'B' or Level == 'b')
    Level = 1.25;
    else if (Level == 'A' or Level == 'a')
    Level = 1.75;

    TotalPay = amtToPay * Level;
    cout << "Length of Story (words): "; cout << numWords; cout << endl << endl;
    cout << "Amount Due Author: "; cout << "$"; cout << TotalPay; cout << endl << endl << endl;

    system("PAUSE");
    return 0;
}//end main function

My instructor wants me to write a program that can calculate the amount of money to pay an author who is submitting an article to a magazine. The amount of money to pay the author is based off of how many words are in the article. It works like this...

-if length (in words) is less than 7,500: the author gets paid $0.08 per word.

-if length is 7,500 to 8,000: the author gets paid a fixed $600.

-if length is 8,000 to 17,500: the author gets paid $0.075 per word.

-if length is 17,500 to 19,000: the author gets paid a fixed $1313.

-if length is greater than 19,000: the author gets paid $0.08 per word.

Also: There are three different "Levels" of authors (A,B, and C). A "C" Level author (new author) would get paid based on the information above. A "B" Level author (established writer) would get paid 1.25 times the amount of a Level C author. An "A" Level author (rockstar) would get paid 1.75 times the amount of a Level C author.

The Math: Basically, I wrote the program so that it first calculates the amount to pay the author (amtToPay). Then, it calculates what the (Level) is equal to. Then the (TotalPay) is the (amtToPay) multiplied by the (Level).

My Problem: Everything works great except for the part where it //calculates the Level of the author. For example, if I were to input the author as an "A" Level, he should get paid 1.75 times that of a Level C author. So, it should multiply the (amtToPay) by 1.75, except what is actually doing is multiplying it by "1" and is ignoring the ".75".

I am new to programming and I understand that there are probably many other ways to write this. But please try and help me the best that you can. Thank you.

Level is an integer type so when you assign the floating point numbers to it, the fractional parts are dropped.

Try defining double rateLevel and then

if (Level == 'C' or Level == 'c')
rateLevel = 1;
else if (Level == 'B' or Level == 'b')
rateLevel = 1.25;
else if (Level == 'A' or Level == 'a')
rateLevel = 1.75;

TotalPay = amtToPay * rateLevel;

The proper way to do this is to not use floating point types at all, except maybe for printouts. The way to accomplish this is to scale everything by a power of ten that will remove all fractional components from your values. In your code the least significant digit is in the thousands place (0.075). This means that you need to multiply all your values by 1000. This is called your scale factor. Then you can do your math using only integral types, int , long , int64_t , etc. At the end of your calculations you can split the results into whole number and fractional components.

Like this:

int TotalPayDollars = TotalPay/1000;
int TotalPayMilliDollars = TotalPay - 1000*TotalPayDollars;
int TotalPayCents = (int)((double)TotalPayMilliDollars/10 + 0.5);

The first line is all integer math so the dividing by 1000 discards any fractional parts.

The second line finds the difference between your original value and the truncated value. We multiply TotalPayDollars by 1000 to bring it into the same units as TotalPay again.

In the last line the + 0.5 works to round up to the nearest cent.

NOTE: when choosing a scale factor it is very important to make sure that you don't overflow your integer type. A 32 bit signed integer can only hold numbers up to 2^31-1 (2,147,483,647). If any of your calculations will go higher than that value then you should use a 64 bit integer type.

Level is type char which is integral, you should create a new variable specifically to hold the amount boosted by level:

double level_boost;
//logic
Totalpay = amtToPay * level_boost;

PS: in your logic, you do not have to use && :

if (numWords < 7500)
amtToPay = numWords * .08;
else if (numWords < 8000) 
//if the numwords is less than 7500 will be handled by first if
amtToPay = 600;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM