473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Decimals in C++?

The1corrupted
134 100+
Alright, I have issues with the following code and would like some insight as to why certain aspects do not work. Right now, I cannot get the program to float an appropriate decimal in order to come up with a percentage. It's only giving me 100% or 0%.

Expand|Select|Wrap|Line Numbers
  1. // Left or Right
  2. #include <iostream.h>
  3. #include <lvp\random.h>
  4.  
  5. //Declare variables
  6.  
  7. int Guess;
  8. int RL;
  9. int x;
  10. int c;
  11. int v;
  12. int r;
  13. int w;
  14. float p;
  15.  
  16. //Make random numbers (produce a 1 or a 2)
  17.  
  18. //-----------------------------------
  19. void GuessStuff() 
  20. { //Start GuessStuff
  21.     for (int z=1; z<=5; z++)
  22.     { //Start for
  23.     cout<<c+1<<". Guess left or right (1 = Right, 2 = Left): ";
  24.     cin>>Guess;
  25.     cout<<endl;
  26.     RL=random(2)+1;
  27.     cout<<RL<<endl;
  28.     if (Guess==RL) 
  29.     { //Start if
  30.         cout<<"    You got it right!  The answer was "<<RL<<endl<<endl;
  31.         r++;
  32.     } //End if
  33.     else 
  34.     { //Start else
  35.         cout<<"    You got it wrong!  The answer was "<<RL<<endl<<endl;
  36.         w++;
  37.     } //End else
  38.     c++;
  39.     } //end for
  40. } //End GuessStuff
  41.  
  42. //----------------------------------
  43.  
  44. int main () { //Start Main
  45.  
  46.     do
  47.     { //Start do
  48.         randomize();
  49.         cout<<"Main Menu"<<endl<<"1. Play the game"<<endl<<"2. Quit."<<endl<<"Answer? ";
  50.         cin>>x;
  51.         cout<<endl;
  52.  
  53.         if (x==1)
  54.         { //Start if
  55.         GuessStuff();
  56.         } //End if
  57.         else 
  58.         { //Start else
  59.             p=(r/c)*100;
  60.     //Problem Area--->>   cout<<"Good bye"<<endl<<"You got "<<r<<" right and "<<w<<" wrong."<<endl<<"You got "<<p<<"%"<<endl;
  61.         return (0);
  62.         } //End else
  63.  
  64.     } //End do
  65.     while (c==v); 
  66.     { //Start while
  67.         v=v+5;
  68.         return (main());
  69.     } //End While
  70.     return (0);
  71. } //End Main
Oct 2 '07 #1
3 1555
Ganon11
3,652 Expert 2GB
r and c are both integers, so when you do r/c, the compiler uses integer division, which returns an integer. You need to cast one of these to a float in the division, so that the compiler will use floating point division and return a floating point number. So, instead of

Expand|Select|Wrap|Line Numbers
  1. p=(r/c)*100;
use
Expand|Select|Wrap|Line Numbers
  1. p=((static_cast<float>(r))/c)*100;
in C++, or
Expand|Select|Wrap|Line Numbers
  1. p=(((float)r)/c)*100;
for C.
Oct 2 '07 #2
The1corrupted
134 100+
Aha! Thanks for the help :D We were all noobs at one point, right?
Oct 2 '07 #3
Ganon11
3,652 Expert 2GB
Definitely. I was in your exact position about 3 years ago, so don't worry - keep at it, and you can learn quite a lot.
Oct 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
0
by: thx | last post by:
I have MySQL 4.0.18 on Windows 2000 and I have a problem with floats and decimals. The table has floats and decimals fields but when I enter non-integers in these fields, they get rounded. So if I...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.