Thread: Loops forever

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    23

    Loops forever

    Hi.. I am doing C programming on microsoft Visual C++. I want to prompt the user for coordinate again and again if the input coordinate doesn't meet the condition. But after I type in the coordinate, the program keeps on looping infinitely. Sorry for the super BEGiNNER question.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    
    	int x,y,invalid=1;
    
    	while (invalid)
    	{
    		printf("Please enter the coordinate of the ship:\n");
    		scanf(" %d %d", &x,&y);
    
    		invalid=((x>10 || x<0) &&(y<10 || y>25))?1:0;
    	}
    	
    }

    I want to prompt the user again for coordinates if the user types a coordinate within this range
    (x>10 || x<0) &&(y<10 || y>25)

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    I think you need to look up the Truth tabels for A?ND , OR and is there an XOR ?


    well .. it seems that ..

    if i entered 11 and 1

    then your code should stop asking right ?



    X is greater than 10 OR x is less than Zero

    ( x>10 || x<0 )

    Well yes it is Greater than 10 .. so we have a 1 OR less than Zero ?

    no its not less than Zero ..so we have a Zero ..

    1 OR 0 = 1

    next

    ( y<10 || y>25 ) . y is 1

    so yes y is less than 10 .. 1
    is it greater than 25 ? nope .. so a zero 0

    1 OR 0 = ?? EQUALS 1

    1 AND 1 == 1


    yes One .. Infinite loop

    now lets try something that dosnt match ...

    ...9...

    X is greater than 10 and X less than Zero ..

    nope ?
    its neither .. 0 OR 0 = 0

    Next one .. Lets use 1 ..

    Y les than 10 or greater than 25 ? yup its less than 10 and not greater than 25

    1 OR 1 = 1

    1 AND 0 = ?? 1

    there is problem in the logic

    You see even when one of the Value is not right ..you still get a One and when both values are right you get a One also ..

    Truth Tables
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    thanks for the detailed reply man.. I got it. My condition was wrong..

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    If it were me, I'd rather do something like this. The user gets feedback, and the code is easier to read and understand.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    
    	int x,y,invalid=1;
    
    	while (invalid == 1)
    	{
    		printf("Please enter the coordinate of the ship:\n");
    		scanf(" %d %d", &x,&y);
    
    		if ((x>=0 && x<=10) && (y>=10 && y<=25)) {
    			invalid = 0;
    		} /* if ((x>=0 && x<=10) && (y>=10 && y<=25)) */
    		else {
    			printf("Invalid.\n");
    			invalid = 1;
    		}/* else */
    	} /* while (invalid == 1) */
    	
    }/* main */

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    yours is much neater cosmic_cow.. I've another problem.

    I need the x coordinate to be between 0 and 19, and the
    y coordinate to be between 10 and 19.[As stated in the condition]

    I tried entering a coordinate of (5,5). This is supposed to yield
    a value of 1. then it's supposed to go through the while loop again but it's not looping! Can someone tell me why? thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    int main()
    {
    
    	int x,y,i,j,A,invalid=1,rc;
    	
    	FILE* fw = fopen("status.txt", "r");
    	FILE* fp = fopen("status.txt", "w");
    	
    	/*Creating a text file which has 20 rows of 20 zeroes*/
    	for(i=0;i<20;i++)
    	{
    		for(j=0;j<20;j++)
    			fprintf(fp,"%d ", 0);
    		fprintf(fp,"\n");
    	}
    	fclose(fp);
    	
    
    	while(invalid)
    	{
    		printf("Please enter the coordinate:\n");
    		scanf(" %d %d", &x,&y);
    
    		invalid=((x>0 && x<19) &&(y>10 && y<19))? 0: 1;
    		printf("The value of invalid the first time is %d\n", invalid);
    		A=20*y+x;
    		if(invalid=0)
    		{	
    			/*Reading the status.txt file*/
    			for (i=0;i<=A;i++)
    			{
    				fscanf(fw, "%d ", &rc);
    			}	
    			fclose(fw);
    				if (rc==1)
    					invalid=1;	
    				else 
    					invalid=0; 
    		}
    	}
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program needs another variable, like int pair = 0, before the while loop starts.

    Then, when you get your first pair, set the variable pair to 1. Inside the while loop, after invalid is set to 0, you need code something like:
    Code:
    //this should be placed so it runs ONLY when the 
    //first set of coordinates is valid
    if(pair == 0) {  //or if(!pair) {
      invalid = 1;
      pair = 1;
    }
    That will cause a second time through the while loop, and if your other logic is ok, the second set of coordinates can be assigned.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    - You cannot open a file for "r" before even creating it. If the file exists, under unix, you may get results but not what you expected. If the file doesn't exist then 'fw' is NULL throughout execution and you have not checked for this. What's the point of this file anyway? Are we trying to do some sort of named pipe at this programming level?? Anyway do your "r" open after you've written the whole file and fclose()'d it when it gets flushed.

    - I think you meant "if(invalid==0)" rather than "if(invalid=0)"?

    - If you close 'fw' then it's not open for the next pass of the loop and stuff will fail. But again you haven't checked for that so you'd never see it.

    gcc:32: Confused by earlier errors, giving up.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    Thanks adak and core_cpp! Changing the if(invalid=0) to if(invalid==0) solved the problem. I am creating the status.txt first and then reading it..

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    Another small question.. what does the following statement mean? invalid and val are variables..
    Thanks..
    invalid = invalid?1:val?1:0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loops and macros
    By ginom71 in forum C Programming
    Replies: 13
    Last Post: 07-18-2009, 01:04 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. Writing forever loops - using for(;;) or while(1)?
    By hzmonte in forum C Programming
    Replies: 8
    Last Post: 07-15-2005, 09:02 PM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM