|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Okay I can't get my program to work. The user is prompted to enter an integer, and the result is displayed in English.
** Enter an integer: 823 eight two three Enter an integer: -007 minus seven ** However, when you receive the user's input, you need to take the rightmost digit and store it into the array, get rid of the rightmost digit, and then walk backwards in the array from last digit saved to the first digit saved. After that, you switch on the saved digit and output the digit's English equivalent. I am pretty sure I have the correct structure down for the program, but I just can't get it to work. Sometimes it will print, regardless of number, "seven six five four three two one". Other times I'll enter 43 and it will return "434 four three two one zero". Help? ___________________________________ #include <stdio.h> int main (void) { int i, right_digit; int digit[11] = {'\0'}; printf ("Enter an integer: "); scanf ("%d", &i); getchar (); if ( i == 0 ) { printf ("zero"); getchar (); return 0; } if ( i < 0 ) { i = -i; printf ("minus"); } for ( i = digit[11]-1; i >= 0; i--) while ( i != 0) { right_digit = i % 10; i = i / 10; } switch ( i ) { case 1 : printf ( "one " ) ; break ; case 2 : printf ( "two " ) ; break ; case 3 : printf ( "three " ) ; break ; case 4 : printf ( "four " ) ; break ; case 5 : printf ( "five " ) ; break ; case 6 : printf ( "six " ) ; break ; case 7 : printf ( "seven " ) ; break ; case 8 : printf ( "eight " ) ; break ; case 9 : printf ( "nine " ) ; break ; case 0 : printf ( "zero " ) ; break ; } return 0; } |
| Sponsored Links |
|
|
|
|||
|
It's not working because there's a bug here, well 2 actually.
Can you see it? You're assigning the user input into i, but then putting it into i in the for loop! Secondly digit[11] is an invalid index, you're reading pass the array, which will be a random value, hence the randomness. i could be assigned any value. So your algorithm is obviously wrong. Suggestion, before you write ANY code, write down the logic on paper first, in pseudocode. Once you've got this right, only then start writing code. Saves a heck lot of time. |
|
|||
|
A) Are you implying I should assign a different variable to use in the for loop?
B) The program states that to store the digits into the array we should use { digits[11] = (9, 9, 9, 9, 9, 9, 9, 9, 9, 9) }. However, a friend of mine told me it would be simpler to write { digits[11] - ('\0') }. C) I'll make use of this suggestion. |
|
|||
|
A) Yes, unless you're overwriting the user input intentionally. If the user enters 43 which is stored in i, you are overwriting this i in the for loop! Do you understand the logic of your own program?
B) You're missing the point. digit[11] is invalid for the array. The last index you can access is 10 NOT 11! C) Make sure you understand the logic of your own program because at the moment it seems like you don't. |
|
|||
|
If one were to declare an array of ten elements, then the indeces could only be from 0 to 9. All counting in C++ - and in most programming languages for that matter - starts at zero.
|
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Basic Calculator | AfTriX | VB Tutorials | 3 | 02-29-2008 09:53 AM |
| Please Help With A C Program!! | siren | C and C++ | 7 | 04-17-2007 09:45 AM |
| Registration code (used for demo's of ur program) | Deathcry | C and C++ | 5 | 03-11-2007 02:26 PM |
| Where to Put PHP Code | clookid | PHP Tutorials | 1 | 01-11-2007 09:44 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |