+ 2

How can I change between variable types?

Please bear in mind that I am a newbie so this is me experimenting. I have written a small game that generates a random die roll which accumulates. After each roll, the player then gets the choice to roll again or stop. If at any time a 6 is rolled, the total is reset to 0 and the turn over. I have that all working fine. Now I am.trying to add a points system where thr score given = total / 10. however, I am struggling to go from a total of 24 to a score of 2.4 It must be possible! Thank you.

31st Jul 2017, 2:34 AM
Richard Appleton
Richard Appleton - avatar
9 Respostas
+ 11
copy and paste into code playground on your pc? but what i posted before should work. int total = 24; // player rolls float score = float(total) / 10.0;
31st Jul 2017, 2:51 AM
jay
jay - avatar
+ 10
float result = float(total) / 10.0; Will this work?
31st Jul 2017, 2:41 AM
jay
jay - avatar
+ 10
can u show us your code?
31st Jul 2017, 2:47 AM
jay
jay - avatar
+ 8
According to the details provided in your question, @Jay's suggestion should work perfectly.
31st Jul 2017, 4:32 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
I have this as an app on my phone. I will try your code tomorrow and let you know. It is.4.am in UK and I have to go to bed!
31st Jul 2017, 3:06 AM
Richard Appleton
Richard Appleton - avatar
+ 2
I have my total as an Int. And set up a new variable for the score as a float. Then I can do this: cout << "Your total is: " << total<< " which scores "<< score; I know there will be other ways around this, but ai want to learn how to do it as oart of my experimenting. Thanks.
31st Jul 2017, 2:45 AM
Richard Appleton
Richard Appleton - avatar
+ 2
Is there an easier way than typing it all in here?
31st Jul 2017, 2:49 AM
Richard Appleton
Richard Appleton - avatar
+ 1
@Jay. Worked perfectly! Thank you. :-) So does float(Total) temporarily change the variable from int to float?
31st Jul 2017, 5:34 PM
Richard Appleton
Richard Appleton - avatar