![]() |
|
#1
|
|||
|
|||
Fresh Small C++ Program :PHello all, I just started taking C++ and its a bit different than my usual C programs I wrote
The simple question im working says to write a program that inputs a five digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. For example, lets say I entered 93032 it would look like this 9 3 0 3 2 My code is only this so far: CPP / C++ / C Code:
its a bit strange why we were assigned this question, considering we never went over anything on it =p |
|
#2
|
|||
|
|||
Re: Fresh Small C++ Program :PI would begin using mask.
I mean you should do: I assume int => 16 bits CPP / C++ / C Code:
each of the number is in the array. Hope this help! Last edited by LuciWiz : 26-Jan-2006 at 02:27.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#3
|
||||
|
||||
Re: Fresh Small C++ Program :PQuote:
Probably to prove you understand the rudiments of math, like to get the last digit from an integer use the modulus operator: CPP / C++ / C Code:
Then because you're done with that digit, you can remove it from the value: CPP / C++ / C Code:
Repeat It also lets you use your knowledge of arrays, for loops, and cout. Each (except cout) you already know from C __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#4
|
|||
|
|||
Re: Fresh Small C++ Program :PQuote:
1. Bad assumption. Although I know that some compilers have 16-bit ints, every single compiler that I have has 32-bit ints. The size of an int is not specified in any C or C++ standards document. 2. Nothing in the problem statement suggests or requires 16-bit ints. Why should the solution depend on 16-bit ints? (As a matter of fact, the example that the Original Poster used --- 93032 --- requires 17 bits in its binary representation.) Quote:
Exactly what is in the array? That's a question that can be answered by actually running your code. It can also be answered by looking at the code: Each time through the loop it leaves an individual hex digit of the number in place with the other hex digits zeroed out. Huh??? How does this help??? I pasted your code into the Original Poster's program (after supplying the missing semicolon on your mask declaration) and ran it with an input that can be represented as a 16-bit integer and printed out the elements of the array: CPP / C++ / C Code:
Here's the result: Code:
Quote:
I am sure that everyone appreciates all attempts to help, but if you really want to help, I respectfully suggest that you test your code and see what it gets and see if it could help the Original Poster solve the stated problem. Regards, Dave Last edited by davekw7x : 26-Jan-2006 at 10:23.
|
|
#5
|
|||
|
|||
Re: Fresh Small C++ Program :PDave:
You are the kind of people I don't like to work with. Are you able to help ? Are you able to give advice ? if you are only able to bash, go away. I only want to help, not give a full solution. This guy is doing homework, so he have to learn and try and solve the problems! I think I gave a good hint. When I said I assume int -> 16 bits, was for the example purpose only. If your compiler use 32 bits, use the loop for 32 bits. Then, I agree I forgot to shift right, but I think we are all here to help and give advice. |
|
#6
|
|||
|
|||
Re: Fresh Small C++ Program :PWaltP, I realized you do have to your integer division and modulus (%) from a friend today. The only problem is, my math skills are not so bright, I do not know the forumla for this either, but I think I may have a clue how.
I appreciate weeb0, waltp, and dave for your feedback. weeb0 I understood what you were saying too, and btw this is not homework I'm working on this now still, give me a minute =D Sabin P.S; Weebo, i dont think dave was trying to insult ya or anything or be negative towards ya for any reason =] |
|
#7
|
|||
|
|||
Re: Fresh Small C++ Program :PQuote:
You are correct in saying that my post didn't contribute any direct help to the Original Poster. Normally I don't jump into a thread unless I feel that I can offer something towards solving a problem. In this case, I think that Walt had given a pretty good hint for one approach to the problem. My post was not intended to be a personal insult, but I meant to point out that the code that you posted wouldn't help to solve the problem of extracting decimal digits from an integer value. A lot of people follow the threads in this forum looking for general hints and techniques for solving various programming problems. In general, I can't see how masking and shifting is useful in obtaining decimal digits. (Binary, octal or hexadecimal digits, yes, but those weren't any part of the problem here.) I respectfully suggest that people always test their code and look at the output and then decide whether it will actually help other people. This forum is a wonderful place, and I enjoy seeing other people's points of view. I learn something every single time that I visit here. You are entitled to your opinion. I am entitled to mine. Regards, Dave Last edited by davekw7x : 26-Jan-2006 at 16:18.
|
|
#8
|
||||
|
||||
Re: Fresh Small C++ Program :PQuote:
And Dave actually is one of the best people on this board to give help. Simply look back on many of his posts. I think you did him an injustice. He was simply stating that the values the original poster was using seem to be decimal, not hex. Your solution is very good for hex. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#9
|
|||
|
|||
Re: Fresh Small C++ Program :PQuote:
Quote:
To be fair: I think I could have phrased my criticism better. I didn't mean for it to be personal. One of the things that I am constantly working on is: "How can I give more help to more people?" If any individual takes my statements as a personal put-down, then others may see it that way, too. This means that I haven't helped everyone that I could have, since the entire interchange will probably be discounted. (The other thing that happens is that the original purpose of the thread gets lost in the noise. I hope that we can get back on track now.) When I try to help, sometimes I nail it; sometimes --- not so much. Regards, Dave "The mighty words of the proud are paid in full by mighty blows of fate, and at long last these blows will teach us wisdom." --- Sophocles Antigone "Well, I can tell you that the part about the blows is true. I'm still waiting for the 'wisdom' thing." --- davekw7x gidforums |
|
#10
|
||||
|
||||
Re: Fresh Small C++ Program :PJust hover through the little green buttons(Right to his name) of Dave, and you'll find what Dave is worth of!
__________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| creating a file in [c] | i hate c | C Programming Language | 15 | 21-Nov-2005 13:52 |
| Small help with this program please | NigelZen | C Programming Language | 3 | 29-Aug-2005 00:17 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 13:04 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
| Having a small problem with this program | Krc784 | C++ Forum | 1 | 04-Nov-2004 23:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The