d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Project For Class. Help Please
Add Reply New Topic New Poll
Member
Posts: 24,320
Joined: Jun 4 2008
Gold: 541.37
Aug 6 2014 03:28pm
this part of the project deals with functions and pointers and reading from files. my problem i'm having now is getting the array from the file to last throughout the whole project.
the function definition and declarations were given to us by the teacher..she wants us to focus on calling the functions.
this is what im having trouble getting to show up
the file name is storms.txt which is a notepad file that looks like this:

2009
11
6, 59, 900
11, 100, 111
7, 80, 909
7, 90, 900
7, 52, 800
8, 70, 900
8, 75, 900
8, 90, 900
9, 200, 990
9, 50, 700
10, 75, 800



if i press 1 and type in storms.txt and tell it to print the array it works, however when i press 1, enter storms.txt, and then have it loop back to the menu so i can press 2, it gives me garbage and "myArray" is not declared at that point.
my code so far:
.h provided by teacher:
Code
/*
File: lb48341.h
Purpose: It contains functions to read and parse a file containing storms
Functions:
- readFromFile
- parsingFunction
*/

#include <stdio.h>
#include <stdlib.h>

#define COLSIZE 3 /* column size */

/*
Function name: readFromFile
Returns:
0: success
1: error reading file
Paramenters:
- char* [] : file name
- int [][COLSIZE] : array containing storms
- int * : pointer to an integer containing the year value
- int * : pointer to an integer contining the number of storms
*/
int readFromFile(const char*, int[][COLSIZE], int *, int *);


/*
Function name: parsingFunction
Returns:
0: success
2: error parsing file
Paramenters:
- FILE * : pointer to the file to read
- int [][COLSIZE] : array containing storms
- int * : pointer to an integer containing the year value
- int * : pointer to an integer contining the number of storms
*/
int parsingFunction(FILE *, int [][COLSIZE], int *, int *);


function definitions provided by teacher:

Code
#include "lb48341.h"

int readFromFile(char *fileName, int passedArray[][COLSIZE], int *pYear, int *pNumbOfStorms)
{
FILE *fp;
char buf[1000];
int size = 0;

fp = fopen(fileName, "r");
if (fp == NULL)
{
return 1;
}
else
{
int i = 0;
int p = parsingFunction(fp, passedArray, pYear, pNumbOfStorms);
if (p == 1)
return 2;
}

fclose(fp);
return 0;
}

int parsingFunction(FILE *fp, int passedArray[][COLSIZE], int *pYear, int *pNumbOfStorms)
{
char buf[1000];
char* tk;

int lineNumb = 0;
int year = 0;
int numberOfStorms = 0;
int val = 0;
int month = 0;
int wSpeed = 0;
int press = 0;
int tkNumb = 0;
int i=0;

while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (lineNumb == 0) /* reading year */
{
year = atoi(buf);
*pYear = year;
}
else if (lineNumb == 1) /* reading number of storms */
{
numberOfStorms = atoi(buf);
*pNumbOfStorms = numberOfStorms;
}
else /* storm info */
{
tkNumb = 0;
val = strtok(buf, ", ");
month = atoi(val);

val = strtok(NULL, ", ");
wSpeed = atoi(val);

val = strtok(NULL, "\n");
press = atoi(val);

passedArray[i][0] = month;
passedArray[i][1] = wSpeed;
passedArray[i][2] = press;
i++;
} /*end else */
lineNumb++;
}
if (lineNumb > 0)
return 0;
else
return 1;
}


my main .c:

Code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lb48341.h"
#define CATSIZE 6
#define MAXSTORMS 25
#define COLSIZE 3
#define MONTHS 6

int main(void)
{
int year=0;
int nStorms, nHurricanes;
int totalStorms=0;
int totalHurricanes=0;

int wSpeed;
float wSpeedKnts;
float cumWindSpeedKnts=0.0f;
int category=0;
int totalCat1=0;
int totalCat2=0;
int totalCat3=0;
int totalCat4=0;
int totalCat5=0;
int tStormsInJun=0;
int tStormsInJul=0;
int tStormsInAug=0;
int tStormsInSep=0;
int tStormsInOct=0;
int tStormsInNov=0;
int mHighestStorms=0;

int i,j;
int input;





int month=0;


do
{
printf("\n\t\tHEg v2.0\n");
printf("\t\t========");
printf("\n\n0. Exit\n");
printf("1. Read Season Data\n");
printf("2. Analysis\n");
printf("\nEnter Selection: ");
scanf("%d", &input);

switch(input)
{
case 0: printf("\nThanks for using HEg v2.0\n\n"); break;
case 1:
{
int i, j;
cumWindSpeedKnts=0;

for(i=0;i<MAXSTORMS; i++)
for(j=0;j<COLSIZE; j++)
seasonArray[i][j]=0;

for(i=0;i<CATSIZE;i++)
categoryArray[i]=0;

for(i=0;i<MAXSTORMS; i++)
for(j=0;j<MONTHS; j++)
stormArrayPerMonth[i][j]=0;

char fileName[25];
int readVal;
int myArray[13][3];

printf("\nPlease enter file name: ");
scanf("%s", fileName);

readVal = readFromFile(fileName, myArray , &year, &nStorms);

if( readVal == 1)
{
printf(">>ERROR: Unable to open file %s\n\n", fileName);

}/*end if */
else
{
printf(">> Read File - OK\n");
printf(">> Parsed File - OK\n\n");

} /* end else */

} /*end case 1 */
break;

case 2:
{

char cinput;

printf("\t\t2. Hurricane Season Analysis:\n");
printf("\t\t-----------------------------");
printf("\n\na. Print Hurricane Season Data\n");
printf("b. Storm Classification\n");
printf("c. Calculate PDF\n");
printf("d. Calculate the Expected Value\n");
printf("\nEnter Sub-Selection: ");
scanf("%s", &cinput);

switch(cinput){
case 'a':
{




printf("2009 Hurricane Season Data\n");
printf("===========================\n\n");
printf("Month Wind Speed Max Pressure\n");
printf("----- ---------- -------------\n");
for(i=0; i<11;i++)
{
printf("%d\t",*array[i][0]);
}/* end for */

}/* end a */
break;
case 'b':
{
printf("ok\n");
}/* end b */
break;
case 'c':
{
printf("ok\n");
}/* end c */
break;
case 'd':
{
printf("ok\n");
}/* end d */
break;
} /* end switch */

}/*end case 2 */
break;


default:
{
printf("\nERROR: Menu option not available in HEg v2.0\n\n");
break;
}/*end default */

}/*end switch*/

} while(input!=0);/*end while */


system("PAUSE");
return 0;
}


sub menu option a is where im struggling.

This post was edited by JonBully on Aug 6 2014 03:30pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 6 2014 03:33pm
Code
printf("%d\t",*array[i][0]);


*array is not defined.

Also int myArray[][] is defined outside the scope of your second select case so you will not be able to use it outside of option 1.
Member
Posts: 24,320
Joined: Jun 4 2008
Gold: 541.37
Aug 6 2014 03:35pm
Quote (AbDuCt @ Aug 6 2014 04:33pm)
Code
printf("%d\t",*array[i][0]);


*array is not defined.

Also int myArray[][] is defined outside the scope of your second select case so you will not be able to use it outside of option 1.


yea i forgot to delete that..that was just from me playing around with it..leftover code.
how can i get option 2 to read the same array as option 1 without re-entering the file name?

e: nvm thank you for the help..made me rethink the way a switch works..makes sense now lol

This post was edited by JonBully on Aug 6 2014 03:55pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll