![]() |
|
#1
|
|||
|
|||
Need help with a C program (Long)Hi all,
Im pretty new to C, so please forgive me if my code doesnt make sence in parts Anyways Im writing the second program of my CS class assigment but my program seems kind of all over the place at the moment. The question that im answering is: You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step. Some of the problems i've been getting are: 1) if i enter a character instead of a number at the start of the program where it asks for the users input, the program just keeps repeating that character infinatly down screen and i dont know how to make so it only accepts an integer input (so that if anything else is entered , the error message is triggered). 2) if i enter a number that is not in an increment of 5 it displays the error message fine, but then also displays the 'amount of coins' part and prints out a bunch of crazy numbers like 2147348480 Fifty cent piece's. 1 Ten cent piece. and I dont know what to do to stop it doing that 3) is there a way to show the original amount that the user entered, in the printf statement down the bottom of the program when it displays the coins needed? This is what ive done so far: CPP / C++ / C Code:
Any help would be great thanks. |
|
#2
|
||||
|
||||
|
Hello McFury. Welcome to GIDForums. Congratulations on using the c-syntax highlighting on your first post! That is a rarity around these parts
![]() Anyway, one thing that I notices right off is that your program is not modular. I don't know if you want to get this running and then split it into functions, but just keep that in mind. Overall, I think that you have a great start on this and it is a pretty decent program as well as easy to read. I will try to answer your questions the best I can... Quote:
CPP / C++ / C Code:
Quote:
CPP / C++ / C Code:
Quote:
CPP / C++ / C Code:
Hope this helps. Good luck, d |
|
#3
|
|||
|
|||
|
Thanks heaps for your reply dsmith, it was very helpful :-)
The only thing that seems to still be causing a problem is when i enter a character(or any key thats not a number) at any time from this statement, onwards it still repeats the character infinately down the screen. CPP / C++ / C Code:
also, yes I am suppose to present the program and algorithm in modular form but i couldnt remember 'exactly' what was meant by modular thanks in advance. |
|
#4
|
||||
|
||||
|
Quote:
Are you required to "error proof" the program? 1) If not, don't bother at this early stage of your programming. 2) If so, you will have to leave scanf() behind and use a combination of fgets()/sscanf() or something similar. This gets difficult. Quote:
1) Input section 2) Computation section 2a) 50 cent 2b) 25 cent 2c) 10 cent 2d) 5 cent 3) Output section If you are using functions, place the segments in separate functions. If not, write each section of the code so it can stand alone. __________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Call a C program through Linux shell script | nuwandee | C Programming Language | 3 | 29-Mar-2004 21:54 |
| Modify a C program | ayoub | C Programming Language | 3 | 15-Mar-2004 11:34 |
| one program access another? | dgoulston | CPP / C++ Forum | 1 | 07-Oct-2003 11:26 |
| convert long to pointer to char | realpopeye | CPP / C++ Forum | 2 | 26-Sep-2003 10:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The