Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-11-2004, 08:47 PM   #1 (permalink)
B00tleg
Registered User
 
B00tleg's Avatar
 
Join Date: Oct 2004
Posts: 9
B00tleg is on a distinguished road
Unhappy Need help on program

This assignment has me working with IF and ELSE statements and using them to validate certain ranges of variables and then to display an appropiate message if a variable is outside of a given range (i.e) "that number is not correct". I have written the program and compiled it. I'm trying to make the number zero invalid and able to be input in the program. However if you hit zero in the program it will display both the message for the invalid number and also the other message for any other variable that is entered and is valid.

Here is the source code. I've been beating my head trying to figure out how to make it just display the one message when zero is typed in. Well I gotta take a break and I'll be checking back in a while. Thanks for any help in advance.



Code:
#include <iostream> #include <cstdlib> using namespace std; int main() { float discount1 = .020; // amount of discount float discount2 = .030; float discount3 = .040; float discount4 = .050; float quantity; // # of packages float totalCostOfPurchase; //final price float price = 99; //price per package cout << "Please enter the number of packages being sold. " <<endl; cin >> quantity; // determine discount based off of number of packages sold if (quantity <= 0) cout << "This amount is incorrect, please enter one or more to get the correct price and discount. " << endl; if (quantity >= 1) totalCostOfPurchase = price * quantity; else if (quantity <= 9) totalCostOfPurchase = price * quantity; else if (quantity >= 10) totalCostOfPurchase = price * quantity - discount1; else if (quantity <= 19) totalCostOfPurchase = price * quantity - discount1; else if (quantity >= 20) totalCostOfPurchase = price * quantity - discount2; else if (quantity <= 49) totalCostOfPurchase = price * quantity - discount2; else if (quantity >= 50) totalCostOfPurchase = price * quantity - discount3; else if (quantity <= 99) totalCostOfPurchase = price * quantity - discount3; else if (quantity >= 100) totalCostOfPurchase = price * quantity - discount4; cout << "Thank you for your purchase. The final amount with your discount is $" <<totalCostOfPurchase <<endl; return 0; }
__________________
B00tleg is offline   Reply With Quote
Old 10-12-2004, 12:02 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
Quote:
Code:
else if (quantity <= 9) totalCostOfPurchase = price * quantity; else if (quantity >= 10) totalCostOfPurchase = price * quantity - discount1; else if (quantity <= 19)
A value of 0 is below 9, thus the calculation starts.
This can be handlet in the if(quantity <= 0) put a return state here, and it will never go any further.

Also with the furhter calculations theres is a misshap, if a value of 15 or 20 for that matter is entered, only the
totalCostOfPurchase = price * quantity - discount1;
is beeing calculated, it never goes any further..

You need to make a more precise if() clause ie:
Code:
else if (quantity >= 0 && quantity <= 9) totalCostOfPurchase = price * quantity; else if (quantity >= 10 && quantity <= 19) totalCostOfPurchase = price * quantity - discount1; else if (quantity >= 20 && <= 49) totalCostOfPurchase = price * quantity - discount2; etc....
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM
Help on interest program B00tleg Standard C, C++ 2 10-07-2004 08:50 PM
Program Call to AS400 using JTOpen sde Java 0 05-12-2004 07:08 AM
Area Calculation Program revolution Python 0 02-16-2003 11:31 PM


All times are GMT -8. The time now is 09:58 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle