![]() |
|
#1
|
|||
|
|||
Repetition structure problem and questionGreetings, I am a beginner in C++ and new to GIDForums, and was wondering if anyone could help me with a programming problem I'm having. Any help would be greatly appreciated!
My professor gave the following instructions: Write a program to calculate daily expense and produce a daily general ledger for a user. Input from user would be store name or expense category and dollar amount such as: Food Lion 85.67 Gasoline 23.89 The output should look like this: Item Amount --------- ------- Food Lion 85.67 Gasoline 23.89 Dry Clean 5.56 ---------------------------------------------------------- TOTAL: $115.02 That is the question presented... I have programmed this so far: CPP / C++ / C Code:
And of course, I have this output format, but I don't know where to enter it in the above program: CPP / C++ / C Code:
Basically, my question is how can I get this program to 'work'? :) In specific, my first question is how can I retrieve what the user types in for category and amount variables, and output them in the format listed above? (i.e. How do I print out all user input after all input statements have been entered?) Another big question I have is: How do I program my statement, while (tolower(amount[0]) != 'q') to quit when user enters in a CATEGORY rather than AMOUNT? How can it quit when it prompts to enter category, rather than amount? I can't seem to get that working... Finally, the "total" variable is not working and outputs an incorrect amount. Can anyone help me fix this? Again, any help is greatly appreciated! Thank you! Last edited by LuciWiz : 22-Oct-2004 at 05:26.
Reason: Inserting code tags...
|
|
#2
|
|||
|
|||
|
Quote:
I apologize for not using the color coding technique in my earlier post. Since the thread no longer gives me the option to edit my post, I have quoted it and used the color coding technique to make it easier to read. Again, I am new to the forum and sorry for this inconvenience. |
|
#3
|
||||
|
||||
|
Quote:
That's OK, since you are new the forum it is understandable. I actually really appreciate you posting back with your formatted code - I will format your previous code as well. Also, I am sorry I can't answer to your post just now - promiss to come back with a solution when I get the time, if nobody else does. Kind regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#4
|
|||
|
|||
|
Quote:
Thanks for being so kind and sincere! :-) So far, I have modified my program a little, but I still haven't gotten the results I want. Here is my modified program: CPP / C++ / C Code:
Now, what I am having trouble with is it only accepts ONE category and ONE amount, and automatically displays the output without accepting any more input. I have corrected the 'total' variable, which is a plus. However, I would like for the program to repeat the two statements asking for category and amount, and to output all input entered instead. Again, any help with this program is really appreciated! P.S. Leave the formatting to me (the setw() and other iomanip functions), I just need to know how to get the body of this program right! |
|
#5
|
|||
|
|||
|
i am not over sure but i remember stg like that the line
CPP / C++ / C Code:
CPP / C++ / C Code:
i hope this helps you... |
|
#6
|
||||
|
||||
|
Quote:
That's for sure amount is defined as a float. therefore you cannot type in a 'Q', big or small. You might have better luck with 0. But because floating point is not an exact value, you might try CPP / C++ / C Code:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#7
|
|||
|
|||
|
Quote:
Thank you both very much for your time and help! Thanks for correcting me on the logical || (OR) operator. I forgot that it needed to be repeated on both sides. I still have some questions unfortunately... I have decided to do something completely different in my program. Instead of the user quitting by entering 'q' or 'Q' in category or amount-- (not possible because it's a float), I have named another char variable, "quit." This will allow the user to keep entering in categories and amounts until the user presses 'N' or 'n.' I like this approach better because it allows for a switch point when the user has finished entering both categories and amounts. However, I am still not achieving the desired output. How can I retrieve all user input (categories and amounts) and output them under the "Item / Amount" output format? For instance, if the user enters Food Lion, Gasoline, Dry Clean and perhaps numerous other categories and 50.00, 34.00, 5.00, and other amounts, how can I output all of these under the format I have at the end of my program? (Please keep in mind, it's not just three separate inputs, it can be infinitely many inputs, until the user presses 'n'). Here is my modified program: CPP / C++ / C Code:
Again, I really appreciate your help! |
|
#8
|
||||
|
||||
|
Hi brookeville. Have you learned about arrays yet? You need to place the input into an array for later retreival. For now using a static array would be the easiest, but you will need to place a "maximum" on the amount of items that can be entered.
I have modified your code, but haven't tested it.. CPP / C++ / C Code:
That should get you pointed in the right direction. You may want to add some limit checking if the user tries to enter more than 100 items. HTH __________________
The best damn Sports Blog period. |
|
#9
|
|||
|
|||
|
Quote:
Hello and thank you very much for your help! My professor tought the basics of arrays, so I know a little about them... Before I attempted the program, I wrote out pseudocode basically stating that all user input should be printed out right after the input time. All should be in the loop body-- ask for category, ask for dollar amount, print out category entered, print out dollar amount, sum dollar amount. But getting that into code was somewhat difficult! I have adjusted the modified program you showed me in the following way: CPP / C++ / C Code:
The only problem I'm having now is allowing the user to input a category with spaces. It endlessly repeats the loop if a space is entered for category. I was thinking along the lines of using cin.getline(), but that didn't work. What I have here is a string type for category. I thought that would allow for spaces, but there is still something I'm doing wrong. Can someone please instruct me on this final question? Again, thanks so much for your help. |
|
#10
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Insert problem in Linked list | Kay Chan | C Programming Language | 1 | 03-Sep-2004 17:06 |
| a C input question.. | tmike | C Programming Language | 2 | 19-Sep-2003 02:39 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
| Code problem (a newbie question) | monkster87 | CPP / C++ Forum | 3 | 11-Aug-2003 12:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The