Thread: need help with a loop

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    need help with a loop

    I have a program,
    and i need to create a loop....

    I want a do you want to continue loop I have been having so much trouble with it i have tried many things and its not working for example

    #define FALSE 0
    #define TRUE 1
    ........................
    loop=FALSE

    while (loop == FALSE)
    {
    /*STATEMENTS!!!!*/

    printf (" Would you like to try again , please enter y/n")
    scanf ("%c", loop)
    if ((loop ==y) || (loop==Y))
    loop=FALSE
    else if ((loop ==n) || (loop==N))
    loop=TRUE


    TY TY maybe have error checking or another loop for that section so if any1 enters G or w.e

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I assume you used 'y' and 'Y' etc. You should also look into [code] and [/code] in the sticky at the top of the forum.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    that didnt help at all!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A WHILE loop written this way will never be entered:
    Code:
    while (loop == FALSE)
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Darkw1sh View Post
    that didnt help at all!
    Well, maybe next time you should ask a question. Since the loop you posted works (assuming you use real character constants), it's hard to guess what you think the error is. (EDIT: If you weren't careful with your input in the part you didn't post, you might have to use " %c" (notice carefully the difference) in your scanf statement.)
    Quote Originally Posted by Dino View Post
    A WHILE loop written this way will never be entered:
    Code:
    while (loop == FALSE)
    What?
    Last edited by tabstop; 09-12-2009 at 09:00 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Something like this?

    Code:
    char loop;
    
    loop = 'y';
    
    while (loop == 'y' || loop == 'Y')
    {
       /*STATEMENTS!!!!*/
    
       printf (" Would you like to try again , please enter y/n")
       scanf ("%c", &loop)    //you forgot the ampersand which scanf() needs
       
    }
    You're Rube Goldberg of the loop was confusing the issue. KISS. If the variable loop is going to control the while statement, and you want it to be either 'y' or 'n' (or 'Y' or 'N'), then you don't need TRUE or FALSE, at all.

    Hope that makes sense.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tabstop View Post
    Well, maybe next time you should ask a question. Since the loop you posted works (assuming you use real character constants), it's hard to guess what you think the error is. (EDIT: If you weren't careful with your input in the part you didn't post, you might have to use " %c" (notice carefully the difference) in your scanf statement.)

    What?
    Oops - duh. Sorry about that. Too much wine with dinner.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    TY TY trying it now!


    Quote Originally Posted by Adak View Post
    Something like this?

    Code:
    char loop;
    
    loop = 'y';
    
    while (loop == 'y' || loop == 'Y')
    {
       /*STATEMENTS!!!!*/
    
       printf (" Would you like to try again , please enter y/n")
       scanf ("%c", &loop)    //you forgot the ampersand which scanf() needs
       
    }
    You're Rube Goldberg of the loop was confusing the issue. KISS. If the variable loop is going to control the while statement, and you want it to be either 'y' or 'n' (or 'Y' or 'N'), then you don't need TRUE or FALSE, at all.

    Hope that makes sense.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let me answer your next question: It's looping twice, because you're using scanf, and you should be using something better. See the FAQ about reading input from a user.


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

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Code:
    /*Included Files*/
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
    float side1, side2, side3, s, area;
    char loop, y,Y, n,N, ch;
    
    while (loop == 'y' || loop == 'Y')
    {
    
    
    
      /*Input*/
        printf  ("Enter the value for side 1:" );
        scanf ("%f", &side1);
        printf  ("Enter the value for side 2:" );
        scanf ("%f", &side2);
              printf  ("Enter the value for side 3:" );
        scanf ("%f, %c", &side3);
    	ch=getchar();
              /*Error Checking*/
         if (side1&&side2&&side3 !=0)
         {
    			if ((side1+side2<=side3) || (side1+side3<=side2) || (side2+side3<=side1))
    			printf ( "The triangle is invalid.\n");
    			else if ((side1==side2) && (side2==side3))
    			{
    				printf ( "The triangle is an equilateral triangle.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1==side2) || (side1==side3) || (side2==side3))
    			{
    				printf ( "The triangle is isosceles.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1!=side2) && (side2!=side3) && (side1!=side3))
    			{
    				printf ( "The triangle is scalene. \n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
       		}
    	 else printf ( "The triangle is invalid.\n");
       printf ( "Would you like to try another triangle? Enter Y/N:");
    	scanf ("%c", &loop);
    
    } 
    return 0;
    }

    this is what i got and for some reason the loop does not work at all it doesn't let me even run the program

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes. The loop only happens if loop is 'Y' or 'y'. Since loop is 'bob' that isn't true. You may want to initialize your variables.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    hrm its not working... no matter what i have done i am new to C/C++ and this loop wont work

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Listen...see this?
    Code:
    char loop, y,Y, n,N, ch;
    
    while (loop == 'y' || loop == 'Y')
    You never initialize the variable loop to a value. Therefore, it could contain ANY value. The chances of that value being 'Y' or 'y' are probably pretty slim, so you won't enter the loop.

    Do you get it?

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need the y,Y,n or N variables, but I left them in so you could polish it up.

    This is working code.


    Code:
    /*Included Files*/
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
      float side1, side2, side3, s, area;
      char loop, y,Y, n,N, ch;
    
      //with while loops you frequently need to "prime the pump" (assign the
      //value of your control variable).
      loop = 'y';   
      while (loop == 'y' || loop == 'Y')
      {
        /*Input*/
        printf  ("Enter the value for side 1:" );
        scanf ("%f", &side1);
        
        printf  ("Enter the value for side 2:" );
        scanf ("%f", &side2);
        
        printf  ("Enter the value for side 3:" );
        scanf ("%f, %c", &side3);
        
        /*Error Checking*/
        if (side1&&side2&&side3 !=0)
        {
    			if ((side1+side2<=side3) || (side1+side3<=side2) || (side2+side3<=side1))
    			  printf ( "The triangle is invalid.\n");
    			else if ((side1==side2) && (side2==side3))
    			{
    				printf ( "The triangle is an equilateral triangle.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1==side2) || (side1==side3) || (side2==side3))
    			{
    				printf ( "The triangle is isosceles.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1!=side2) && (side2!=side3) && (side1!=side3))
    			{
    				printf ( "The triangle is scalene. \n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
        }
    	  else printf ( "The triangle is invalid.\n");
    
        printf ( "Would you like to try another triangle? Enter Y/N:");
        //ch = getchar();
        scanf (" %c", &loop);
    
      } 
      ch++;              //just stops a stupid warning on my compiler
      return 0;
    
    }
    /*
    When you use scanf(), it may leave a newline char '\n', in the keyboard
    buffer. A following scanf() for a number, will skip that '\n' char, and act
    like you want.
    
    A following scanf() for a char or a string, will not. It will see the '\n'
    as the only char that you want to enter, and "skip" over the scan(), 
    goofing up your program.
    
    There are two common solutions:
    
    1) Use a getchar() to "pull" the '\n' char, off the keyboard buffer, or
    2) Put a space into the scanf() function, telling it to ignore the first
    char it runs across. 
    
    An example of #1: 
    ch = getchar();
    scanf ("%c", &loop);
    
    An example of #2:
    scanf (" %c", &loop); //note the space before the %c
    */

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    well thank you for everything i did a little research and got a lot done but now i have 1 last problem, if u run the following code when it asks yes or no to redo the code if i print yes it writes "would you like to redo....y/n?" and on the same line it prints "Please enter side 1:" how would i get rid of that repetition
    heres the code if i can explain anything in anymore detail please let me know

    Code:
    /*Included Files*/
    #include <stdio.h>
    #include <math.h>
    #define FALSE 0
    #define TRUE 1
    
    int main(void)
    {
    
    float side1, side2, side3, s, area;
    char loop,  ch, restart;
    loop= FALSE;
    
    
    while (loop == FALSE)
    {
    
    
    	restart = 0;
      /*Input*/
        printf  ("Enter the value for side 1:" );
        scanf ("%f", &side1);
        printf  ("Enter the value for side 2:" );
        scanf ("%f", &side2);
              printf  ("Enter the value for side 3:" );
        scanf ("%f, %c", &side3);
    	ch=getchar();
              /*Error Checking*/
         if (side1&&side2&&side3 !=0)
         {
    			if ((side1+side2<=side3) || (side1+side3<=side2) || (side2+side3<=side1))
    			printf ( "The triangle is invalid.\n");
    			else if ((side1==side2) && (side2==side3))
    			{
    				printf ( "The triangle is an equilateral triangle.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1==side2) || (side1==side3) || (side2==side3))
    			{
    				printf ( "The triangle is isosceles.\n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
    			else if ((side1!=side2) && (side2!=side3) && (side1!=side3))
    			{
    				printf ( "The triangle is scalene. \n");
    				s =(side1+side2+side3)/2;
    				area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
    				printf ("The area of the triangle is %6.2f\n", area);
    			}
       		}
    	 else printf ( "The triangle is invalid.\n");
    	
    	do
    	{
    	
    	printf ( "Would you like to try another triangle? Enter Y/N:");
    	scanf ("%c, %c", &loop);
    	
    	if ((loop=='Y')||(loop=='y'))
    	{
    		loop=FALSE;
    		restart = 1;
    	}
    	else if ((loop=='N') || (loop=='n'))
    	{
    		loop=TRUE;
    		restart=1;
    	}
    	else restart = 0;
    	}
    	while (restart==0);
    }
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. 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
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  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