weird question

This is a discussion on weird question within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int number, flag=1; int w,h,n; int tall,wide,position,x=0,y=0,a=0; char line[50],common[50]; FILE *ifp; void readfile(); ...

  1. #1
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    weird question

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    	int number, flag=1;
    	int w,h,n;
    
    	int tall,wide,position,x=0,y=0,a=0;
    	char line[50],common[50];
    	FILE *ifp;
    
    void readfile();
    
    main()
    {
    	int x;
    
    	while(x!=0)
    	{
    		printf("\n (1) ReadFile Function\n  ");
    		scanf("%i",&x);
    
    		if (x==1)
    		{
    			readfile();
    			
    		}
    	}
    }
    
    
    void readfile()
    {
    
    	if ((ifp=fopen("objects.dat", "r")) == NULL)
    	{
    		printf("\n  * error opening file objects.dat *");
    	}
    
    	fgets(line,100,ifp);
    	position=(strlen(line)-1);
    
    	while (position>0)
    	{ 
    		if(line[x]==' ')
    		{     number++;    }
    		position--; x++;
    	}
    		
    	number+=1; x=0;
    
    	while (number>0)
    		{	
    			
    			if(line[y]=' ')
    			{
    				if (flag==5)
    				{
    					n=atoi(common);  flag+=1;  common==0;  x=0;  number--;  break;
    				}
    				
    				if (flag==4)
    				{
    					h=atoi(common);  flag+=1;  common==0;  x=0;  number--;
    				}
    				
    				if (flag==3)
    				{
    					w=atoi(common);  flag+=1;  common==0;  x=0;  number--;
    				}
    				
    				if (flag==2)
    				{
    					tall=atoi(common); common==0; flag+=1; x=0;   number--; break;
    				}
    
    				if (flag==1)
    				{
    					wide=atoi(common);  flag+=1;  common==0;  x=0;  number--;
    				}
    			}
    
    			common[x]=line[y];
    			y++; x++;
    		}
    	
    	printf("\n Number 1: %i",wide);
    	printf("\n Number 2: %i\n\n\n",tall);
    }
    this was a function i wrote today but it isn't working properly........it's just telling me Number 1 and 2 = 0.....can anyone run this and tweak with it and an "objects.dat" file to try to make it work?? i know it looks odd but can anyone mess with it real fast? Thanks.

  2. #2
    Unregistered
    Guest
    Works great!

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    The first problem is that you defined 'x' globally and set it equal to 0, so it will never enter the while loop in main. If I get time later I'll try to look at it some more.

    See ya

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >int tall,wide,position,x=0,y=0,a=0;
    Unnecessary, global variables are initialized the their equivalent of all bits zero automatically.

    >if(line[y]=' ')
    Oopsie, assignment inside a conditional. You probably meant ==, not =.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A weird question.
    By IanKoro in forum C Programming
    Replies: 19
    Last Post: 06-08-2009, 12:00 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Weird Question maybe
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2001, 06:30 PM

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