C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 08:21 PM   #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
Darkw1sh is offline   Reply With Quote
Old 09-12-2009, 08:41 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,805
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.
tabstop is offline   Reply With Quote
Old 09-12-2009, 08:47 PM   #3
Registered User
 
Join Date: Sep 2009
Posts: 10
that didnt help at all!
Darkw1sh is offline   Reply With Quote
Old 09-12-2009, 08:50 PM   #4
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,070
A WHILE loop written this way will never be entered:
Code:
while (loop == FALSE)
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 09-12-2009, 08:56 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,805
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.
tabstop is offline   Reply With Quote
Old 09-12-2009, 08:58 PM   #6
Registered User
 
Join Date: Sep 2006
Posts: 3,139
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.
Adak is offline   Reply With Quote
Old 09-12-2009, 09:04 PM   #7
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,070
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.
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 09-12-2009, 09:18 PM   #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.
Darkw1sh is offline   Reply With Quote
Old 09-12-2009, 09:19 PM   #9
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,637
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 09-12-2009, 09:20 PM   #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
Darkw1sh is offline   Reply With Quote
Old 09-12-2009, 09:22 PM   #11
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,805
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.
tabstop is offline   Reply With Quote
Old 09-12-2009, 09:48 PM   #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
Darkw1sh is offline   Reply With Quote
Old 09-12-2009, 10:56 PM   #13
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 810
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?
rags_to_riches is offline   Reply With Quote
Old 09-13-2009, 05:01 AM   #14
Registered User
 
Join Date: Sep 2006
Posts: 3,139
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
*/
Adak is offline   Reply With Quote
Old 09-13-2009, 09:00 PM   #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;
}
Darkw1sh is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
funny-looking while loop Aisthesis C++ Programming 3 08-30-2009 11:54 PM
nested loop, simple but i'm missing it big_brother C Programming 19 10-23-2006 10:21 PM
While loop misbehaving (or misunderstanding) mattAU C Programming 2 08-28-2006 02:14 AM
while loop help bliznags C Programming 5 03-20-2005 12:30 AM
loop issues kristy C Programming 3 03-05-2005 09:14 AM


All times are GMT -6. The time now is 04:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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