Thread: I need help.

  1. #16
    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;

  2. #17
    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
    -----------------------------------------------

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Shooby
    I don't really understand what you just told me.
    Sorry, lunch Basically Salem and Davt already explained the scanf problem. I suggested you either read a complete string so the lone \n is also taken from the input buffer, or that you read in an integer (%i) into an integer variable. If you did that, you couldn't have read "a", "b"... though.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Shooby
    Right, I have gotten this far, but it doesn't seem to work:
    Code:
       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");
          }
    Any ideas?
    = assigns a value.
    == tests for equality.

    For future reference...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed