Thread: Do while help please

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    Do while help please

    Im making a do while loop for C and the part I'm having trouble with looks like this


    Code:
    char y;
    
    do
    	{
    	printf("On this machine the number of bytes in a char is : %d\n", sizeof(char));
    
    	printf("On this machine the number of bytes in a short is : %d\n", sizeof(short));
    
    	printf("On this machine the number of bytes in a int is : %d\n", sizeof(int));
    
    	printf("On this machine the number of bytes in a long is : %d\n", sizeof(long));
    
    	printf("On this machine the number of bytes in a float is : %d\n", sizeof(float));
    
    	printf("On this machine the number of bytes in a double is : %d\n", sizeof(double));
    
    	printf("On this machine the number of bytes in a pointer is : %d\n", sizeof(char));
    
    	printf("On this machine the number of bytes in a struct is : %d\n", sizeof(struct student));
    
    	printf("Would you like to see the information again?\n");
    
    
    
    y = getchar();
    
    }
    
    while (scanf("%s",&y));
    
    while (y == 'y');
    
    while (y != EOF);
    
    
    }

    Well im new to this programming stuff, but im suppose to make a program that displays all those "On this machine" quotes everytime the user enters the letter "y". And if the user enters anything else, the program is suppose to print out : "Goodbye" and exit the program. The problem is that whenever i execute and enter "y", it prints out "on this machine", which is what I want. But i dont know how to get it to print "goodbye" and exit.

    If anyone could help me, I will love you long time.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by joshua
    Im making a do while loop for C and the part I'm having trouble with looks like this


    Code:
    char y;
    
    do
    {
        y = getchar(); /* Read something into y*/
    
    } while (scanf("%s",&y)); /* Now treat the single character y as a string... */
    /* Your do-while is officially over. */
    
    while (y == 'y'); /* while y is 'y', do nothing at all, indefinately */
    /* Your plain old 'while' loop is over, if at all possible. */
    
    while (y != EOF);  /* See above, except test for EOF, except you can't. */
    /* Your THIRD loop is now over. */
    
    
    }
    The EOF FAQ will tell you why it's wrong to use a char to test for EOF.

    Keep the love for someone who wants it.


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

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    when you say my do while loop is over, do you mean it wont loop anymore?

  4. #4
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Ermm, is it just me, or is this thread somehow familiar to this one?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    I Love You!!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > or is this thread somehow familiar to
    Well both posters are from the same school, so I guess they're both in the same class as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    int y = 0;
    
    do
    {
       /* statements */
      y = getchar();
    } while (y == 'y' || y == 'Y');
    Something similair to this is what you want.

Popular pages Recent additions subscribe to a feed