Hi All,

I Think I am close. I am trying to write a small user function that gives "roundCalc" to the main as a rounded number UP to the nearest 1000. (If 1011 should round up to 2000). I have "calcTotStat" reading in, and I thought I had it right, but my cout statement is returning 0.00. I know this is simple for all the experts, but after 10 hours yesterday on program 1 for class, and 3.5 hours today, I think my brain is fried.lol.

Thanks in advance,

#include <iostream>//I/o
#include <iomanip>//output manipulation library
#include <cctype>//function for character manipulation
#include <string>//string library
using namespace std;

//This finstion will round value from calculated line 6
//and will return a number rounded to nearest multiple of 1000
double round(double calcTotStat)// parameters for function

{
       double roundCalc;
       
       calcTotStat += .5;
       roundCalc = (int) (calcTotStat / 1000);
       roundCalc * 1000;

       
       return (roundCalc); // return to main
}

//body of program will get user inputs for processing and calculations
//output text to user for inqueries
    
    
int main()
{
    const double CHILD_ALLOWANCE = 1000;
    const int SINGLE = 75000;
    const int MARRIED_JOINT = 110000;
    const int HEAD_HOUSE = 75000;
    const int QUALIFY_WID = 75000;
    const int MARRIED_SEP = 55000;
    int children, fileStatus; 
    double childTaxPrevious, childAllow, remainCredit, grossAdjust, calcTotStat;
    double roundCalc;
    cout << "Enter the number of qualifying children: "; cin >> children; cout << endl;//ask for user input for children variable
    cout << endl;
    
//calculate for line 1
    childAllow = children * CHILD_ALLOWANCE;//calculation for number of children
    
//get amount from user for previous child tax credits
    cout << "Enter any Advanced Child Tax Credit amount\n"
    "(if none, enter 0): "; cin >> childTaxPrevious; cout << endl;
//calculate and determine user elgibility for child tax credits
//program exits with warning if conditions not met
//entry for line 3 if condition met    
    if (childTaxPrevious <= childAllow)
        remainCredit = childAllow - childTaxPrevious;//calculation for elgibility determination
        
    else {
        cout << "\tSTOP--You cannot take this credit\n"
        "\tYou have already received the maximum allowed"; cout << endl;//output to user if requirements not met
        cout << endl;
        
   system ("PAUSE");
   return 3;//program exit with code 3
}    
//get user input for line 4 adjusted gross
   cout << "Enter your Adjusted Gross Income (1040, line 35): ";
   cin >> grossAdjust; cout << endl;
   cout << endl;
   
// get filing status from user
    cout << "1 - Single"; cout << endl;
    cout << "2 - Married, Filing Jointly"; cout << endl;
    cout << "3 - Head of Household"; cout << endl;
    cout << "4 - Qualifying Widow(er)"; cout << endl;
    cout << "5 - Married, Filing Separately"; cout << endl;
    cout << "Enter your filing statis from above 91-5): ";
    cin >> fileStatus; cout << endl;//user input 1-5 for filing status
    cout << endl;

//detrmine arguments and calculations for line 6 based on answer from files status
    
    if (fileStatus == 1){
        calcTotStat = SINGLE - grossAdjust;
            
        round(calcTotStat);
}
    cout << fixed << showpoint << setprecision(2); 
    cout << roundCalc; cout << endl;

  
   cout << endl << endl;
   system ("PAUSE");
   return 0;
   
}

Recommended Answers

All 4 Replies

I think it should be:

// calcTotStat += .5;
roundCalc = (int) (calcTotStat / 1000);
if ((int) calcTotStat % 1000 > 0)
  roundCalc++;

roundCalc *= 1000;

I think it should be:

// calcTotStat += .5;
roundCalc = (int) (calcTotStat / 1000);
if ((int) calcTotStat % 1000 > 0)
  roundCalc++;

roundCalc *= 1000;

Thanks, I couldn't get this to return a value either.

I think my parameters are correct, but it just isn't working for me.

Cougraclaws

//This finstion will round value from calculated line 6
//and will return a number rounded to nearest multiple of 1000
double round(double calcTotStat)// parameters for function

{
       double roundCalc;
       
       calcTotStat += .5;
       roundCalc = (int) (calcTotStat / 1000);
       roundCalc * 1000;

       
       return (roundCalc); // return to main
}

Line 10 has no effect since you're calculating a value and not storing it anyway, so you can delete that line and it would make no difference.

Always be careful when using doubles in calculations like this. Depending on your exact needs, things can round off or truncate incorrectly. You are probably adding 0.5 for just that reason, but if that takes it past the 1000 mark, you could get a bad answer (i.e. 999.75 should round up to 1000, but when you add 0.5, you get 1000.25, which rounds up to 2000. Try adding 0.0000001 or something instead. Or perhaps add nothing. You'll have to decide.

You're going to run into problems if something is EXACTLY 1000, 2000, 3000, etc., again due to potential roundoff error, but other than that, your best bet is to truncate to an integer, then round down to the nearest thousand, then add a thousand:

#include <iostream>
using namespace std;


double roundUp(double calcTotStat)// parameters for function
{
       int roundDown = ((int) (calcTotStat) / 1000) * 1000;
       int roundUp = roundDown + 1000; 
       double roundCalc = roundUp;;
       return (roundCalc); // return to main
}


int main()
{
    cout << roundUp (1000.2) << endl;
    cout << roundUp (1000.0) << endl;
    cout << roundUp (999.99) << endl;
    cout << roundUp (500999.99) << endl;
    cout << roundUp (-999.8) << endl;

    cout << "Press any key to exit program\n";
    cin.get ();
 	return 0;
}

Note the incorrect result for when the parameter is exactly 1000.0. Since you have doubles, you always run into that problem with exact numbers. Also note the incorrect result for the negative number. You'll have to have slightly different logic for negative numbers due to the way the / operator works. but this is one way of doing it for positive numbers if you don't care about the problems involving exact multiples of 1000.


Note, you are mixing up the terms "rounding up" and "rounding off" in both the thread title and the comments in your code. I assume you want to always round UP.

Thank you this makes sense. It works well.

Cougarclaws

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.