Not working? 
Author Message
 Not working?

The last bit of my program isn't working correctly:
****
#include <stdio.h>
#include <string.h>
#include <math.h>

#define BUFFER 10

char *GetWord(char c)
{
 char *ret=NULL;

 switch(c)
 {
  case '0':ret = "zero";break;
  case '1':ret = "one";break;
  case '2':ret = "two";break;
  case '3':ret = "three";break;
  case '4':ret = "four";break;
  case '5':ret = "five";break;
  case '6':ret = "six";break;
  case '7':ret = "seven";break;
  case '8':ret = "eight";break;
  case '9':ret = "nine";break;
 };
 return ret;

Quote:
}

char *GetUnit(int now, int total)
{
 char *ret=NULL;
 switch(total)
 {
  case 0:

  break;
  case 1:

  break;
  case 2:

  break;
  case 7:
  if(now==0)ret = "thousand";
  if(now==1)ret = "hundred";
  if(now==2)ret = "tens";
  if(now==3)ret = "ones";
  break;

 };
return ret;

Quote:
}

int main()
{
char amount[BUFFER];
int i=0;
int len;
char *temp=NULL;

double hun;
double ten;

double test1;
double test2 = 112.43;
double test3;

printf("Enter amount: ");
fflush(stdout);

fgets(amount, strlen(amount), stdin);

len = strlen(amount);

printf("%s\n",amount);
printf("%d\n",len);

/*while(amount[i] != '.')
{
 printf("Word: %s %d\n",GetWord(amount[i]),i);
 i++;

Quote:
} */

test1 = modf(test2, &test3);
printf("Test1: %f\n",test1);
printf("Test2: %f\n",test2);
printf("Test3: %f\n",test3);

printf("and %0.2f cents.\n", test1);

hun = test2/100;
printf("Hundreds: %1.0f\n",hun);

ten = (test2 - (hun*100) )/10;
printf("Tens: %1.0f\n", test2 - (100*hun));/* THIS BIT DOES NOT WORK*/

return 0;

Quote:
}



Fri, 09 Jan 2004 21:16:32 GMT  
 Not working?
The last bit of my program isn't working correctly:
****
#include <stdio.h>
#include <string.h>
#include <math.h>

#define BUFFER 10

char *GetWord(char c)
{
char *ret=NULL;

switch(c)
{
  case '0':ret = "zero";break;
  case '1':ret = "one";break;
  case '2':ret = "two";break;
  case '3':ret = "three";break;
  case '4':ret = "four";break;
  case '5':ret = "five";break;
  case '6':ret = "six";break;
  case '7':ret = "seven";break;
  case '8':ret = "eight";break;
  case '9':ret = "nine";break;

Quote:
};
return ret;
}

char *GetUnit(int now, int total)
{
char *ret=NULL;
switch(total)
{
  case 0:

  break;
  case 1:

  break;
  case 2:

  break;
  case 7:
  if(now==0)ret = "thousand";
  if(now==1)ret = "hundred";
  if(now==2)ret = "tens";
  if(now==3)ret = "ones";
  break;

Quote:
};
return ret;
}

int main()
{
char amount[BUFFER];
int i=0;
int len;
char *temp=NULL;

double hun;
double ten;

double test1;
double test2 = 112.43;
double test3;

printf("Enter amount: ");
fflush(stdout);

fgets(amount, strlen(amount), stdin);

len = strlen(amount);

printf("%s\n",amount);
printf("%d\n",len);

/*while(amount[i] != '.')
{
printf("Word: %s %d\n",GetWord(amount[i]),i);
i++;

Quote:
} */

test1 = modf(test2, &test3);
printf("Test1: %f\n",test1);
printf("Test2: %f\n",test2);
printf("Test3: %f\n",test3);

printf("and %0.2f cents.\n", test1);

hun = test2/100;
printf("Hundreds: %1.0f\n",hun);

ten = (test2 - (hun*100) )/10;
printf("Tens: %1.0f\n", test2 - (100*hun));/* THIS BIT DOES NOT WORK*/

return 0;

Quote:
}



Fri, 09 Jan 2004 21:17:48 GMT  
 Not working?

This isn't an IRC chat channel. It takes a while for your message to
move on to a couple million news servers, then for people to download,
then read, then reply.

Some times it can take DAYS to get a response. If you post and didn't
get a reply in 24-48 hours, then try posting again.



Fri, 09 Jan 2004 21:41:16 GMT  
 Not working?


Quote:
> printf("Tens: %1.0f\n", test2 - (100*hun));/* THIS BIT DOES NOT
> WORK*/

Uhmm, question, what exactly isn't working?

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Fri, 09 Jan 2004 23:20:33 GMT  
 Not working?
