473,382 Members | 1,202 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,382 software developers and data experts.

Why does C++ give the wrong answer

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std; 
  3. int main ()
  4. {
  5.    int numberOfStems=100;
  6.    float lowMark=1/4;
  7.    cout << "Low Mark= " << lowMark <<endl; //This outputs 0
  8.    cout << "Pass Mark=" << 40*numberOfStems/100 << endl; //This outputs 40
  9.    return 0;
  10. }
  11.  
I am using VS8 and compile and run with Ctrl+F5
Sep 18 '10 #1
4 2516
Banfa
9,065 Expert Mod 8TB
It doesn't in both cases it outputs the correct number or rather the number you asked it to calculate.

Simple case first

40*numberOfStems/100 where numberOfStems = 100

40*100/100 = 40

The case that is probably confusing you

float lowMark=1/4;

You have chosen to store the result in a float (best practice is to use double rather than float unless you have an communicable reason why floats should be used) because you are expecting a fraction, that is good.

However you have written 1/4 both 1 and 4 are integers so it does integer arithmetic under integer arithmetic 1/4 = 0. Having calculated 0 it converts it to a float for storage.

You need to force it to do floating point arithmetic, the easiest way to do that is to use floating point constants rather than integer constants like this

float lowMark=1.0/4.0;
Sep 18 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. float lowMark=1.0/4.0; 
Actualy, 1.0 and 4.0 are double. This will prompt a warning frim the compiler about possible loss of accuracy when string in the smaller float.

You should code:

Expand|Select|Wrap|Line Numbers
  1. float lowMark=1.0f/4.0f; 
Sep 18 '10 #3
Banfa
9,065 Expert Mod 8TB
If we are going to get picky then since we all agree that you should use double rather than float you should code
Expand|Select|Wrap|Line Numbers
  1. double lowMark=1.0/4.0;
:p
Sep 18 '10 #4
Thank you to weaknessforcats for giving me a reply.
Sep 19 '10 #5

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

Similar topics

5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
18
by: free2cric | last post by:
Hi, I attanded an interview on C++ Question asked were and my answers to them.. 1. In a CPP program what does memory leak occure? -- i said.. In a constructor , the pointer variables were...
6
by: peter pilsl | last post by:
postgres 7.3.2 I store unicode-data in postgresql. The data is retrieved via webinterfaces, processed with perl and then stored in postgresql (and viceversa). All is going nice with one...
1
by: VBSponge | last post by:
Hi all. In A2K i have a report with a control bound to to calculate the page count for the report. I need to retrieve this page count from the report, and build it into a TOC containing this an...
6
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea...
14
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- ...
13
by: llothar | last post by:
On windows everything is '.pyd' but there seems to be two ways to get this on unix? Why and what is the rule?
8
by: .nLL | last post by:
<%=2/33334%> returns 5.99988000239995E-05 how can i convert it to a n.nn ?
42
by: lorlarz | last post by:
Contrary to what one authority in the JavaScript field says: JavaScript does make errors when dealing with just with integers. This authority (Douglas Crockford.) says: "integer arithmetic in...
1
by: Marc Gravell | last post by:
Check the server's event log. Also - did you apply the change at both client and server? Personally, I avoid all of these extensions - they don't make for an easily scalable service if you want...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.