473,666 Members | 2,038 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 1168
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
4703
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 gc, and not major gc? Thanks. -ekl
6
1856
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 great benefit from it? What does ELSE minor-mode for Emacs do that is so great for you? Chris
0
1847
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 above coding header I get frequent syntax errors with files that work flawlessly under Linux. The Syntax error usually appear near continuation lines of the form
144
6845
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 initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
4
2478
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 DateTime variable set to Date of Birth If (DateTime.Now.Subtract(dteDOB.Value.AddYears(18)).TotalDays >= 0) Then minor = false End If This appears to work, but seems a little obfuscated. I was just wondering
9
1501
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 sites. For instance: When I create a template or region editing control. Why doesn't the region or template pick up the attributes of the container I'm editing (and preferably staying in the flow of the page)? Why does this matter? Well, if I...
3
2002
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 run the query, only certain data is selected and exported onto a separate database that I can easily upload onto our ftp server. It has been working fine for all year until the past few weeks I have run into some problems. Initially when I...
19
3961
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. Framework-Specific Guidelines Naming Conventions and Styles
0
882
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 different table and inserts on the table I just created (see below). However, I continue to get an Interface Error (see below) when i run it. And yet, if I allow the code to call createTables() and then manually call selectSQL() after a couple...
6
1388
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 of names that will be tried before giving up. template - the default prefix for all temporary names. You may change this to control the default prefix.
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.