loops in a main statement....

This is a discussion on loops in a main statement.... within the C Programming forums, part of the General Programming Boards category; Hello, all... I have a program and what it is trying to do is read in an outside file and ...

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

    Question loops in a main statement....

    Hello, all...

    I have a program and what it is trying to do is read in an outside file and take the 3 numbers in the files to read into row, columns, and colors. So it's something like 2 G B... 2 for the row, G for the column and B for black (there is black and white).

    I have all my functions working properly, I think. But the problem I am having is with my main statement and calling the functions properly. I've been fiddling around with/trying to get this part to work for about 2 hours and I'm just getting really frustrated, so I figured maybe I can get some help.

    so here is what I got so far...

    Code:
         
    #include<stdio.h>
    #define sentinel 100 X X
    
    void drawVert(char lastcol);
    void drawToStone(char curcol, char stonecol); 
    void drawStone(char stonecol, char lastcol, char color);
    void drawRestOfRow(char curcol, char lastcol);
    void drawEmptyRow(char lastcol); 
    
    int main(void)
    {
    	FILE* myin;
    	char column;
    	char lastcol='H';
    	char curcol='A';
    	char stonecol;
    	int stonerow;
    	int currow;
    	char color;
    
    	myin=fopen("fewstones.txt","r");
    	fscanf(myin, "%d %c %c", &stonerow, &stonecol, &color);
    
    	for (currow=1; currow<=8; currow++)
    	{
    		
    		if (currow != stonerow || curcol != stonecol)
    			drawEmptyRow(lastcol);
    		if (currow != 8)
    			drawVert(lastcol);
    
    		else
    		{
    			while (stonerow == currow)
    			{
    				drawToStone(curcol,lastcol);
    				drawStone(stonecol,lastcol,color);
    			}
    			if (curcol !=lastcol)
    				drawRestOfRow(curcol, lastcol);	
    			
    			curcol = stonecol + 1;
    			fscanf(myin, "%d %c %c", &stonerow, &stonecol, &color);
    		}
    	}
    	return(0);
    }
    
    void drawVert(char lastcol)
    {
    	char column='A';
    
    
    	for(column='A'; column<=lastcol; column++)
    		printf("|   ");
    	printf("\n");
    
    	for(column='A'; column<=lastcol; column++)
    		printf("|   ");
    	printf("\n");
    }
    
    void drawToStone(char curcol, char stonecol)
    {
    	for(curcol; curcol<stonecol; curcol++)
    	{
    		if(curcol == 'A')
    			printf("---");
    		else if(curcol == 'H')
    			printf("--");
    		else
    			printf("----");
    	}
    }
    
    void drawStone(char stonecol, char lastcol, char color)
    {
    	if(stonecol == 'A')
    		printf("%c%c-", color, color);
    	else if(stonecol == lastcol)
    		printf("%c%c", color, color);
    	else	
    		printf("%c%c%c-", color, color, color);
    }
    
    void drawRestOfRow(char curcol, char lastcol)
    {
    	for(curcol; curcol<lastcol; curcol++)
    	{
    		printf("----");
    	}
    	if(curcol==lastcol)
    		printf("--");
    }
    
    void drawEmptyRow(char lastcol)
    {
    	char column;
    	for(column='A'; column<lastcol; column++)
    		printf("----");
    
    	printf("-\n");
    }
    If someone can help me out and steer me in the right direction, or tell me even if I am going in the right direction that would be great!! If you want to know what the program is exactly doing, I put it briefly, I can explain more...... At this point I have lost hope and am just outright confused..... Please show me the light... thank you.

    -Ricky
    Last edited by xero18; 10-28-2005 at 12:49 PM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Your major problems seem to lie in this region:

    Code:
    while (stonerow == currow)
    			{
    				drawToStone(curcol,lastcol);
    				drawStone(stonecol,lastcol,color);
    			}
    			if (curcol !=lastcol)
    				drawRestOfRow(curcol, lastcol);	
    			
    			curcol = stonecol + 1;
    			fscanf(myin, "%d %c %c", &stonerow, &stonecol, &color);
    Take a look at the while statement there, besides the fact that stonerow HAS to be 8 for it to work(from your if-else statement) the while loop will never end. If stonerow is 8 then it will go on forever because stonerow will always equal currow, unless you change it, and you don't.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    void drawRestOfRow(char curcol, char lastcol)
    {
    	for(curcol; curcol<lastcol; curcol++)
    	{
    That code is pointless.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Durban even if I replace that it still doesn't work properly... And it's also if stonerow does not equal 8, not if stonerow equals 8...

    dwks, how is it pointless? I don't want it to run if those variables aren't correct and I also need a counter for curcol...

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Code:
    if (currow != 8)
        drawVert(lastcol);
    else ///////////IF CURROW == 8(Only the 8th column is drawn?)
    {
                    while (stonerow == currow)
    	{
    		drawToStone(curcol,lastcol);
    		drawStone(stonecol,lastcol,color);
    	}
    	if (curcol !=lastcol)
    		drawRestOfRow(curcol, lastcol);	
    			
    	curcol = stonecol + 1;
    	fscanf(myin, "%d %c %c", &stonerow, &stonecol, &color);
    }
    you really need to visualize what you are trying to do and write the correct logic from that. Debugging and stepping through to watch the values change might help you also. A simple row/column algorithm could be done as an O^2 algorithm. For Example:

    Code:
    for( int i=0; i<numRows; i++ )
    {
       printf("Row %d: ", i);
       for( int j=0; i<numCols; i++ )
       {
           //Do your checks here ie. Empty column
           //You could even read from the file here
           printf("%d   ", YourData[i][j]);
       }
       printf("\n");
    }
    Of course, your version could work. C++ comes with the functionality to do everything 135124 ways.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    dwks is referring to the expression highlighted in red:
    Code:
    for(curcol; curcol<lastcol; curcol++)
    It doesn't do anything, you might as well just do:
    Code:
    for (; curcol<lastcol; curcol++)

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Code:
    for(curcol; curcol<lastcol; curcol++)
    that syntax is correct, the real reason it doesn't work is because you can't iterate through a single char.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You can increment the value of a char, like any other variable. I didn't say the syntax was incorrect, I just pointed out what was pointless.
    Code:
    for (curcol; curcol<lastcol; curcol++)
    is the same as
    Code:
    for(; curcol<lastcol; curcol++).

  9. #9
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I agree it is the same. Pff your right, you can iterate a char. Its getting late hehe. I was thinking about iterating data that was already set like an array. I've been working with network headers for weeks now and it got drilled into my head that I couldn't go outside of the data range heh.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on main()
    By braddy in forum C Programming
    Replies: 9
    Last Post: 03-21-2008, 11:16 AM
  2. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  3. Anybody fix this simple code?
    By Killinger in forum C++ Programming
    Replies: 25
    Last Post: 08-09-2004, 01:53 PM
  4. void main or int main
    By subdene in forum C++ Programming
    Replies: 12
    Last Post: 04-09-2002, 08:25 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM

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