give FH a programming section!

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Need some more C help from whoever has the knowledge if possible.

Code:
void read(char place[11][11], int dist[10][10]) {
 	
char Locations[21];
int a, b;

	FILE *in_file;

	in_file = fopen("Locations.txt","r");
        if (in_file == NULL) {

		printf("\nCannot Open File, Please Check Filename Is Correct %s\n", &Locations);
		exit(0);
	}
		else if (in_file != NULL) {
      
        printf("File Opened Successfully", &Locations);
        printf("\n");
    }
		for (a=0; a <= place;a++) {
          for (b=0; b <= dist;b++){
				fscanf(Locations, "%d ", &Locations[a][b]);
					printf("%-4d ", &Locations[a][b]);
    }
					printf("\n");
    }

 fclose(in_file);

}

im trying to open the text file, print it in2 a 2d array under 1 function. Can you see where its going wrong? Try not to mind the layout its all a bit muddled really while i work out how to do it,

thanks
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
Ok I've never programmed in C so my observation is probably a bit silly, but it seems you have too many end brackets.

After your "printf("\n");" line you have an end bracket that doesnt seem to match up with any other, or should I say it ends the function and so your "fclose(in_file);" lies outside the function.

Err.. Could that be it?
 

DocWolfe

Part of the furniture
Joined
Jan 3, 2005
Messages
2,855
Whats the error you're getting? I've reformatted the code makes it easier to read.

Code:
void read(char place[11][11], int dist[10][10]) 
{

	char Locations[21];
	int a, b;

	FILE *in_file;
	in_file = fopen("Locations.txt","r");
	
	if (in_file == NULL) 
	{

		printf("\nCannot Open File, Please Check Filename Is Correct %s\n", &Locations);
		exit(0);
	}
	else if (in_file != NULL) 
	{

		printf("File Opened Successfully", &Locations);
		printf("\n");
	}
	
	for (a=0; a <= place;a++) 
	{
		for (b=0; b <= dist;b++)
		{
			fscanf(Locations, "%d ", &Locations[a][b]);
			printf("%-4d ", &Locations[a][b]);
		}
		
		printf("\n");
	}

	fclose(in_file);

}
 

Himse

FH is my second home
Joined
Jan 31, 2004
Messages
2,179
English stop filling my head with this sorcery :(

It's gobbledegook :<
 

Cadelin

Resident Freddy
Joined
Feb 18, 2004
Messages
2,515
I count 5 opening and 5 closing brackets. The code is messy and that should be cleaned up because it makes it easier to spot problems.

I have spotted several issues in the code but you don't say what errors/problems you are having so it makes hard to figure out whats causing the problem.

You start by defining a pointer to a file.
You then open the location.txt file in read mode.
You check that your pointer is not null and print out and error message if it is. I am not sure what your line of code does. Why not just use a normal error message for now?
You then use an if else statement. I was under the impression that you need an "else" statement at the end of a set of "else if" statements. Why not just use an "else" and get rid of the extra uneeded condition?
I don't know what you are trying to do with the next two print statements.
You then come to a loop. a is an integer, place is an array of chars. With an array you need to define which element you want and you can't compare int with chars. smilarly with dist, you need to do dist[j] < b.
You then get to the bit where you scan the file. You have defined Locations as a 1 dimensional array and yet here you treat it as a two dimensional array.


I don't mean to be rude, I am certainly no C expert but you need to get the basics right first. Get something working an add a line at a time and make sure you understand whats going on. Most books do contain examples of reading and writing to files.
 

DocWolfe

Part of the furniture
Joined
Jan 3, 2005
Messages
2,855
Also doing this....

Code:
for (a=0; a <= place;a++)

Will not work as 'place' is not a numerical value so you can't do a <= (less or equal to comparison) it is an array.

In C# you have a function provided to you to be able to automatically determine the length of an array e.g. place.Length but this doesn't exist in C unfortunately. You will have to manually define the length of the array.

If I was writing this in C# it would look like;

Code:
for (int a = 0; a < place.Length; a++)

I think if this is your first go at programming you should start with something a little easier like C# or Java :p
 

liloe

It's my birthday today!
Joined
Jan 25, 2004
Messages
4,166
I think if this is your first go at programming you should start with something a little easier like C# or Java :p

ooor *shake shake* J# =))

On-topic, I think the fact that place is not an int makes the whole thing crash. There should be a StrToInt function to fix that.
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
im trying to read a text file in that function. The text file should be printed as a 2d array for example

towna townb townc
town a - 100 200
town b 100 - 100
town c 200 100 -

i wanted to put validation in to tell the user they have input a correct file. If not the error message would say the file error
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
1>.\jim.c(74) : error C2109: subscript requires array or pointer type
1>.\jim.c(75) : error C2109: subscript requires array or pointer type

the errors i get atm
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
i was wondering if id need 2 seperate functions, one for reading the file, one for displaying the file?
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
nm people, i seemed to crack it :) thanks for your time tho

