I am having a problem trying to divide a number. So in my in my program I have a number and I want it to divide it by 10. The problem is that the result is going to be decimal. I run my program I put the number it divdes it by ten but it shows me a number: 197844384.600000. It shows me that number all the time, it doesn't matter if I change the numbers. I am using:

printf( "%f\n", number/10)

to show the number.

Thank You,

C-+

Recommended Answers

All 19 Replies

More code please. Preferably a full test program that exhibits the problem.

#include <iostream>

using namespace std;

int profit_expenditure(int rent, int electricity, int telephone, int internet, int salary, int profit){
    
    int expenditureprofit;
    
    expenditureprofit = profit-rent-electricity-telephone-internet-salary;
    
    return (expenditureprofit);
}
     

int main()

{
    // int profit_expenditure= profit - expenditure, int gg= ganancia,
    int rent;
    int electricity;
    int telephone;
    int internet;
    int salary;
    int profit;
    float maaser = profit_expenditure(rent, electricity, telephone, internet, salary, profit)/10;

    
    
    cout<<"Welcome to my program \n";
    
    cout<<"Write your profit: \n";
    cin>>profit;
    
    cout<<"Put the cost of your rent: \n";
    cin>>rent;
    
    cout<<"Put the cost of the electricity: \n";
    cin>>electricity;
    
    cout<<"Put the cost of the telephone: \n";
    cin>>telephone;
    
    cout<<"Put the cost of the internet: \n";
    cin>>internet;
    
    cout<<"Put the salary you give to your workers: \n";
    cin>>salary;
      
    cout<<"Your profit is: ";
    cout<<int profit_expenditure(rent, electricity, telephone, internet, salary, profit);

    
    cin.get();
    
    cout<<"\nPress Enter to calculate the 10%";
    cin.get();
    printf("The total is: %f\n", maaser);
          
    
    cin.get();
    
    
    
    return 0;
    
}

How about doing your calculation AFTER you've input all the parameters which the calculation depends on.

You could you make an example, I don't understand. Sorry but I just starting.

Thank You,

C-+

You're trying to do

int value;
int result = calculate(value);
cin >> value;

What you need to do is this

int value;
cin >> value;
int result = calculate(value);

Statements happen in the order you write them.

Ok, Thank you very much Salem. Will try to find it on my program

Thanks Salem it worked, but when the result is for example is 124 and then the program divides it by 10 the result is 12.0000. Why? The result should be 12.400

Do you realize that you're using int as a data type? Try using double or float

Thanks Salem and neosomosis. Its working excellent.

A quick reminder....

Try not using int as a data type when it comes to salary, price, etc. Use double instead.

Thanks. But why do you say that double its better?

Don't worry I just found why double is better. Thanks

No problem, dude....Glad to help ;)

well just to add something...

if u have already used a particular data type for somthing and in the middle of ur program u want to use other one for that (just for a particular use) ...then u can use casting ....
i.e. changing one data type into other momentarily...

commented: Helpful +1

Thanks rajat. Good suggestion.

what if you have to have the two input values as intigers, not allowed to classify them as floating or double? i can change the result classification thu.

..we have to divid two intigers (int) but still get an accurate answer.
ex: 10/3, is 3 with int
but there should be ways to make it =3.3333.
or 8/3 to be 2.666

what if you have to have the two input values as intigers, not allowed to classify them as floating or double? i can change the result classification thu.

..we have to divid two intigers (int) but still get an accurate answer.
ex: 10/3, is 3 with int
but there should be ways to make it =3.3333.
or 8/3 to be 2.666

absolutely you can do this. As was stated previously, you can explicitly tell C++ to hold onto the decimal portion. For example:

int n1 = 10;
int n2 = 3;

cout << (double)n1/n2 << endl;

//Note:  (double)n1/n2 is the same as ((double)n1)/n2

We have declared two integer variables. Then, before we calculate n1/n2, we explicitly "cast" n1 to be of type double (i.e we temporarily convert it to a double data type). So now n1 is of type double (temporarily) and n2 is an integer. We now have "double"/"integer", and C++ automatically keeps the most degree of accuracy it needs. I.e., now we will display information to the accuracy of a "double" data type.

However, if we had done this:

int n1 = 10;
int n2 = 3;

//note the parentheses around n1/n2 this time
cout << (double) (n1/n2) << endl;

The calculation n1/n2 would have been done first, i.e. "integer/integer", so C++ will throw away the decimal portion and only keep integer accuracy. Thus we would be left with 3, and this number 3 would then be converted to a double data type.

thanks, i didn't know about inserting (double)in front, I tried multipling num1 by 1.0, and it also resulted in a decimal.

also, this is not in assignment but what if i want to make it more percise, in the sense of repeating decimals? i.e. intead of 3.3333, have it 3 1/3 but only making a fraction when the result is more accurate as a fraction.

Well, you could use the idea of integer division, i.e. a|b ("a divides b"), and remainders...

So for example, we say "3 divides 10" 3 times, with a remainder of 1. So you could take the floor of your standard division, floor(10/3) = 3, i.e. just cast the answer as an integer, like (int) (10/3), and then use the idea of modulus to find the remainder (and thus the numerator of your fraction, where the denominator is simply the divisor...)

EDIT: Note that this thread was actually over 4 months old, so perhaps next time it is better to start a new thread...

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.