Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Help with a C program!

Name: Anonymous 2011-11-28 18:02

Okay guys, here is the problem I'm trying to solve:

Suppose there is a company that sells an item at a base retail price of $57.95. They give discounts if for volume purchases. Suppose they give the following discounts:
Quantity Discount
1-4 no discount
5-9 10%
10-19 20%
20-49 30%
50-more 35%
Write a program that asks the user to enter how many of the items they wish to buy. The program should then display messages saying what there total discount was (if any) and the total cost for the purchase after the discount. As the total will be in dollars and cents, when giving the output the prices should be formatted to print with two digits past the decimal place.



And here is my code:
http://pastebin.com/Ct0JSXQT


Can anyone please tell me what I'm doing wrong?
(I am pretty new to C programming, so please forgive my stupidity.)

Name: Anonymous 2011-11-28 18:10

First, tell us the compiler errors.
Second, read some beginner's guide to C (or, rather, the K&R book).
Third, your code is incomplete, has missing values in assignments and linebreaks in strings.
Fourth, we don't do your homework. If you have a specific problem, tell it. Otherwise, read your compiler errors.

Name: Anonymous 2011-11-28 18:11

>>1
Can anyone please tell me what I'm doing wrong?
pretty much everything. it hurts my eyes to look at your code

Name: Anonymous 2011-11-28 18:15

Here's a screenshot of the compiler errors:

http://i.imgur.com/TvrjR.jpg

And I know that all the like "discount1", "discount2", etc aren't completed, but they're not really what my question is.

I'm just trying to see if I'm setting up the code correctly in general.

Also, I do have a pdf for "Kernighan & Ritchie - The C Programming Language (2nd Edition)", if that's what you were talking about, although it's still pretty confusing to me.

Lastly, this isn't homework, I'm just trying to practice C programming because I am going to have to do it next semester for a beginning programming class.

Name: Anonymous 2011-11-28 18:20

You haven't declared the types for any of the discount* variables, you never do any math to determine what they should be, you use $var when you should use printf("%.2f",var).

Name: Anonymous 2011-11-28 18:23

You need to put i before <=, '<=4' is meaningless garbage. And remove those line breaks from your string literals.

Name: Anonymous 2011-11-28 18:31

Here's a pastebin with the math, and i's before the <=4, but what do you mean the "<=4" is meaningless garbage?:

http://pastebin.com/GUNrNi3m

I know that they're not done right, I just wanted to show the anon who mentioned math that I know what needs to go there, I just don't know how to properly do it.

And how do I declare types for the discount variables?

Also, someone else told me I didn't need 5 types of "discount1", "discount2", and that I only needed 1, but how would I go about doing that?

Name: Anonymous 2011-11-28 18:45

Okay, scratch the other pastebins, I got this one:

http://pastebin.com/kn9kxxVi

Which is not leading to any compiling problems.

Also, it recognizes the appropriate discount percentages now, it just seems that the only problem is that it is not actually printing a final discounted price.

See here (a screenshot of the output):
http://i.imgur.com/fy9cY.jpg

Name: Anonymous 2011-11-28 19:41

>>8
The printf statements should be corrected to e.g.
printf("You recieved a 30 percent discount! Your total purchase price is: %d\n", discount);
(%d is only for integer variables BTW)
I think you should read up on C before trying to write code like this.

Name: Anonymous 2011-11-28 22:32

Clearly you need to refactor.
1. First you should rewrite the discounts in an array of struct (startqty endqty discount).
2. Implement the C prog to for loop over the array to select the proper discount.
3. Once that works, final EXPERT PROGRAMMER part is to implement a DSL that takes in the discount table in your post verbatim and parses it to the array of structs.
4. Come back to work next year to expand the business logic EXPERT SYSTEM to allow logical constraint backtracking over complex business rules.

Name: Anonymous 2011-11-29 8:17

<--- check 'em dubz

Name: Anonymous 2011-11-29 9:53

Where do these retards come from, /g/?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List