Thread: problem with writing to file

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    problem with writing to file

    Right, here's my code:

    Code:
    #include <stdio.h>
    
    #define NO_POINTS 10
    #define PPM 0.40
    #define TRUE 1
    #define FALSE 0
    
    void menu(int *pChoice);
    void enterDistances(int array[NO_POINTS][NO_POINTS]);
    void dispChart(int array[NO_POINTS][NO_POINTS]);
    void calcDistance(int array[NO_POINTS][NO_POINTS]);
    void totalCost(int array[NO_POINTS][NO_POINTS]);
    void readFile();
    void writeFile(int array[NO_POINTS][NO_POINTS]);
    
    int main(void)
    {
    	int choice = 0;
    	int distEntered = FALSE;
    
    	int array[NO_POINTS][NO_POINTS];
    
    	printf("\n\n**********************************************************\n");
    	printf("*************** Travel Expenses Calculator ***************\n");
    	printf("**********************************************************");
    
    	menu(&choice);
    
    	while(choice != 6)
    	{
    		switch(choice)
    		{
    			case 1:
    				enterDistances(array);
    				distEntered = TRUE;
    				break;
    			case 2:
    				if(distEntered == TRUE)
    				{
    					dispChart(array);
    				}
    				else
    				{
    					printf("\n\nNo distances have been entered");
    				}
    				break;
    			case 3:
    				if(distEntered == TRUE)
    				{
    					calcDistance(array);
    				}
    				else
    				{
    					printf("\n\nNo distances have been entered");
    				}
    				break;
    			case 4:
    				if(distEntered == TRUE)
    				{
    					totalCost(array);
    				}
    				else
    				{
    					printf("\n\nNo distances have been entered");
    				}
    				break;
    			case 5:
    				writeFile(array, outFile);
    				break;
    			case 6:
    				break;
    		}
    
    		menu(&choice);
    	}
    
    	return 0;
    }
    
    void menu(int *pChoice)
    {
    	printf("\n\n\n1. Enter distances");
    	printf("\n2. Display distances in a grid");
    	printf("\n3. Display distance between two points");
    	printf("\n4. Calculate cost of journey between two points");
    	printf("\n5. Write distances to file");
    	printf("\n6. Exit"); 
    	printf("\nChoice: ");
    	scanf("%d", pChoice);
    }
    
    void enterDistances(int array[NO_POINTS][NO_POINTS])
    {
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		for(int j = 0; j < NO_POINTS; ++j)
    		{
    			int temp;
    
    			if(i == j)
    			{
    				array[i][j] = 0;
    			}
    			else if(j > i)
    			{
    				printf("\nHow many miles is it between point %d and %d: ", (i + 1), (j + 1));
    				scanf("%d", &temp);
    
    				array[i][j] = temp;
    				array[j][i] = temp;
    			}
    		}
    	}
    }
    
    void dispChart(int array[NO_POINTS][NO_POINTS])
    {
    	printf("\n     *** Miles between each point ***\n\n     ");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		printf("%4d", (i + 1));
    	}
    
    	printf("\n     ");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		printf("----");
    	}
    
    	printf("\n");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		printf("%3d |", (i + 1));
    
    		for(int j = 0; j < NO_POINTS; ++j)
    		{
    			if(array[i][j] == 0)
    			{
    				printf("   -");
    			}
    			else
    			{
    				printf("%4d", array[i][j]);
    			}
    		}
    
    		printf("\n");
    	}
    
    }
    
    void calcDistance(int array[NO_POINTS][NO_POINTS])
    {
    	int point1, point2;
    
    	printf("\n\nPlease enter the two points: ");
    	scanf("%d %d", &point1, &point2);
    
    	printf("\n\nThe distance between point %d and %d is %d", point1, point2, array[point1 - 1][point2 - 1]);
    }
    
    void totalCost(int array[NO_POINTS][NO_POINTS]) // need to check limits
    {
    	int point1, point2;
    	float cost;
    
    	printf("\n\nPlease enter the two points: ");
    	scanf("%d %d", &point1, &point2);
    
    	cost = array[point1 - 1][point2 - 1] * PPM;
    
    	printf("\n\nIt costs %2.2fGBP to travel from %d to %d, or vice versa, at %2.2f PPM", cost, point1, point2, PPM);
    }
    
    void readFile()
    {
    }
    
    void writeFile(int array[NO_POINTS][NO_POINTS])
    {
    	FILE *outFile = fopen("Test.txt", "w");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		fprintf(outFile, "%4d", (i + 1));
    	}
    
    	fprintf(outFile, "\n     ");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		fprintf(outFile, "----");
    	}
    
    	fprintf(outFile, "\n");
    
    	for(int i = 0; i < NO_POINTS; ++i)
    	{
    		fprintf(outFile, "%3d |", (i + 1));
    
    		for(int j = 0; j < NO_POINTS; ++j)
    		{
    			if(array[i][j] == 0)
    			{
    				fprintf(outFile, "   -");
    			}
    			else
    			{
    				fprintf(outFile, "%4d", array[i][j]);
    			}
    		}
    
    		fprintf(outFile, "\n");
    	}
    
    	fclose(outFile);
    }
    Sorry about pasting the lot, but im not sure if the rest of it would be needed to help you guys help me.
    Basically, when i compile and run this, it does nothing but create an empty file "Test.txt". It bypasses the menu at the beginning, and im stumped at why it does this, im going insane.
    As you might have guessed, im new to file i/o, so be gentle with me

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    this doesn't compile - outFile undeclared - here:

    Code:
    case 5:
           writeFile(array, outFile); /*hasn't been declared before this*/
           break;
    leads me to guess that this isn't your latest build - post your
    latest cos the start of this looks ok (on a very brief look).
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Oh that line should be "writeFile(array);". My compiler didnt pick it up :S im working on unix, if that means anything

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    ah ive solved it, id setup the paths incorrectly, sorry to have wasted anyones time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  2. Problem with file writing
    By Goldrak in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2006, 06:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM