Conversion help

Recommended Posts

IS there any way I can make the program do the conversion here? I tried changing everything to double but it just wouldn't work. Too many int functions. I need this to do the double conversion and then back to int. fin = (87/100)* 50; Gotta do some more reading but again I would appreciate some input.
#include <iostream>

using namespace std;

int main() {
double fin;

fin = (87/100)* 50;

cout << "\n\t" << fin <<"\n\n\n";

system("PAUSE");
return 0;
}



Share on other sites
try this:
#include <iostream>#include <cstdlib>   // for your system("PAUSE")using namespace std;int main() {double fin;fin = (87.0/100.0) * 50.0;cout << "\n\t" << (int)fin <<"\n\n\n";system("PAUSE");return 0;}

also, your question is not too clear. you have no functions or int functions in your program. so how exactly are you trying to make this work?

Share on other sites
Ok if you compile this section it will work. The results are incorrect as the lack of conversion causes a return of zero. I can't type in the decimal. I need to somehow make a conversion to double and then back. I marked it on the code.

#include <iostream>#include <stdlib.h>#include <windows.h>#include <ctime>#include <vector>#include <algorithm>#include <string>using namespace std;int i, J=0, J2=0, J3=0 ;int DICE(int n, int x);int TOP_S(int tssc);int PER_ACC(int ts, int per);int T,T2,T3;int main() {srand(time(0));// RAce start---------------------------cout << "\t\tAND THEY'RE OFF\n\n";for(i=0;i<9; i++){// first leg of raceif(i==1){ T = PER_ACC(85,50);// Horse 1 T2 = PER_ACC(80,60);// Horse 2 T3 = PER_ACC(75,70);// Horse 3cout << " " << T <<" ";cout << " " << T2 <<" ";cout << " " << T3 <<" "; }Sleep(200);// lags 2 - 5 trying to add 5% to performance per legif(i<6 && i>1){T =  (T/100)*105;T2 =  (T/100)*105;T3 =  (T/100)*105;cout << " " << T <<" ";cout << " " << T2 <<" ";cout << " " << T3 <<" ";}}system("PAUSE");return 0;}// Top speed functionint TOP_S(int tssc) {bool zerox = tssc%6;int roll,smdi, d1,d2;roll = tssc/6;d1 = DICE(roll,6);if(zerox){smdi = tssc%6;d2 = DICE(1,smdi);return d1+d2;}else{return d1;}}// dice function. example: a call of DICE(2,4) is like rolling two 4 sided dice.int DICE(int n, int x){ int i, score = 0;  for (i = 1; i <= n; i++){ score += rand()% x + 1;} return score; }// Performance this takes both top speed argument and acceleration argument and returns performance.int PER_ACC(int ts, int acc){int fin;fin = (TOP_S(ts)/100)* acc;/*<-----problem is here the division needs to be done as a decimal but after the multiplacation it needs to go back to int.*/return fin;  }

Share on other sites
How about you break it down some to achieve what others suggest in your last post.

Now you need to divison to be a "float". You are aware that TOP_S returns an "int". So you must type cast it.
float temp1 = (float)TOP_S(ts) / 100.0f;

Now that you have a float, you can use it how you want it to be.
fin = temp1 * acc;

Since acc is an "int", and temp1 is a "float", the result will be an "int", fin is also an "int" so we are a-ok.

Do you kind of see the process now? A key thing to remmeber is to break it down into litter steps, so you can control the flow of the program and determine what is conveted into what. Do you understand or need more explanation? [smile]

- Drew

I also noticed that you will need to do one more conversion. It lies in this block of code. once you fix it, the last digits will not be 0.
// lags 2 - 5 trying to add 5% to performance per legif(i<6 && i>1){T =  (T/100)*105;T2 =  (T/100)*105;T3 =  (T/100)*105;

Here's a hint, remember what has been said about dividing by a float [wink]
Here was one final output for me (even though I think you have a few logic errors)
                AND THEY'RE OFF 25  34  30  26  27  27  27  28  28  28  29  29  29  30  30

Share on other sites
I believe you're approaching this from the wrong angle. To me the logical way to do this kind of thing is to do the multiplications BEFORE the divisions. This eliminates the very slow int<->float conversions too.
T =  (T/100)*105;fin = (TOP_S(ts)/100)* acc;fin = (87/100)* 50;
You do this:
T =  (T * 105) / 100;fin = (TOP_S(ts) * acc) / 100;fin = (87 * 50) / 100;

There's also a very easy way to round instead of truncate should you want to:
T =  (T*105 + 50)/100;fin = (TOP_S(ts) * acc + 50) / 100;fin = (87 * 50 + 50) / 100;
[/code]

Share on other sites
I understand 90% of what everyone is saying but I also know there is a way to cast so that my math problem works as is. fin = (87/100)* 50; I have to learn how to cast so this works as there will be times when it will be necessary.

I think my game is a bit too much for me to handle. I guess it's back to the books for another couple weeks :/

I really appreciate all of the input and help.

Thanks:)

p.s.
I have thought advanced guitar lead for over 15 years now. When I start teaching the guitar I find that if I don't give the student small rewards along the way they often get discouraged and quit. This is why I wanted to make a small cool game to reward my studying over the past month. As thus far C++ has been nothing but learning boring rules via books. I can see the reward will be awesome but man learning this makes learning Eddie Van Halen's
Eruption solo look like Cat in the Hat.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628345
• Total Posts
2982187

• 9
• 24
• 9
• 9
• 13