Jump to content

Program not working...code in trouble.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Oiprocs

Oiprocs

    Newbie

  • Members
  • Pip
  • 4 posts
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;
}

#2
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts
It's not working because there's a bug here, well 2 actually.

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
Oiprocs

Oiprocs

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#4
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts
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.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
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.