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;
}
Program not working...code in trouble.
Started by Oiprocs, Oct 24 2007 03:35 PM
4 replies to this topic
#1
Posted 24 October 2007 - 03:35 PM
|
|
|
#2
Posted 24 October 2007 - 09:23 PM
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.
Oiprocs said:
for ( i = digit[11]-1; i >= 0; i--)
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.
#3
Posted 25 October 2007 - 11:41 AM
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.
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.
#4
Posted 25 October 2007 - 05:46 PM
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.
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.
#5
Posted 26 October 2007 - 02:49 PM
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.


Sign In
Create Account

Back to top









