473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Minor Coding error

11 New Member
#include<stdio.h>

#define SHIP_COST 7.00
#define FREE_SHIP_LIMIT 25.00

main ()
{
float BP = 0;
float numBooks=0;
float Avg = 0;
float Total = 0;


while (BP != -1)
{
printf("please enter the price of book: %d", BP);
scanf("%d", &BP);

Total += BP * 1.05;
numBooks++;
}

Avg= Total/numBooks;
printf("the average is %f", Avg);
printf("The total price is %f", Total);
}


given my code, when i compile it, i get a very very weird output. it'll print out the first printf but with a 0 infront of it. Also, when i run the code and enter "-1" the loop doesnt end and sometimes, it infinitely prints out" please enter the price of book" Please help as much as possible. I've never had this problem before.
Apr 25 '08 #1
2 1161
Meetee
931 Recognized Expert Moderator Contributor
#include<stdio.h>

#define SHIP_COST 7.00
#define FREE_SHIP_LIMIT 25.00

main ()
{
float BP = 0;
float numBooks=0;
float Avg = 0;
float Total = 0;


while (BP != -1)
{
printf("please enter the price of book: %d", BP);
scanf("%d", &BP);

Total += BP * 1.05;
numBooks++;
}

Avg= Total/numBooks;
printf("the average is %f", Avg);
printf("The total price is %f", Total);
}
In bold there are few errors which are really minor but create big issues if not recovered!! float xyz should be initialized to 0.0 (float xyz = 0.0) and while in printf and scanf you need to write %f instead of %d so correct them.

Regards
Apr 25 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
float xyz should be initialized to 0.0 (float xyz = 0.0)
.

Almost correct. 0.0 is a double. A float should be initialized using 0.0f.
Expand|Select|Wrap|Line Numbers
  1. float xyz = 0.0f;
  2. float abc = 0.0F;
  3.  
Also:
while (BP != -1)
will not work as you desire. Not even using -1.0F.

Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
Due to the automatic rounding, these operators may report a condition as true when the numbers
are only close in value.

Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

To compare two floating point numbers, you have to establish a sigma error tolerance.

Expand|Select|Wrap|Line Numbers
  1. if (fabs(i - j) < 0.000001) { ... // almost equal }
  2.  
Apr 25 '08 #3

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

Similar topics

4
4696
by: EKL | last post by:
What do I have to do to guarantee that the objects that my code creates are reclaimed by only minor garbage collection runs? In other words, what do I have to do to only generate garbage for minor...
6
1851
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
0
1845
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
144
6735
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
4
2471
by: Michael C# | last post by:
Given a DateTime variable, what's the best way to check "minor" status (i.e., less than age 18 years) based on today's date? I came up with this: Dim minor As Boolean = true 'dtDOB is a...
9
1490
by: Larry Charlton | last post by:
I've begun trying to create some basic applications and controls and am left wondering if Microsoft rushed this to market, is just anti-web standards, or just wants it to be hard to create great...
3
1990
by: juliehg | last post by:
Hi, I'm currently using MS Access 2000 to keep all my actors measurements, resumes and personal details on a database. To upload their details onto our website, I have built an event so when I...
19
3941
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
0
875
by: Greg Corradini | last post by:
Hello all, In a script i just wrote, my code calls a function createTables(), which checks for an existing table and then creates it, and then immediately calls selectSQL(), which selects from a...
6
1374
by: James T. Dennis | last post by:
Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says: This module also provides some data items to the user: TMP_MAX - maximum number...
0
7220
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
7105
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
7308
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
7479
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
5617
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,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.