0

I cannot understand why the results of my division appear rounded down?

#include <iostream>
#include <stdio.h>
using namespace std;

int rVote, oVote, dVote;
int rTotal, oTotal, dTotal, total;
int rPercent, dPercent, oPercent;

bool loop(char vote){
        switch(toupper(vote)){
                case 'D':
                    dVote = 1;
                    dTotal = dTotal + dVote;
                    return true;
                case 'R':
                    rVote = 1;
                    rTotal = rTotal + rVote;
                    return true;
                case 'O':
                    oVote = 1;
                    oTotal = oTotal + oVote;
                    return true;
                case 'Q':
                    return false;
        }
        return true;
}
int main(){
        char vote;
        do{
                printf("Enter Vote [D/R/O] or Q to quit: ");
                scanf("%c%*c", &vote);
        } while (loop(vote));
        total = dTotal + rTotal + oTotal;
        rPercent = ((rTotal/total)*100);
        dPercent = ((dTotal/total)*100);
        oPercent = ((oTotal/total)*100);
        printf("Democrate total vote %d: %d%%\n", dTotal, dPercent);

        printf("Republican total vote %d: %d%%\n", rTotal, rPercent);

        printf("Other total vote %d: %d%%\n", oTotal, oPercent);
}

Great I am being really dumb.... Why is it that any of my *Percent's not printing the % value in this c/c++ program?

Thanks.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • 2
    This is a not a C/C++ program. It's a only C++ program because it won't compile as C. However, it is written as if it were a C program. You should pick one of the two and learn it, instead of learning some kind of mixture between the two. Despite the appearances, they are very different languages. If you decide to go with C++, I'll recommend [a good introductory C++ book](http://stackoverflow.com/q/388242/46642). – R. Martinho Fernandes Oct 19 '11 at 03:46
  • I want to be a C programmer, sort of picked up C first, But in a C++ programming class atm. The teacher doesn't seem to mind. So mainly why i write all my code in c format per say. – John Riselvato Oct 19 '11 at 04:30

2 Answers2

4

It's because you're doing integer division. Integer division in C/C++ rounds down. So your following code:

    rPercent = ((rTotal/total)*100);
    dPercent = ((dTotal/total)*100);
    oPercent = ((oTotal/total)*100);

is all rounding down to 0.

To fix this, you should cast to a floating-point type:

    rPercent = (int)((double)rTotal/total*100);
    dPercent = (int)((double)dTotal/total*100);
    oPercent = (int)((double)oTotal/total*100);

EDIT:

The code above could give some weird results due to rounding behavior. Perhaps something like this might be more appropriate since it rounds to the nearest %:

    rPercent = (int)((double)rTotal/total*100 + 0.5);
    dPercent = (int)((double)dTotal/total*100 + 0.5);
    oPercent = (int)((double)oTotal/total*100 + 0.5);
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • Interesting. Thank you. I didn't know you could write it like this. I'll make this the answer after my 10 minute wait is over. glad i asked the question, i like this fix better then what i remember how i did it a while ago. – John Riselvato Oct 19 '11 at 03:35
  • converting to double is overkill though....if you simply multiply by 100 first, you don't have to cast back and forth – Keith Nicholas Oct 19 '11 at 03:37
  • It isn't strictly necessary to use floating point math here, you just would want to reduce the value (divide) at the end of the calculation rather than the beginning. – Jeff Mercado Oct 19 '11 at 03:37
  • @Keith Nicholas: Yeah, maybe in this case it is. There's a lot of ways to do this and yours works too - provided you don't run into integer overflow. – Mysticial Oct 19 '11 at 03:38
  • The casts to `int` are unnecessary. Since the targets of the assignments are of type `int`, the value will be converted implicitly. – Keith Thompson Oct 19 '11 at 04:19
  • @Keith Thompson: I get a compiler warning in Visual Studio if I don't cast. But other than that, you're correct, it's not necessary. – Mysticial Oct 19 '11 at 04:21
1

because you are zeroing them first :)

do this for each of the percent

dPercent = ((dTotal*100)/total);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156