Thread: I need help.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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
    -----------------------------------------------

  8. #8
    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?

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

  10. #10
    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.

  11. #11
    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
    -----------------------------------------------

  12. #12
    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");
    }
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I see a big struggle coming up over the way scanf() handles the %c conversion...

    If you type in 'A' followed by return into this code, you get what is in the comments
    Code:
    scanf("%c", &ch);  // Stores the character 'A' in 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);  // Stores a newline in ch1
            switch (ch1)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    switch (buff[0])
    {
       case 'a' :
       case 'A' :
            printf("Do you wish to convert:\n");
            printf("1.From Euro to Sterling\n2.From Sterling to Euro\n");
            fgets( buff, BUFSIZ, stdin );
            switch (buff[0])
    Tip for posting code - make sure your editor is set to only use spaces for indentation. If you mix spaces and tabs, your editor may cope and produce nice code, but it will look a mess when posted on a message board, even when it is within code tags.

    You also might want to consider using some functions to stop your nested switch statements getting out of hand
    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    switch (buff[0])
    {
       case 'a' :
       case 'A' :
            do_sterling();
            break;

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    ok. First off the break is in the wrong place. the switch(ch1) is still part of case 'A' so the break (which tells the computer that case 'A' is now finished) should come afterwards. You were also having the normal problems with scanf(). When you read in ch you actually press 'A' followed by return. The scanf picks up the 'A' but leaves the return in the input buffer. When the next scanf comes along it reads the return character without waiting for the user to enter a value. There are a number of ways to fix this mentioned in the FAQs. I've added a bit of a rough and ready fgets() code to make the code work. You might want to add some error checking.
    Code:
    #include <stdio.h>
    
    int main()    /* main() should return an int */
    {
      char ch;
      char ch1;
      char buf[BUFSIZ];
      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");
      fgets(buf, sizeof(buf), stdin);     /* safe way to read input from user - need to add error checking */
      ch = buf[0];    /* we are only interested in the first character of the input string */
      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");
          fgets(buf, sizeof(buf), stdin);
          ch1 = buf[0];
    
          switch (ch1)
          {
            case '1' :
              printf("Enter amount in Euro\n");
              fgets(buf, sizeof(buf), stdin);
              sscanf(buf, "%f", &eur);
              ster=eur*0.70;
              printf("The equivalent in Sterling is %4.2f\n", ster);
              break;
    
            case '2' :
              printf("Enter amount in Sterling\n");
              fgets(buf, sizeof(buf), stdin);
              sscanf(buf, "%f", &ster);
              eur=ster*1.42;
              printf("The equivalent in Euro is %4.2f\n", eur);
              break;
    
            default:
              printf("invalid entry");
              break;
          }
    
          break;   /* end of case 'A' and case 'a' */
    
        case 'b' :
        case 'B' :
          printf("...");
          break;
    
        default :
          printf("invalid entry");
          break;
      }
    
      return 0;  /* main() should return 0 if successful */
    }
    enjoy!

    edit:: beaten by salem
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed