Thread: problem with printf in array of structs...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    problem with printf in array of structs...

    i have no problems in writing what i inputted to the FILE...
    but i get problems displaying in onscreen to think that the codes are almost similar
    except printf(); and fprintf();
    can i ask what is wrong? u can run this code by urselves to greater understand what i mean...

    P.S... the 2nd for loop isnt displaying what i want it to display [i.e the same display in the FILE, which is correct]

    thanks in advance!...
    Code:
    #include <stdio.h>
    
    typedef struct robot{
    	char name[50];
    	int energy;
    } ROBOTS;
    
    int main(void)
    {
    	FILE *file = fopen("Data/robots.txt", "w");
    	int i;
    	ROBOTS array_robots[3];
    
    	clrscr();
    
    	if(file==NULL)
    	{
    		printf("\n\nError: can't create file.\n\n");
    		printf("Directory may not exist.\n");
    		getche();
    	    return 1;
    	}
    
    	else
    	{
    		for(i = 0; i < 3; i++)
    		{
    			printf("\nEnter a robot's name:\t\t");
    			scanf("%s", &array_robots[i].name);
    			printf("\nEnter its energy:\t\t");
    			scanf("%d", &array_robots[i].energy);
    			printf("\n\n-------Saving robot...--------\n\n");
    		}
    
    		for(i = 0; i < 3; i++);
    		{
    			printf("\nRobot name: \t%s", array_robots[i].name);
    			printf("\nRobot energy: \t%d", array_robots[i].energy);
    			printf("\n\n--------------------------");
    		}
    
    		for(i = 0; i < 3; i++)
    		{
    			fprintf(file, "\n\nRobot name: \t%s", array_robots[i].name);
    			fprintf(file, "\nRobot energy: \t%d", array_robots[i].energy);
    			fprintf(file, "\n\n--------------------------");
    		}
    
    		fclose(file);
    
    		getche();
    		return 0;
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First things first:
    Code:
    scanf("%s", &array_robots[i].name);
    Remove the & here. When you're using scanf() to read into an array of char, you do not need to use the & because the array will decay into a pointer automatically. In fact, using & as above gives you the wrong type. It is very likely to work on most systems, but is not correct.

    Now, the real problem is this:
    Code:
    for(i = 0; i < 3; i++);
    The semicolon there means that nothing will happen each iteration of the loop; then, when i is 3, the block is entered and array_robots[3] is accessed, which is invalid. An easy-to-overlook typo/slip.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    thanks mate...
    i was so dumb.. xD haha
    sorry, cuz im still starting at coding...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  3. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM