Problem with DO-WHILE Loop

Discussion in 'C Programming' started by Redduck, Sep 21, 2004.

  1. Redduck

    Redduck Guest

    Hello everyone. I am frustrated, I have written the simple program
    below for a class and I am having problems with the DO-WHILE loop. On
    the first run through the loop, everything works well, the menu is
    displayed, the input is registered and the loop runs. On the second
    (and following) runs the menu is printed twice. I am sure there is
    something very basic that I am missing, but I cannot see it. Can
    anyone help?

    Thanks in advance.

    Ken

    ****Here is the code****

    /* currency.c - Version 1.2
    This program receives a selection from the
    user to determine the type of currency they
    would like the exchange rate for. It then returns
    the value of the amount of US currency they entered
    in the currency type that they selected.*/

    #include <stdio.h>

    int main()
    {
    char nation; //Declare variable for the users choice of currencies

    float us_dollar;
    float canada_conv;
    float euro_conv;
    float krona_conv;
    float yen_conv;
    float pound_conv; //Declare variables of Float type

    int cont = 0;

    canada_conv = 1.2997; //Set exchange rates for all variables
    euro_conv = 1.2056;
    krona_conv = 7.5625;
    yen_conv = 0.009057;
    pound_conv = 0.7694;
    us_dollar = 1.00; //set initial dollar amount

    printf("Currency Conversion\n\n"); //Title of program

    do {

    cont = 0;

    //Prompt user for currency
    printf("Which currency would you like to convert?\n\n");
    printf("Press 'C' for Canadian Dollars.\n");
    printf("Press 'E' for Euros.\n");
    printf("Press 'K' for Swedish Krona.\n");
    printf("Press 'Y' for Japanese Yen.\n");
    printf("Press 'P' for British Pounds.\n\n");
    printf("Press 'Q' to quit.\n\n");

    nation=getchar(); //Retrieves users choice and assigns "nation"
    variable

    switch (nation) //Selection process to give amount based on user
    selection
    {
    case 'C':
    case 'c':
    printf("\nPlease enter the US dollar amount you want to
    convert.\n\n");
    scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    convert
    printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
    us_dollar, us_dollar * canada_conv);
    cont = 0;
    break;

    case 'E':
    case 'e':
    printf("\nPlease enter the US dollar amount you want to
    convert.\n\n");
    scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    convert
    printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
    * euro_conv);
    cont = 0;
    break;

    case 'K':
    case 'k':
    printf("\nPlease enter the US dollar amount you want to
    convert.\n\n");
    scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    convert
    printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
    us_dollar * krona_conv);
    cont = 0;
    break;

    case 'Y':
    case 'y':
    printf("\nPlease enter the US dollar amount you want to
    convert.\n\n");
    scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    convert
    printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
    us_dollar * yen_conv);
    cont = 0;
    break;

    case 'P':
    case 'p':
    printf("\nPlease enter the US dollar amount you want to
    convert.\n\n");
    scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    convert
    printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
    us_dollar * pound_conv);
    cont = 0;
    break;

    case 'Q':
    case 'q':
    cont = 1;
    printf("Thanks for using the Currency Conversion program.\n\n");
    break;

    default: //This is the error check. If the enter an incorrect
    choice, they get shut down
    break;
    }

    } while (cont!=1);


    //Give user time to see results and end program
    printf("\n\nPress any key and Enter to exit the program.\n");
    scanf("%d");
    return 0;
    }
    Redduck, Sep 21, 2004
    #1
    1. Advertising

  2. Redduck

    Pete Gray Guest

    Hello Ken,

    You probably have "leftover characters" in the input buffer following the
    call to scanf() - I've seen this before with progs that mix scanf() and
    getchar(). Easiest solution, for this program, is not to mix them. Flushing
    the buffer would probably work too, but I've not tried it.

    Regards,
    -Pete.

    "Redduck" <> wrote in message
    news:...
    > Hello everyone. I am frustrated, I have written the simple program
    > below for a class and I am having problems with the DO-WHILE loop. On
    > the first run through the loop, everything works well, the menu is
    > displayed, the input is registered and the loop runs. On the second
    > (and following) runs the menu is printed twice. I am sure there is
    > something very basic that I am missing, but I cannot see it. Can
    > anyone help?
    >
    > Thanks in advance.
    >
    > Ken
    >
    > ****Here is the code****
    >
    > /* currency.c - Version 1.2
    > This program receives a selection from the
    > user to determine the type of currency they
    > would like the exchange rate for. It then returns
    > the value of the amount of US currency they entered
    > in the currency type that they selected.*/
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char nation; //Declare variable for the users choice of currencies
    >
    > float us_dollar;
    > float canada_conv;
    > float euro_conv;
    > float krona_conv;
    > float yen_conv;
    > float pound_conv; //Declare variables of Float type
    >
    > int cont = 0;
    >
    > canada_conv = 1.2997; //Set exchange rates for all variables
    > euro_conv = 1.2056;
    > krona_conv = 7.5625;
    > yen_conv = 0.009057;
    > pound_conv = 0.7694;
    > us_dollar = 1.00; //set initial dollar amount
    >
    > printf("Currency Conversion\n\n"); //Title of program
    >
    > do {
    >
    > cont = 0;
    >
    > //Prompt user for currency
    > printf("Which currency would you like to convert?\n\n");
    > printf("Press 'C' for Canadian Dollars.\n");
    > printf("Press 'E' for Euros.\n");
    > printf("Press 'K' for Swedish Krona.\n");
    > printf("Press 'Y' for Japanese Yen.\n");
    > printf("Press 'P' for British Pounds.\n\n");
    > printf("Press 'Q' to quit.\n\n");
    >
    > nation=getchar(); //Retrieves users choice and assigns "nation"
    > variable
    >
    > switch (nation) //Selection process to give amount based on user
    > selection
    > {
    > case 'C':
    > case 'c':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
    > us_dollar, us_dollar * canada_conv);
    > cont = 0;
    > break;
    >
    > case 'E':
    > case 'e':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
    > * euro_conv);
    > cont = 0;
    > break;
    >
    > case 'K':
    > case 'k':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
    > us_dollar * krona_conv);
    > cont = 0;
    > break;
    >
    > case 'Y':
    > case 'y':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
    > us_dollar * yen_conv);
    > cont = 0;
    > break;
    >
    > case 'P':
    > case 'p':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
    > us_dollar * pound_conv);
    > cont = 0;
    > break;
    >
    > case 'Q':
    > case 'q':
    > cont = 1;
    > printf("Thanks for using the Currency Conversion program.\n\n");
    > break;
    >
    > default: //This is the error check. If the enter an incorrect
    > choice, they get shut down
    > break;
    > }
    >
    > } while (cont!=1);
    >
    >
    > //Give user time to see results and end program
    > printf("\n\nPress any key and Enter to exit the program.\n");
    > scanf("%d");
    > return 0;
    > }
    Pete Gray, Sep 21, 2004
    #2
    1. Advertising

  3. Redduck

    Al Bowers Guest

    Redduck wrote:
    > Hello everyone. I am frustrated, I have written the simple program
    > below for a class and I am having problems with the DO-WHILE loop. On
    > the first run through the loop, everything works well, the menu is
    > displayed, the input is registered and the loop runs. On the second
    > (and following) runs the menu is printed twice. I am sure there is
    > something very basic that I am missing, but I cannot see it. Can
    > anyone help?
    >


    Using function getchar leaves a '\n' char in stdin. A quick
    fix would use function fgets in place of getchar. See below.
    >
    > ****Here is the code****
    >
    > /* currency.c - Version 1.2
    > This program receives a selection from the
    > user to determine the type of currency they
    > would like the exchange rate for. It then returns
    > the value of the amount of US currency they entered
    > in the currency type that they selected.*/
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char nation; //Declare variable for the users choice of currencies
    >


    make nation a char array, ie.
    char nation[8];

    > float us_dollar;
    > float canada_conv;
    > float euro_conv;
    > float krona_conv;
    > float yen_conv;
    > float pound_conv; //Declare variables of Float type
    >
    > int cont = 0;
    >
    > canada_conv = 1.2997; //Set exchange rates for all variables
    > euro_conv = 1.2056;
    > krona_conv = 7.5625;
    > yen_conv = 0.009057;
    > pound_conv = 0.7694;
    > us_dollar = 1.00; //set initial dollar amount
    >
    > printf("Currency Conversion\n\n"); //Title of program
    >
    > do {
    >
    > cont = 0;
    >
    > //Prompt user for currency
    > printf("Which currency would you like to convert?\n\n");
    > printf("Press 'C' for Canadian Dollars.\n");
    > printf("Press 'E' for Euros.\n");
    > printf("Press 'K' for Swedish Krona.\n");
    > printf("Press 'Y' for Japanese Yen.\n");
    > printf("Press 'P' for British Pounds.\n\n");
    > printf("Press 'Q' to quit.\n\n");
    >
    > nation=getchar(); //Retrieves users choice and assigns "nation"
    > variable
    >

    use fgets in place of getchar.

    fgets(nation, sizeof nation, stdin);

    > switch (nation) //Selection process to give amount based on user
    > selection


    switch(nation[0]);

    > {


    ........... snip .............


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Sep 21, 2004
    #3
  4. Redduck

    CBFalconer Guest

    Redduck wrote:
    >
    > Hello everyone. I am frustrated, I have written the simple program
    > below for a class and I am having problems with the DO-WHILE loop. On
    > the first run through the loop, everything works well, the menu is
    > displayed, the input is registered and the loop runs. On the second
    > (and following) runs the menu is printed twice. I am sure there is
    > something very basic that I am missing, but I cannot see it. Can
    > anyone help?


    .... snip code ...

    As usual the problem is the use of scanf for interactive input.
    Think about what is left in the input stream after executing that
    call. Also think about other methods of getting the users input
    which allow you to be sure of the input stream condition.

    --
    "It is not a question of staying the course, but of changing
    the course" - John Kerry, 2004-09-20
    "Ask any boat owner the eventual result of continuing the
    present course indefinitely" - C.B. Falconer, 2004-09-20
    CBFalconer, Sep 21, 2004
    #4
  5. Redduck

    ranjeet Guest

    The problem is of the clearence of the buffer:

    Now when u enter the choice then after that u have to clear the buffer also.

    As u know that *getchar* takes all the input into the buffer and then from
    there it takes the value to the allocated mamory.

    And the *getch* takes the each respective character and simultaneously stores
    into the allocated memory. and once it over flows the memory sapsce a error
    code is desplayed (this thing u have to handle manually)

    So try to use the getch instead of getchar:

    Or use the fflush function:

    in this u will be handle the problem.

    any way if u have the doubts then let me know.


    Regards
    Ranjeet



    (Redduck) wrote in message news:<>...
    > Hello everyone. I am frustrated, I have written the simple program
    > below for a class and I am having problems with the DO-WHILE loop. On
    > the first run through the loop, everything works well, the menu is
    > displayed, the input is registered and the loop runs. On the second
    > (and following) runs the menu is printed twice. I am sure there is
    > something very basic that I am missing, but I cannot see it. Can
    > anyone help?
    >
    > Thanks in advance.
    >
    > Ken
    >
    > ****Here is the code****
    >
    > /* currency.c - Version 1.2
    > This program receives a selection from the
    > user to determine the type of currency they
    > would like the exchange rate for. It then returns
    > the value of the amount of US currency they entered
    > in the currency type that they selected.*/
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char nation; //Declare variable for the users choice of currencies
    >
    > float us_dollar;
    > float canada_conv;
    > float euro_conv;
    > float krona_conv;
    > float yen_conv;
    > float pound_conv; //Declare variables of Float type
    >
    > int cont = 0;
    >
    > canada_conv = 1.2997; //Set exchange rates for all variables
    > euro_conv = 1.2056;
    > krona_conv = 7.5625;
    > yen_conv = 0.009057;
    > pound_conv = 0.7694;
    > us_dollar = 1.00; //set initial dollar amount
    >
    > printf("Currency Conversion\n\n"); //Title of program
    >
    > do {
    >
    > cont = 0;
    >
    > //Prompt user for currency
    > printf("Which currency would you like to convert?\n\n");
    > printf("Press 'C' for Canadian Dollars.\n");
    > printf("Press 'E' for Euros.\n");
    > printf("Press 'K' for Swedish Krona.\n");
    > printf("Press 'Y' for Japanese Yen.\n");
    > printf("Press 'P' for British Pounds.\n\n");
    > printf("Press 'Q' to quit.\n\n");
    >
    > nation=getchar(); //Retrieves users choice and assigns "nation"
    > variable
    >
    > switch (nation) //Selection process to give amount based on user
    > selection
    > {
    > case 'C':
    > case 'c':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
    > us_dollar, us_dollar * canada_conv);
    > cont = 0;
    > break;
    >
    > case 'E':
    > case 'e':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
    > * euro_conv);
    > cont = 0;
    > break;
    >
    > case 'K':
    > case 'k':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
    > us_dollar * krona_conv);
    > cont = 0;
    > break;
    >
    > case 'Y':
    > case 'y':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
    > us_dollar * yen_conv);
    > cont = 0;
    > break;
    >
    > case 'P':
    > case 'p':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
    > us_dollar * pound_conv);
    > cont = 0;
    > break;
    >
    > case 'Q':
    > case 'q':
    > cont = 1;
    > printf("Thanks for using the Currency Conversion program.\n\n");
    > break;
    >
    > default: //This is the error check. If the enter an incorrect
    > choice, they get shut down
    > break;
    > }
    >
    > } while (cont!=1);
    >
    >
    > //Give user time to see results and end program
    > printf("\n\nPress any key and Enter to exit the program.\n");
    > scanf("%d");
    > return 0;
    > }
    ranjeet, Sep 21, 2004
    #5
  6. Redduck

    ranjeet Guest

    Here is your code corredtion it will work fine..... Enjoy :

    "Pete Gray" <> wrote in message news:<gxL3d.19922$wV.4542@attbi_s54>...
    > Hello Ken,
    >
    > You probably have "leftover characters" in the input buffer following the
    > call to scanf() - I've seen this before with progs that mix scanf() and
    > getchar(). Easiest solution, for this program, is not to mix them. Flushing
    > the buffer would probably work too, but I've not tried it.
    >
    > Regards,
    > -Pete.
    >
    > "Redduck" <> wrote in message
    > news:...
    > > Hello everyone. I am frustrated, I have written the simple program
    > > below for a class and I am having problems with the DO-WHILE loop. On
    > > the first run through the loop, everything works well, the menu is
    > > displayed, the input is registered and the loop runs. On the second
    > > (and following) runs the menu is printed twice. I am sure there is
    > > something very basic that I am missing, but I cannot see it. Can
    > > anyone help?
    > >
    > > Thanks in advance.
    > >
    > > Ken
    > >
    > > ****Here is the code****
    > >
    > > /* currency.c - Version 1.2
    > > This program receives a selection from the
    > > user to determine the type of currency they
    > > would like the exchange rate for. It then returns
    > > the value of the amount of US currency they entered
    > > in the currency type that they selected.*/
    > >
    > > #include <stdio.h>
    > >
    > > int main()
    > > {
    > > char nation; //Declare variable for the users choice of currencies
    > >
    > > float us_dollar;
    > > float canada_conv;
    > > float euro_conv;
    > > float krona_conv;
    > > float yen_conv;
    > > float pound_conv; //Declare variables of Float type
    > >
    > > int cont = 0;
    > >
    > > canada_conv = 1.2997; //Set exchange rates for all variables
    > > euro_conv = 1.2056;
    > > krona_conv = 7.5625;
    > > yen_conv = 0.009057;
    > > pound_conv = 0.7694;
    > > us_dollar = 1.00; //set initial dollar amount
    > >
    > > printf("Currency Conversion\n\n"); //Title of program
    > >
    > > do {
    > >
    > > cont = 0;
    > >


    > > //Prompt user for currency
    > > printf("Which currency would you like to convert?\n\n");
    > > printf("Press 'C' for Canadian Dollars.\n");
    > > printf("Press 'E' for Euros.\n");
    > > printf("Press 'K' for Swedish Krona.\n");
    > > printf("Press 'Y' for Japanese Yen.\n");
    > > printf("Press 'P' for British Pounds.\n\n");
    > > printf("Press 'Q' to quit.\n\n");
    > >
    > > nation=getchar(); //Retrieves users choice and assigns "nation"
    > > variable
    > >

    getchar(); // this is used to take the character when u press the enter.
    // other wise it will re take the character agin and
    // whicle loop is going to operate two times.... here is the
    // main problem

    > > switch (nation) //Selection process to give amount based on user
    > > selection
    > > {
    > > case 'C':
    > > case 'c':
    > > printf("\nPlease enter the US dollar amount you want to
    > > convert.\n\n");
    > > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > > convert
    > > printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
    > > us_dollar, us_dollar * canada_conv);
    > > cont = 0;
    > > break;
    > >
    > > case 'E':
    > > case 'e':
    > > printf("\nPlease enter the US dollar amount you want to
    > > convert.\n\n");
    > > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > > convert
    > > printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
    > > * euro_conv);
    > > cont = 0;
    > > break;
    > >
    > > case 'K':
    > > case 'k':
    > > printf("\nPlease enter the US dollar amount you want to
    > > convert.\n\n");
    > > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > > convert
    > > printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
    > > us_dollar * krona_conv);
    > > cont = 0;
    > > break;
    > >
    > > case 'Y':
    > > case 'y':
    > > printf("\nPlease enter the US dollar amount you want to
    > > convert.\n\n");
    > > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > > convert
    > > printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
    > > us_dollar * yen_conv);
    > > cont = 0;
    > > break;
    > >
    > > case 'P':
    > > case 'p':
    > > printf("\nPlease enter the US dollar amount you want to
    > > convert.\n\n");
    > > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > > convert
    > > printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
    > > us_dollar * pound_conv);
    > > cont = 0;
    > > break;
    > >
    > > case 'Q':
    > > case 'q':
    > > cont = 1;
    > > printf("Thanks for using the Currency Conversion program.\n\n");
    > > break;
    > >
    > > default: //This is the error check. If the enter an incorrect
    > > choice, they get shut down
    > > break;
    > > }

    >
    > > } while (cont!=1);
    > >
    > >
    > > //Give user time to see results and end program
    > > printf("\n\nPress any key and Enter to exit the program.\n");
    > > scanf("%d");


    why u r using scanf("%d");
    remove this.....
    > > return 0;
    > > }
    ranjeet, Sep 21, 2004
    #6
  7. Redduck

    pete Guest

    ranjeet wrote:

    > use the getch instead


    getch isn't part of the standard library.
    What you do in the privacy of your own home,
    is your own business, but no getch here.

    --
    pete
    pete, Sep 21, 2004
    #7
  8. Redduck

    Richard Bos Guest

    (ranjeet) wrote:

    [ Learn to snip. Do not use cuteisms such as "u" for "you".
    Do not top-post. ]

    > The problem is of the clearence of the buffer:
    >
    > Now when u enter the choice then after that u have to clear the buffer also.
    >
    > As u know that *getchar* takes all the input into the buffer and then from
    > there it takes the value to the allocated mamory.
    >
    > And the *getch*


    There is no getch() in C. There's a getch() in curses, and one in conio;
    neither is on-topic here, and in fact IIRC they behave similarly, but
    not identically.

    > So try to use the getch instead of getchar:


    Do not do this.

    > Or use the fflush function:


    You can't fflush() an input stream.

    All you need to do is filter out the newlines. With the hints of the
    other posts in this thread, that shouldn't be hard.

    Richard
    Richard Bos, Sep 21, 2004
    #8
  9. Redduck

    CBFalconer Guest

    ranjeet wrote:
    >
    > The problem is of the clearence of the buffer:
    >
    > Now when u enter the choice then after that u have to clear the buffer also.


    Please do not use silly abbreviations such as 'u' in c.l.c. It is
    highly annoying and hard to read. Also do not toppost, your
    answer belongs after (or intermixed with) the material to which
    you reply, after snipping anything not germane to your answer.

    --
    "It is not a question of staying the course, but of changing
    the course" - John Kerry, 2004-09-20
    "Ask any boat owner the eventual result of continuing the
    present course indefinitely" - C.B. Falconer, 2004-09-20
    CBFalconer, Sep 21, 2004
    #9
  10. Redduck

    ranjeet Guest

    (Redduck) wrote in message news:<>...
    > Hello everyone. I am frustrated, I have written the simple program
    > below for a class and I am having problems with the DO-WHILE loop. On
    > the first run through the loop, everything works well, the menu is
    > displayed, the input is registered and the loop runs. On the second
    > (and following) runs the menu is printed twice. I am sure there is
    > something very basic that I am missing, but I cannot see it. Can
    > anyone help?
    >
    > Thanks in advance.
    >
    > Ken
    >
    > ****Here is the code****
    >
    > /* currency.c - Version 1.2
    > This program receives a selection from the
    > user to determine the type of currency they
    > would like the exchange rate for. It then returns
    > the value of the amount of US currency they entered
    > in the currency type that they selected.*/
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char nation; //Declare variable for the users choice of currencies
    >
    > float us_dollar;
    > float canada_conv;
    > float euro_conv;
    > float krona_conv;
    > float yen_conv;
    > float pound_conv; //Declare variables of Float type
    >
    > int cont = 0;
    >
    > canada_conv = 1.2997; //Set exchange rates for all variables
    > euro_conv = 1.2056;
    > krona_conv = 7.5625;
    > yen_conv = 0.009057;
    > pound_conv = 0.7694;
    > us_dollar = 1.00; //set initial dollar amount
    >
    > printf("Currency Conversion\n\n"); //Title of program
    >
    > do {
    >
    > cont = 0;
    >
    > //Prompt user for currency
    > printf("Which currency would you like to convert?\n\n");
    > printf("Press 'C' for Canadian Dollars.\n");
    > printf("Press 'E' for Euros.\n");
    > printf("Press 'K' for Swedish Krona.\n");
    > printf("Press 'Y' for Japanese Yen.\n");
    > printf("Press 'P' for British Pounds.\n\n");
    > printf("Press 'Q' to quit.\n\n");
    >
    > nation=getchar(); //Retrieves users choice and assigns "nation"
    > variable
    >

    OVER HERE U JUST ADD ONE MORE STAEMENT getchar();
    every thing will work fine.

    > switch (nation) //Selection process to give amount based on user
    > selection
    > {
    > case 'C':
    > case 'c':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
    > us_dollar, us_dollar * canada_conv);
    > cont = 0;
    > break;
    >
    > case 'E':
    > case 'e':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
    > * euro_conv);
    > cont = 0;
    > break;
    >
    > case 'K':
    > case 'k':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
    > us_dollar * krona_conv);
    > cont = 0;
    > break;
    >
    > case 'Y':
    > case 'y':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
    > us_dollar * yen_conv);
    > cont = 0;
    > break;
    >
    > case 'P':
    > case 'p':
    > printf("\nPlease enter the US dollar amount you want to
    > convert.\n\n");
    > scanf("%f", &us_dollar); //Gather the dollar amount they wish to
    > convert
    > printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
    > us_dollar * pound_conv);
    > cont = 0;
    > break;
    >
    > case 'Q':
    > case 'q':
    > cont = 1;
    > printf("Thanks for using the Currency Conversion program.\n\n");
    > break;
    >
    > default: //This is the error check. If the enter an incorrect
    > choice, they get shut down
    > break;
    > }
    >
    > } while (cont!=1);
    >
    >
    > //Give user time to see results and end program
    > printf("\n\nPress any key and Enter to exit the program.\n");
    > scanf("%d");
    > return 0;
    > }
    ranjeet, Sep 22, 2004
    #10
  11. CBFalconer <> scribbled the following:
    > ranjeet wrote:
    >>
    >> The problem is of the clearence of the buffer:
    >>
    >> Now when u enter the choice then after that u have to clear the buffer also.


    > Please do not use silly abbreviations such as 'u' in c.l.c. It is
    > highly annoying and hard to read.


    I would have said that but it would have caused yet another flamewar.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
    and vow excitedly zooming."
    - JIPsoft
    Joona I Palaste, Sep 22, 2004
    #11
  12. On 21 Sep 2004 04:12:12 -0700, (ranjeet)
    wrote:

    >> > nation=getchar(); //Retrieves users choice and assigns "nation"
    >> > variable
    >> >

    > getchar(); // this is used to take the character when u press the enter.
    > // other wise it will re take the character agin and
    > // whicle loop is going to operate two times.... here is the
    > // main problem


    And what happens if the user happens to type in multiple characters
    before hitting enter?


    <<Remove the del for email>>
    Barry Schwarz, Sep 23, 2004
    #12
  13. Redduck

    ranjeet Guest

    Barry Schwarz <> wrote in message news:<cit7bn$4i0$0$>...
    > On 21 Sep 2004 04:12:12 -0700, (ranjeet)
    > wrote:
    >
    > >> > nation=getchar(); //Retrieves users choice and assigns "nation"
    > >> > variable
    > >> >

    > > getchar(); // this is used to take the character when u press the enter.
    > > // other wise it will re take the character agin and
    > > // whicle loop is going to operate two times.... here is the
    > > // main problem

    >
    > And what happens if the user happens to type in multiple characters
    > before hitting enter?
    >

    As each charcter will be replaced with the cuurent chararcter of multilipe
    chararcter.
    >
    > <<Remove the del for email>>
    ranjeet, Sep 30, 2004
    #13
  14. On 29 Sep 2004 22:44:34 -0700, (ranjeet)
    wrote:

    >Barry Schwarz <> wrote in message news:<cit7bn$4i0$0$>...
    >> On 21 Sep 2004 04:12:12 -0700, (ranjeet)
    >> wrote:
    >>
    >> >> > nation=getchar(); //Retrieves users choice and assigns "nation"
    >> >> > variable
    >> >> >
    >> > getchar(); // this is used to take the character when u press the enter.
    >> > // other wise it will re take the character agin and
    >> > // whicle loop is going to operate two times.... here is the
    >> > // main problem

    >>
    >> And what happens if the user happens to type in multiple characters
    >> before hitting enter?
    >>

    > As each charcter will be replaced with the cuurent chararcter of multilipe
    > chararcter.


    Not until the whole loop iterates again, confusing the user just as it
    did the OP.


    <<Remove the del for email>>
    Barry Schwarz, Oct 2, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,216
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,214
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    474
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    415
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page