assignpiccl7.jpg


cos i rox laddley!!
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
edit: well i got it to print anyway, need to mess around to make the distances correct now ;p
 

Ezteq

Queen of OT
Joined
Jan 4, 2004
Messages
13,457
YES!! fgs yes, give these damn nerds their own section so that innocent bystanders dont have to put up with this sort of behaviour, its intollerable! its disgusting!! its ...making me feel really thick :(

and i think they've been swearing in binary *nods*
 

Nate

FH is my second home
Joined
Mar 13, 2004
Messages
7,454
What the fuck are you doing Emoglish!?!?! This must be some job my employers asked you to do for mileage between the buildings to fuck me up when I keep putting loads more then I should! I'm going Terminator 2 on your ass, your the guy that built Skynet and I'm not Sarah Conner, I don't care if you have a family your dead and emo!!!
 

Lethul

FH is my second home
Joined
Jan 25, 2004
Messages
8,433
Fucks sake, start working on the formatting before you do any more coding :S
 

old.Tohtori

FH is my second home
Joined
Jan 23, 2004
Messages
45,210
/home [Thread]
/10 if [Thread]!=code /goto 40
/20 if [Thread]=code /goto 30
/30 close thread
/40 open [beer] and [post]
/50 if [beer]!=true /goto [store]
/60 if [beer]=true /goto 40
 

Lethul

FH is my second home
Joined
Jan 25, 2004
Messages
8,433
formatting is secondary ! correct code is all! HAIL CORRECT CODE! :(

the code is more likely to be correct if you do good formatting! :)

and especially if you ask someone else for help at least have the decency to have proper formatting :)
 

Sharma

Can't get enough of FH
Joined
Dec 22, 2003
Messages
4,678
We do have a technical discussion forum under General English, everyone there seems to have a fairly high level of technical knowledge (Kryten, Jonty and so forth) and they're normally happy to help unless they're having a bad day!

Techie Discussion - FreddysHouse
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
Just finished a 30hour stint of programming for my coursework...off out in a bit, this sucks :( Jsps suck...servlets suck...java just sucks :(

Nice to hear there are more others around, maybe I'll be spamming some of my problems there in the future :D
 

Nate

FH is my second home
Joined
Mar 13, 2004
Messages
7,454
You suck kirennia, you forgot that one! :D
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Code:
		      int chart [10][10] = {
				{0, 224, 151, 209, 266, 118, 194, 220, 141, 210},
				{224, 0, 224, 30, 109, 113, 60, 10, 85, 88},
				{151, 224, 0, 204, 304, 118, 234, 216, 137, 250},
				{209, 30, 204, 0, 127, 95, 42, 67, 67, 71},
				{266, 109, 304, 127, 0, 190, 82, 109, 190, 72},
				{118, 113, 118, 95, 190, 0, 120, 107, 30, 134},
				{194, 60, 234, 42, 82, 120, 0, 68, 105, 26},
				{220, 10, 216, 67, 109, 107, 68, 0, 80, 97},
				{141, 85, 137, 67, 190, 30, 105, 80, 0, 135},
				{210, 88, 250, 71, 72, 134, 26, 97, 135, 0} 
				};

				printf(" 1: London\n 2: Blackburn\n 3: Cardiff\n 4: Manchester\n 5: Durham\n 6: Birmingham\n 7: Leeds\n 8: Preston\n 9: Stafford\n 0: York\n\n");

				printf("\n Please enter your start point: "); // input first point
				scanf("%5d", &point1);
						while(point1 < 1 | point1 >10)  // if the point is  not equal to a plot point  it will ask the user to re input
						{
							printf("\n Invalid point pelase re enter : ");
							scanf("%5d", &point1);
						}
			
				printf("\n Please enter your end point: "); // input secound point
				scanf("%5d", &point2);
						while(point2 < 1 | point2 >10) 
						{
							printf("\n Invalid point pelase re enter: "); // if the point is  not equal to a plot point  it will ask the user to re input
							scanf("%5d", &point2);
						}
				 
				printf("\n Point1:%5d",chart[point1][point1]);
				printf("\n Point2:%5d",chart[point1][point2]);
				printf("\n Total distance: %5d", chart[point1][point1]+chart[point1][point2]); // Adds the 2 points togteher to work out the distance 
				printf("\n\n\n");

First of all please try excuse the horrible layout, when i get it working it will all be nice and tidy.

Can you see why im getting these compile errors?

Code:
1>.\jim.c(43) : error C2143: syntax error : missing ';' before 'type'
1>.\jim.c(74) : error C2109: subscript requires array or pointer type
1>.\jim.c(75) : error C2109: subscript requires array or pointer type
1>.\jim.c(76) : error C2109: subscript requires array or pointer type
1>.\jim.c(76) : error C2109: subscript requires array or pointer type

Basically what im after it to take the points from the array and compare the distances between 2 points. Am i missing something key out?

cheers
 

Users who are viewing this thread

Top Bottom