Thread: Do while loop

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Do while loop

    Hi everyone.

    I am writing a program. A snippet of the code is below. In particular, I am trying to do a data validation ( see ****** below). I'm asking the user for certain inputs, and then I get to the Do while statement below, and when I execute the program, and I get to the point where I ask the user for a Y/N, it prints out the printf twice. I don't know if I'm making any sense. I realize that this is happening because once I enter data for a certain statement, I hit enter and that triggers the "Do while". How do I stop it from printing the statement twice. I'm using the Microsoft C/C++ compiler...which I here is different than compilers like borland. I must use C/C++ though.

    printf("job grade? (soap handler=1, water manager=2, team manager=3,supervisor=4 :");
    scanf("%d",&grade);
    {
    if(grade != 2 && grade!= 3 && grade != 4)
    bonus2 = 25;

    else if(grade != 1 && grade!= 3 && grade != 4)
    bonus2= 45;

    else if(grade != 2 && grade!= 1 && grade != 4)
    bonus2 = 75;

    else if(grade != 2 && grade!= 3 && grade != 1)
    bonus2 = 100;
    }



    *************************************************
    //once I hit enter from the previous input, it triggers this do while and prints
    Employee worked 3 overtimes?
    You must enter a Y or a N

    Employee worked 3 overtimes?
    You must enter a Y or a N//


    do
    {
    printf("\nEmployee worked 3 overtimes? : ");
    printf("\nYou must enter a Y or a N : ");
    scanf("%c",&ans);
    *************************************************
    }
    while(!(ans == 'Y' || ans == 'N'));


    {
    if (ans =='Y')
    bonus3 = 50;

    else
    bonus3 = 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're using scanf, a notoriously buggy function that can be easily avoided in this situation. Since you are only reading one character from the input stream you can either use ans = getchar() to replace the call to scanf, or clear the stream with a simple loop so that scanf will work properly.

    It would also be a good idea to change the input to upper case in the event that the user enters a lower case letter. y and n should work as well as Y and N.
    Code:
    do 
    { 
        printf("\nEmployee worked 3 overtimes? : "); 
        printf("\nYou must enter a Y or a N : "); 
        while(getchar() != '\n')
            ;
        scanf("%c",&ans);
        toupper(ans);  
    } 
    while( (ans != 'Y' || ans != 'N') );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    In this context, wouldn't the result of toupper need to be assigned back to the 'ans' variable?

    Code:
    ans = toupper(ans);

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    No - toupper returns an integer and changes the string.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    On my system this code
    Code:
        
        char c = 'c';
    
        toupper(c);
    
        printf("(%c)\n", c);
    prints this: (c)

    whereas this code
    Code:
        char c = 'c';
    
        c = toupper(c);
    
        printf("(%c)\n", c);
    prints this: (C)

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    You're right - when I actually compiled that, it wouldn't even let me...

    It's Friday - my head's not totally here

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Thanks everyone for your help. I'm still a beginner so I'm not sure about the code you gave me. BUt, I made some alterations since the post I made. This is what I have now (look below). When I run it, it works when I type "Y" (it displays the answers I want) but when I type "N", it does nothing...the program simple stops. Also, when I type in a lower case y or n, it does nothing. What is the simpliest thing to do to make this work. Thank you again. Code below (note: this is not the entire program)

    {
    if(grade != 2 && grade!= 3 && grade != 4)
    bonus2 = 25;

    else if(grade != 1 && grade!= 3 && grade != 4)
    bonus2= 45;

    else if(grade != 2 && grade!= 1 && grade != 4)
    bonus2 = 75;

    else if(grade != 2 && grade!= 3 && grade != 1)
    bonus2 = 100;
    }

    printf("\nEmployee worked 3 overtimes? : ");
    printf("\nYou must enter a Y or a N : ");

    do

    { scanf("%c",&ans);
    if (ans =='Y')
    bonus3 = 50;

    else if (ans =='N')
    bonus3 = 0;

    }





    while(!(ans == 'Y') || (ans == 'N'));

    printf("\nBonus 1 = %d, Bonus 2 = %d, Bonus 3 = %d\n",bonus1,bonus2,bonus3);

  8. #8
    Barjor
    Guest
    This is one way you can check for Y and y

    if (ans =='Y' || and =='y')

    ~Barjor

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > when I type in a lower case y or n,

    That's because you're only checking for uppercase Y and N - see our discussion about toupper.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    I understand what your saying about the toupper...the main issue right now is when I type "N", it doesn't assign the value I want to a certain variable. Once thats fixed, then I want to beable to do a validity check on the case of the letter or some other character that a user may enter. If someone could just throw this in a compiler, it may make more sense to you. The entire code is below. Sorry for taking up space. Thanks



    #include <stdio.h>

    int main ()

    {

    int seniority;
    int employeenum;
    int bonus1;
    int bonus2;
    int grade;
    char ans;
    int bonus3;


    printf("Please enter the employee's number : ");
    scanf("%d",&employeenum);
    printf("\n");
    printf("How many years has the person been employed with BMW? : ");
    scanf("%d",&seniority);
    printf("\n");


    //Start of 1st Factor//

    {
    if(seniority>10 || seniority<0)
    printf("You entered an incorrect value. Please enter a value between 0-10 : ");

    if(seniority < 1)
    bonus1 = 25;

    else if (seniority >=1 && seniority <=3)
    bonus1 = 50;

    else if (seniority >=4 && seniority <=6)
    bonus1 = 75;

    else if (seniority >=7)
    bonus1 = 100;


    }
    //End of 1st Factor//



    printf("job grade? (soap handler=1, water manager=2, team manager=3,supervisor=4 :");
    scanf("%d",&grade);


    //Start of 2nd Factor//
    {
    if(grade != 2 && grade!= 3 && grade != 4)
    bonus2 = 25;

    else if(grade != 1 && grade!= 3 && grade != 4)
    bonus2= 45;

    else if(grade != 2 && grade!= 1 && grade != 4)
    bonus2 = 75;

    else if(grade != 2 && grade!= 3 && grade != 1)
    bonus2 = 100;
    }

    printf("\nEmployee worked 3 overtimes? : ");
    printf("\nYou must enter a Y or a N : ");

    do

    { scanf("%c",&ans);
    if (ans =='Y')
    bonus3 = 50;

    else if (ans =='N')
    bonus3 = 0;

    }





    while(!(ans == 'Y') || (ans == 'N'));

    printf("\nBonus 1 = %d, Bonus 2 = %d, Bonus 3 = %d\n",bonus1,bonus2,bonus3);




    return 0;
    }

  11. #11
    Barjor
    Guest
    I don't have a compiler here put I would try to write it like this instead

    { scanf("%c",&ans);
    if (ans =='Y')
    bonus3 = 50;

    if (ans =='N')
    bonus3 = 0;

    }

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Thanks for your input. I tried that and it still doesn't work. Its quite strange that it doesn't...I am stumped.

  13. #13
    Barjor
    Guest
    Me again..Still don't have a compiler...I don't get the logic in your last Do/While loop. Why do you have it there? What is the tought behind >> while(!(ans == 'Y' || ans == 'N')); << Not sure I see what you want it to do. Is the line >> printf("\nYou must enter a Y or a N : "); << not supposed to be inside the Do/While loop?. Am I confused? is it late friday and am I sick of work..YES.

  14. #14
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Originally posted by Barjor
    Me again..Still don't have a compiler...I don't get the logic in your last Do/While loop. Why do you have it there? What is the tought behind >> while(!(ans == 'Y' || ans == 'N')); << Not sure I see what you want it to do. Is the line >> printf("\nYou must enter a Y or a N : "); << not supposed to be inside the Do/While loop?. Am I confused? is it late friday and am I sick of work..YES.

    Well, the logic behind [while(!(ans == 'Y' || ans == 'N'));] is that it should execute the loop only when it is NOT a capital Y or a capital N. AS for the printf in the Do while, I'm a beginner and don't completely understand how everything is executed. I just want it to do a validity check to see if the user types in Y or N. If they type a 'y' or a 'n', it should execute the loop and tell the user that it did something wrong and to do it again. Also, depending upon whether they say Y or N, a value must be attached to their answer. At this point, my program works when I type a 'Y'...but not when I type a 'N'. I am quite frustrated as well. Sorry for the confusion. I had a long day of work as well.

  15. #15
    Unregistered
    Guest
    instead of while(!(ans == 'Y') || (ans == 'N'));

    try while(ans != 'Y' && ans != 'N');

    or while(!(ans == 'Y' || ans == 'N'));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM