![]() |
|
#1
|
|||
|
|||
Help! I Cant Get My Program To Work :(hi i am a newbie to C++, and i am having problems with the program below
My goal: To make a program that will allow a user to enter a 5 digit number, and space that number by 3 so it will look like this 1 2 3 4 5 my code ... CPP / C++ / C Code:
what am i doing wrong |
|
#2
|
|||
|
|||
|
Lemme get this straight, you want to place spaces between each digit in the number? So you input 12345, and it outputs "1 2 3 4 5"?
If so, you've not thought out your arithmetic straight. You're getting the input correctly, so everythings fine until line 20 (from the boards). But look further down. You've got major problems here. num2 - num5 are all zero - nothing's happened to them. Then you're trying to divide zero by 1000 - still zero. What you want to do is extract the digits from the number. OK, so you've got 54321. How to get the 5. You divide by 10000, but what happens to the 4321 remainder? Use the mod operator %, which gives you the remainder! 54321 % 10000 = 4321 54321- 4321 = 50000 50000/10000 = 5 I'll do another loop: (4321 - (4321 % 1000) ) / 1000 = 4 Just continue this, and you can then cout the answer nicely spaced. GF |
|
#3
|
|||
|
|||
|
ahhhh thank you so much , i will try this later
![]() |
|
#4
|
|||
|
|||
|
i'm back and i have fixed alot of my errors .. but i am still having problems.... am i even on the right track? my code is below
CPP / C++ / C Code:
sandra |
|
#5
|
|||
|
|||
|
It may be better to show this in a long winded way, but in a way that you can probably follow the logic.
Take the number you input, say for example you entered 75364 then take away the remainder after dividing by 10000 and put it into num2 num2 = num1 % 10000; // num2 will contain 5364 Take this away from your original number to make it divisible by 10000 num1 = num1 - num2; // num1 will contain 70000 from (75364-5364) Divide it by 10000 num1 = num1 / 10000; // num1 will now contain 7 Now work on the next step num3 = num2 % 1000; // num3 will contain 364 num2 = num2 - num3; // num2 will contain 5000 from (5364 - 364) num2 = num2 / 1000; // num will now contain 5 Next step num4 = num3 % 100; // num4 will contain 64 num3 = num3 - num4; // num3 will contain 300 from (364 - 64) num3 = num3 / 100; // num3 will now contain 3 Next step num5 = num4 % 100; // num5 will contain 4 num4 = num4 - num3; // num4 will contain 60 from (64 - 4) num4 = num4 / 10; // num4 will now contain 6 So each variable holds num1 is 7 num2 is 5 num3 is 3 num4 is 6 num5 is 4 So the program segment without comments is num2 = num1 % 10000; num1 = num1 - num2; num1 = num1 / 10000; num3 = num2 % 1000; num2 = num2 - num3; num2 = num2 / 1000; num4 = num3 % 100; num3 = num3 - num4; num3 = num3 / 100; num5 = num4 % 100; num4 = num4 - num3; num4 = num4 / 10; When you have mastered that you may wish to use a loop to take care of 'any' number of digits entered. Another method would be to treat the input as a string and just convert each character in the string. When all that becomes easy you could try writing the whole program in one line Hope that helps, Or indeed if it is correct Shawn |
|
#6
|
|||
|
|||
|
thank you.... i read your responce and i totally understand what you are saying... but for some reason i cannot get my program to work still ..... i dont know i guess this is a lost cause. this is my first week learning c++ and i feel like pulling out my hair
Sandra |
|
#7
|
|||
|
|||
|
It does get better - you will see. Keep going at it. The best way to learn anything is by the mistakes you can make. It would be just great though if there was something that would come along and tell you exactly what it was.
Where are the errors? |
|
#8
|
|||
|
|||
|
thanks for your help this evening.... i dont remember what exactly it said ( i just logged out ) ....... i appreciate you taking the time to help me, and your possitive feedback
thanks again Sandra~ |
|
#9
|
|||
|
|||
|
OK
A great thing about C or C++ is when you get loads of errors don't worry too much. They are most likely caused by the first. It is usually the first error that is important to fix, when you do this the others often just disappear. Or am I being a little too positive ![]() |
|
#10
|
|||
|
|||
|
CPP / C++ / C Code:
Here is a program that does what you want. __________________
Dariklar, 63 human monk Migboaf, 64 dwarvish cleric Xegony, EverQuest. http://dariklar.googlepages.com |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with calendar program | mike3340 | C++ Forum | 0 | 18-Nov-2003 20:25 |
| error during program | rjd72285 | C++ Forum | 0 | 11-Nov-2003 18:49 |
| Why doesnt my form work correctly? | rhino1616 | Web Design Forum | 2 | 06-Nov-2003 17:21 |
| How do web redirection scripts work? | rhino1616 | Web Design Forum | 9 | 27-Oct-2003 09:47 |
| one program access another? | dgoulston | C++ Forum | 1 | 07-Oct-2003 11:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The