Need a bit of help with a default

This is a discussion on Need a bit of help with a default within the C Programming forums, part of the General Programming Boards category; hey im writing a program for a currency converter but my default doesnt seem to be working the way i'd ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    Need a bit of help with a default

    hey im writing a program for a currency converter but my default doesnt seem to be working the way i'd like it to... i no its only something small (i hope) but if anyone could help it would be much appreciated...

    i started with a while loop and i have my main menu with the choices 1-5 and then a switch statement with everything else in it... and the very end i have a default saying "please enter only number 1-5"
    BUT when the user choose 5 it says "thank you for using the currency converter" and then i want it to end but instead my default statement reappears...


    any ideas anyone?

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Post the relevant section of code. You're probably missing a semicolon or something equally minor.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    case 5:
    {
    printf("Thank you for using Orla's Currency Converter!\n\n");
    printf("Have a nice day!");
    }
    default:
    printf("Please type number 1-5 only");




    }//end of switch
    }//while end
    }//end of main

  4. #4
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Here is an example of how your switch statement should work.

    PHP Code:
    switch (choice)
    {
        case 
    1:
                
    printf("Hello\n");
                break;
        case 
    2:
                
    printf("Goodbye\n");
                break;
        default:
                
    printf("Please enter either 1 or 2\n");

    Hope this helps.
    Microsoft is merely an illusion, albeit a very persistant one.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    everything else works fine...only the default at the end...

    could it be because of the loop?

  6. #6
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Post your code, remember to use code tags.
    Microsoft is merely an illusion, albeit a very persistant one.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    my code is really long....wat are tags?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go read the forum announcements and you'll find out.

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

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    Post

    #include<stdio.h>
    main()
    {
    int choice;
    int num;
    float euro;
    float sterling;
    float krone;
    float yen;
    float doll;

    printf("\n\n\nWELCOME TO ORLA'S CURRENCY CONVERTER!\n\n");

    while (num >=1 && num <=5);
    {

    printf("Please select the number of the country which currency you wish to use.\n\n");
    printf("1. Britain\n");
    printf("2. Denmark\n");
    printf("3. Japan\n");
    printf("4. Usa\n"); // Main menu selection
    printf("5. Exit Program\n\n");

    rewind(stdin);
    scanf("%d", &num);
    choice=0;
    switch (num)
    {
    case 1:
    //If Britain is picked then this will be displayed
    printf("You have chosen Britain.\n\n");
    printf("Please select an option:\n\n 1.Euro to Sterling\n\n 2.Sterling to Euro\n\n"); // will display the 2 options

    rewind(stdin);
    scanf("%d", &num);

    while (choice==0)
    {
    if (num ==1)
    {
    printf("You have chosen Euro to Sterling\n");
    printf("\nPlease enter the amount which you would like to convert:\n\n");

    scanf("%f", &euro);
    sterling = euro*.70;
    printf("The amount in Sterling is %5.2f\n\n\n",sterling);
    choice =1;
    }

    else if (num ==2)
    {
    printf("You have chosen Sterling to Euro\n");
    printf("\nPlease enter the amount which you would like to convert:\n\n");

    scanf("%f", &sterling);
    euro = sterling*1.42;

    printf("The amount in euro is %5.2f\n\n\n",euro);
    choice=2: }
    else
    {
    printf("Error!\nPlease press 1 or 2 only!\n\n");
    rewind(stdin);
    scanf("%d",&num); }
    }
    break;

  10. #10
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Im guessing you need to break out of your switch statement, look at the example i posted earlier and compare it to the structure of your switch statement.

    Else just post the while loop, read the anouncements on how to use the code tags (hint: play with the # button when writing posts)
    Microsoft is merely an illusion, albeit a very persistant one.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    You need to add a break at the end of your last case statement (before the default). Without the break you'll fall through to the next case.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    Post

    #include<stdio.h>
    main()
    {
    int choice;
    int num;
    float euro;
    float sterling;
    float krone;
    float yen;
    float doll;

    printf("\n\n\nWELCOME TO ORLA'S CURRENCY CONVERTER!\n\n");

    while (num >=1 && num <=5);
    {

    printf("Please select the number of the country which currency you wish to use.\n\n");
    printf("1. Britain\n");
    printf("2. Denmark\n");
    printf("3. Japan\n");
    printf("4. Usa\n"); // Main menu selection
    printf("5. Exit Program\n\n");

    rewind(stdin);
    scanf("%d", &num);
    choice=0;
    switch (num)
    {
    case 1:
    //If Britain is picked then this will be displayed
    printf("You have chosen Britain.\n\n");
    printf("Please select an option:\n\n 1.Euro to Sterling\n\n 2.Sterling to Euro\n\n"); // will display the 2 options

    rewind(stdin);
    scanf("%d", &num);

    while (choice==0)
    {
    if (num ==1)
    {
    printf("You have chosen Euro to Sterling\n");
    printf("\nPlease enter the amount which you would like to convert:\n\n");

    scanf("%f", &euro);
    sterling = euro*.70;
    printf("The amount in Sterling is %5.2f\n\n\n",sterling);
    choice =1;
    }

    else if (num ==2)
    {
    printf("You have chosen Sterling to Euro\n");
    printf("\nPlease enter the amount which you would like to convert:\n\n");

    scanf("%f", &sterling);
    euro = sterling*1.42;

    printf("The amount in euro is %5.2f\n\n\n",euro);
    choice=2: }
    else
    {
    printf("Error!\nPlease press 1 or 2 only!\n\n");
    rewind(stdin);
    scanf("%d",&num); }
    }
    break;

    then the next 3 case statements are the same

    untill the 5th

    case 5:
    {
    printf("Thank you for using Orla's Currency Converter!\n\n");
    printf("Have a nice day!");
    }
    default:
    printf("Please type number 1-5 only");




    }//end of switch
    }//while end
    }//end of main

  13. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Quote Originally Posted by itsme86
    You need to add a break at the end of your last case statement (before the default). Without the break you'll fall through to the next case.

    thanks so much.... that fixed it

    i knew it was only something small

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You have clearly not done what quzah told you. Put [/code] at the end of your code, and [code] before it.

  15. #15
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Code:
    case 5:
    {
    printf("Thank you for using Orla's Currency Converter!\n\n");
    printf("Have a nice day!");
    }
    Code:
    case 5:
    printf("Thank you for using Orla's Currency Converter!\n\n");
    printf("Have a nice day!");
    break;
    Microsoft is merely an illusion, albeit a very persistant one.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 10:20 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 11:54 AM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 03:17 PM
  4. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 05:08 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21