Thread: I need help.

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    8

    I need help.

    I am currently writing a program in C for a currnecy converter.
    I have hit a wall right at the start:


    float ster;
    float char;
    ster=0;
    eur=0;
    ster=eur*0.7:

    printf("Enter amount in Euro\n");
    scanf("%f", &eur);

    printf("The equivalent in Sterling is %f", &ster);


    This much doesn't work!
    The equivalent just prints off as 0.0000
    Hmm?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    Initialization problem

    You have intialized eur=0;
    do initialize
    float eur=0.0;

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    C is not a logical language. First you need to have your input, then you can do some calculations with it and only when that is done, you can output it.

    also

    Code:
     printf("The equivalent in Sterling is %f", &ster);
    should probably be

    Code:
     printf("The equivalent in Sterling is %f", ster);

    Since you're new, welcome, and please use code tags.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    Ah yes. Thanks.
    Another quick question, is it possible to nest switches or will they overlap and cause all sorts of infinite loops etc.?

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Shooby
    is it possible to nest switches or will they overlap and cause all sorts of infinite loops etc.?
    Like how? I don't see how the compiler could confuse a switch statement if you use { }. You should better stay away from nested if/else clauses without { } though, if that's what you're referring to.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    This seems to interfere with itself, maybe I have made an obvious mistake:

    Code:
    #include <stdio.h>
    
    main()
    {
    char ch;
    int num;
    float eur=0.0;
    float ster=0.0;
    
    
    
    
    printf("Choose a country for currency conversion\n");
    printf ("A.Britain\nB.Denmark\nC.Japan\nD.USA\nE.Exit\n"); 
    scanf("%c", &ch);
    switch (ch)   
    {
    	case 'a' :
     		printf("Do you wish to convert:\n");
     		printf("1.From Euro to Sterling\n2.From Sterling to Euro\n");
     		scanf("%d", &num);
          switch (num)
          {
          	case '1' :
             	printf("Enter amount in Euro\n");
      				scanf("%f", &eur);
      				ster=eur*0.70;
      				printf("The equivalent in Sterling is %4.2f", ster);
                break;
             case '2'  :
    			   printf("Enter amount in Sterling\n");
                break;
             default :
             	printf("invalid entry");
          }
    
       case 'b' :
       	printf("YO");
          break;
    
       default :
       	printf("invalid entry");

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    No, that's an issue with your scanf usage. It's one of those functions that give normal people a headache. You could read a string instead of a single character. Like...

    Code:
    char num[2];
    ...
    scanf("%2s", num);
    switch (*num)
        {
    ...
    Same for num. Or read integer - probably the better solution unless you really need 'a', 'b', 'c'...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    I don't really understand what you just told me.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    Right, I have gotten this far, but it doesn't seem to work:

    Code:
    #include <stdio.h>
    
    main()
    {
    char ch;
    char ch1;
    float eur=0.0;
    float ster=0.0;
    
    
    
    
    printf("Choose a country for currency conversion\n");
    printf ("A.Britain\nB.Denmark\nC.Japan\nD.USA\nE.Exit\n");  scanf("%c", &ch);
    switch (ch)
    {
    	case 'a' :
       case 'A' :
     		printf("Do you wish to convert:\n");
     		printf("1.From Euro to Sterling\n2.From Sterling to Euro\n");
     		scanf("%c", &ch1);
          if (ch1='1')
          {
          	printf("Enter amount in Euro\n");
           	scanf("%f", &eur);
           	ster=eur*0.70;
           	printf("The equivalent in Sterling is %4.2f", ster);
          }
          if (ch1='2')
          {
           	printf("Enter amount in Sterling\n");
          }
          else
          {
          	printf("invalid entry");
          }
    
       case 'b' :
       case 'B' :
       	printf("continue program...");
          break;
    
       default :
       	printf("invalid entry");
    }
    }
    Any ideas?

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    One quick fix to solve your 'interfering' issue is that you have missed the 'break' at the end of case 'A'. You also should really break after the default case - more of a style thing really.

    Once you have that fixed you should be able to reinstate the switch for ch1 - which is more stylish than the if ... else if ... else structure that you (should) have at the moment.
    DavT
    -----------------------------------------------

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    Thanks very much.
    How would I go about looping back to the original menu after each conversion? Do I use the do-while statement?

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Yes.
    DavT
    -----------------------------------------------

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    I put the break in on case 'A' but now the program freezes immediatley after case A is entered.

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Could you post your code? Otherwise we'll just be guessing. An datking a stab in the dark, you did put the break in the line directly above "case'b'" didn't you?
    DavT
    -----------------------------------------------

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    This is it, but putting the break above case 'b' doesn't work either.


    Code:
    #include <stdio.h>
    
    main()
    {
    char ch;
    char ch1;
    float eur=0.0;
    float ster=0.0;
    
    
    
    
    printf("Choose a country for currency conversion\n");
    printf ("A.Britain\nB.Denmark\nC.Japan\nD.USA\nE.Exit\n");  
    scanf("%c", &ch);
    switch (ch)
    {
    	case 'a' :
       case 'A' :
     		printf("Do you wish to convert:\n");
     		printf("1.From Euro to Sterling\n2.From Sterling to Euro\n");
     		scanf("%c", &ch1);
          break;
    
          switch (ch1)
          {
          	case '1' :
          		printf("Enter amount in Euro\n");
           		scanf("%f", &eur);
           		ster=eur*0.70;
           		printf("The equivalent in Sterling is %4.2f", ster);
             	break;
    
             case '2' :
           		printf("Enter amount in Sterling\n");
                scanf("%f", &ster);
                eur=ster*1.42;
                printf("The equivalent in Euro is %4.2f", eur);
             	break;
    
             default:
          		printf("invalid entry");
             	break;
          }
    
    
       case 'b' :
       case 'B' :
       	printf("...");
          break;
    
       default :
       	printf("invalid entry");
    }
    }

Popular pages Recent additions subscribe to a feed