![]() |
Need help with C++ please
Hey all,
I know this might sound silly... But I need help in a simple thing... So the output of the code should look like this.. Code:
Dongxiao Here's my code so far: Code:
#include <iostream> Thanks in advance. EDIT: And just so you can be filled in on what I'm talking about.. This is what the homework says.. Quote:
|
Re: Need help with C++ please
You don't have to be creative: You already have the correct number of "." characters in the sample output that you gave...
So: Code:
cout << "Gross Amount: ............ $" << whatever << endl; |
Re: Need help with C++ please
Quote:
My only problem now is with the alignment.. (The numbers on the right has to be aligned to the right).. |
Re: Need help with C++ please
I'm not going to give you exact code, but I'll explain the logic (but you should know, if you intend to be a programmer, you'll want to get to a point where coming up with ways to do this stuff comes as second nature - I recommend you read each portion of what follows, and pause and see if you can figure out the rest, if you have the time):
Your output lines are of the format: [string 1] [padding periods] [string 2] So if you want every line to be the same number of characters, you want to count the length of [string 1] and [string 2], add those together, subtract that from your desired line width. So: [desired line width] - ([length of string 1] + [length of string 2]) or: [desired line width] - [length of string 1] - [length of string 2] That result number is the number of periods you want to insert (obviously subtract more for any other characters you want inserted, like spaces, if they aren't getting inserted as parts of string 1 or string 2). Then you can just do a for-loop, printing one period in the loop body, and loop as many times as you have periods to print. Alternatively, you can take the easy way out and use the stuff which comes in C++'s iostream and iomanip libraries to control the output of cout. (It has functions to set the width of the output, the fill/padding character, etc.) Consult the reference here (or another site more to your liking) for functions in the iostream/iomani library which look like they might be what you want (I think 'setw' is one of them): http://www.cplusplus.com/reference/ [edit] Ah, I see you're already experimenting with those iostream / iomanip approaches. I usually make my own logic instead of using them, as per the above approach, so I'm not be as good at spottng errors in their use, but what you've got commented out looks, at a glance, to be on the right track. [/edit] Also, usually sites like Stack Overflow often have answers available that explain stuff like this (though they frown on 'homework questions', they still have plenty that answer basic how-to's like this). In fact, a google for something like "c++ how to pad align output with filler padding character" will probably yield some clues. |
Re: Need help with C++ please
Oh, now if you are stuck trying to figure out what the length of "[string 2]" would be, since that's an integer/float/double variable, I would approach it like this:
You know that the amount of digits in the number determines the length. You know in decimal counting systems, each digit is one greater multiple of 10, essentially. For an integer, then, you can figure out how many digits it will be by dividing it by ten until it becomes zero (as C++ will floor decimal results for intergers). For every power of ten it has, you add one more to your digits counter. With a little bit of thinking, it should be possible for you to figure out how to convert the above into code. If you can't after a while, I can show you what I've written to do so. (Hint, either a loop, or a series of if/else-if checks with less/great -than comparisons.) With a little more thinking, you can adapt such approaches to floats/doubles as well. There's other ways too, of course, usually involving using other fancy C++ classes and stuff, if that's the route you want to go. |
Re: Need help with C++ please
Hey.. that looks like homework!
Damn I wish I had the internet when i was studying :( google printf string Padding ;) Edit: I never use C++ were C does the same thing.... I learnt C C++ is bloated in some regards compared to C bah anyway |
Re: Need help with C++ please
If you have a max expected width, say 6 digits:
printf("Gross Amount: ............ $%6.2f",value); Nice thing about C++, you can still use C. Just saying. |
Re: Need help with C++ please
Oh yeah, printf has an option for padding the output too. (The problem with always figuring out the logic yourself is you sometimes completely forget avout some of the other options available that do the work for you, lol.) I still enourage learning programmers to figure stuff like that out. After all, someone has to know how to implement that logic, else who'll implement our printf() padding and our setw() and so on?
As for the comment about C++ being bloated, that really depends on what you mean by bloated, but for a lot of modern software need, you can easily end up with binaries that actually execute faster if you write it taking advantage of C++ features than using plain C. (And this is coming from a plain C fan, don't care for C++ much.) Aside: Sometimes classes don't let you use the C stuff, and insist you use the C++ alternatives. I've had courses where the teacher demanded you used iostream and co. and nothing from stdio.h. I thought it was ******ed too.r |
Re: Need help with C++ please
Your code is already very close.
You're using setprecision to control the number of digits on the dollar amounts, good. It might be good practice to save the previous precision in a variable, and then set it back to what it was when you're done, but if this is homework you may not have been taught that yet and strictly speaking it's irrelevant if this is the entire program in this particular case. Since you are hardcoding the number of periods, instead of <<setfill('.') << setw(14) <<" $" you could do <<string(12,'.')<<" $" to print the periods. Or even better, just manually type them in the text like cout<<"Gross Amount: ............ $" The numbers will actually be right-justified by default, so you don't need to include << right << in your couts. All you need now is another setw() before the numbers, to control the on-screen printed width of them. You'll also want to change the fill character to space again, instead of periods, so your numbers don't look like "$...123.45". If you avoid using setfill as mentioned in the previous paragraph, then you won't need to change that here since it'll already be a space by default, making the code more simple. You could hardcode an setw value for the dollars, but ideally you could calculate the necessary width for the setw based on the input given. (so if I enter 123456789.12 it'll still align properly, and if I enter 123.45 it won't have a ton of extra spaces). But that might not be required for this exercise based on the wording of the question and depending on what the teacher has taught you so far. Good luck and have fun :) |
Re: Need help with C++ please
@Mentalist Traceur what i meant is there is over use of c++ obj orientation by c++ trained people.
Which is slower on the cpu ie games. C people tend not to abuse it. |
All times are GMT. The time now is 12:11. |
vBulletin® Version 3.8.8