Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-24-2007, 07:35 PM
Oiprocs Oiprocs is offline
Newbie
 
Join Date: Oct 2007
Posts: 4
Rep Power: 0
Oiprocs is on a distinguished road
Default Program not working...code in trouble.

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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-25-2007, 01:23 AM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

It's not working because there's a bug here, well 2 actually.

Quote:
Originally Posted by Oiprocs View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-25-2007, 03:41 PM
Oiprocs Oiprocs is offline
Newbie
 
Join Date: Oct 2007
Posts: 4
Rep Power: 0
Oiprocs is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-25-2007, 09:46 PM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-26-2007, 06:49 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 787
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 03:54 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%

Ads