Why would you assume that we know what the part that
does not work should do when it IS working? Please be a little
clearer next time.

I will guess that the problematic part should print the tens
portion of the variable test2 (112.43), to which the result is the integer
1.

Well, if "hun=test2/100", then your line "ten = (test2 - (hun*100) )/10;"
basically always evalutes to "ten = 0/10". This will never get you your
result.
Try something like this:
==============================
#include <stdio.h>
#include <string.h>
#include <math.h>

char *GetWord(char c);

#define BUFFER 10

int main()
{
 char amount[BUFFER];
 int i=0;
 int len;
 char *temp=NULL;

 double hun;
 double ten;

 double test1;
 double test2 = 112.43;
 double test3;

 printf("Enter amount: ");
 fflush(stdout);

 fgets(amount, strlen(amount), stdin);

 len = strlen(amount);

 printf("%s\n",amount);
 printf("%d\n",len);

 test1 = modf(test2, &test3);
 printf("Test1: %f\n",test1);
 printf("Test2: %f\n",test2);
 printf("Test3: %f\n",test3);

 printf("and %0.2f cents.\n", test1);

 // Changed to test3
 hun = (test3/100);
 printf("Hundreds: %1.0f\n",hun);

 // Added a cast - you should consider
 // using modf instead to extract the 1 from 1.12
 // as casting a float to an int is bad practice
 ten = (test2 - (((int)hun)*100) )/10;

 // got rid of on-the-fly calculation
 printf("Tens: %1.0f\n", ten);

 return 0;

Quote:
}

==============================
Also, why did you go through the trouble of assigning this result
to a variable ("ten"), and then calculated it again on the fly in the
printf statement, instead of just writing printf("Tens: %1.0f\n", ten);

Finally, there is no need for a semicolon at the
closing brace of a switch statement.

Alon


Quote:
> The last bit of my program isn't working correctly:
> ****
> #include <stdio.h>
> #include <string.h>
> #include <math.h>

> #define BUFFER 10

> char *GetWord(char c)
> {
> char *ret=NULL;

> switch(c)
> {
>   case '0':ret = "zero";break;
>   case '1':ret = "one";break;
>   case '2':ret = "two";break;
>   case '3':ret = "three";break;
>   case '4':ret = "four";break;
>   case '5':ret = "five";break;
>   case '6':ret = "six";break;
>   case '7':ret = "seven";break;
>   case '8':ret = "eight";break;
>   case '9':ret = "nine";break;
> };
> return ret;
> }

> char *GetUnit(int now, int total)
> {
> char *ret=NULL;
> switch(total)
> {
>   case 0:

>   break;
>   case 1:

>   break;
>   case 2:

>   break;
>   case 7:
>   if(now==0)ret = "thousand";
>   if(now==1)ret = "hundred";
>   if(now==2)ret = "tens";
>   if(now==3)ret = "ones";
>   break;

> };
> return ret;
> }

> int main()
> {
> char amount[BUFFER];
> int i=0;
> int len;
> char *temp=NULL;

> double hun;
> double ten;

> double test1;
> double test2 = 112.43;
> double test3;

> printf("Enter amount: ");
> fflush(stdout);

> fgets(amount, strlen(amount), stdin);

> len = strlen(amount);

> printf("%s\n",amount);
> printf("%d\n",len);

> /*while(amount[i] != '.')
> {
> printf("Word: %s %d\n",GetWord(amount[i]),i);
> i++;
> } */

> test1 = modf(test2, &test3);
> printf("Test1: %f\n",test1);
> printf("Test2: %f\n",test2);
> printf("Test3: %f\n",test3);

> printf("and %0.2f cents.\n", test1);

> hun = test2/100;
> printf("Hundreds: %1.0f\n",hun);

> ten = (test2 - (hun*100) )/10;
> printf("Tens: %1.0f\n", test2 - (100*hun));/* THIS BIT DOES NOT WORK*/

> return 0;
> }



Sat, 10 Jan 2004 17:43:29 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. C++/ATL/ADO - Intellisense not working (statement completion options) not working

2. this code works on SGI does not work on linux

3. CMap Lookup not working (at least I cannot get it to work)

4. Program not work at release mode but work at debug mode

5. GetPrivateProfileSectionNames not working in 98 or ME but does work in 2000

6. Window NT BitMap Works Windows95 BitMap Does not Work

7. Not operator NOT working (!)

8. Setting not Null field to ""(empty string) does not work with CRecordset

9. CFile not working properly, can't figure out why (last message not sent properly)

10. Setting not Null field to ""(empty string) does not work with CRecordset

11. Web Service Not Working -- Namespace not found

12. DataView filter not working in MDI child forms with reference to MDI parent DataSet

 

 
Powered by phpBB® Forum Software