Hello all,

So I have been trying to "redo" a c++ program in c and I am getting stuck on strings (I dont code in c never have still learning).

Any who in my C++ program, I use string concationation tools to print out select output, however c does not want to play ball. I honestly do not know what I have done but is there an easy way to do what I am doing? Essentially I read a bunch of data, perform an average and then print it with a date code (the date code is initially long but I chop it down to the needed length).

Thanks in advance:

CODE:

/***************************************
Devang N. Joshi
Homework Two
CSCI 325 - Averaging in C++
January 22nd, 2011

The purpose of this program is to serve
as basic review on simple file I/O in
C using a well structured data file
and performing simple math on it.
***************************************/
#include <stdlib.h>
#include <stdio.h>

int main()
{
	//declare all variables
	FILE *Infile, *Outfile;
	int stationNum = 0; //variable for the station number 
	string dateCode/*[13]*/;  //variable for the date code
	string dataType/*[4]*/;   /*variable that shows what "type" the data is
			    example...pwl stands for "primary water level"*/

	/*variables for string manipulation*/
	string str;
	string::size_type pos;

	/*raw data variables*/
	float d1=0;
	float d2=0; 
	float d3=0;
	float d4=0;
	float d5=0;
	float d6=0;
	float d7=0;
	float d8=0;
	float d9=0;
	float d10=0;
			    //ten variables for water levels over an hour

	float daySum=0;    //sum of all the water levels over 24hrs
	float dayAvg=0;    //average water level for a given day

	float denominator=240;
			    //240 data points per day, used to cal dayAvg

	int rowCount=0;     //counter used to keep track of day change

	//open and test files for input & output
	Infile = fopen ("lab2.dat", "r");
   	if (Infile == NULL)
     	{
      		fprintf(stderr, "Error with input file....terminating program\n\n");
      		exit (1);
     	}
   	Outfile = fopen ("lab2.out", "w");
   	if (Outfile == NULL)
     	{
      		fprintf(stderr, "Error with output file....terminating program\n\n");
      		exit (1);
     	}

	//initial write to output file...write headings to output file
	fprintf(Outfile,"Data\n\n");
	fprintf(Outfile,"Date		Average Water Height\n");
	fprintf(Outfile,"-----------------------------\n\n");

	/*
	Logic to calculate average daily water levels and
	print them to the output files                   
	*/

	
	while (!feof(Infile)) //test against end of file marker
	{
		if(rowCount<24) //at 24 one day has elapsed
		{
			fscanf(Infile,"%d %s %s %f %f %f %f %f %f %f %f %f %f %f",&stationNum,&dateCode,&dataType,&d1,&d2,&d3,&d4,&d5,&d6,&d7,&d8,&d9,&d10);
			daySum=daySum+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10;

			str = dateCode;	//set string to the value of the dateCode
			pos = str.find_first_of("+");//set variable pos to find '+' marker
			if(pos == string::npos)	//simple logic to make sure the 'dateCode' is formated properly
			{
				printf(stderr, "string concatenation error\n");
			}

			rowCount++;
			

		}
		else //picked up when one days worth of data is read in, can be averaged now
		{
			dayAvg=daySum/denominator; //calculate the average
			fprintf(Outfile,"%s		%f \n", str.substr(0,pos), dayAvg);//print data to the output file 
			daySum=0;    //reset daysum
			dayAvg=0;    //reset dayavg
			rowCount=0;  //reset rowcount
		
		}//if-else

		/* in the else portion of the loop the printing to file is done by
		   printing a substring of the whole string str. The length of the 
		   substring is given by the bounds in the () 0->pos. The pos variable
		   gives the location of the '+' marker in the string, and that gives
		   the needed string
		*/
	
	}//while

	/*when the end of file is picked up, the loop terminates
	  however this will cause the final set of averages to fail
	  that is why the final average is calculated below and the
	  the final write to the output file is done */

	dayAvg=daySum/denominator; //calculate the average
	fprintf(Outfile,"%s		%f \n", str.substr(0,pos), dayAvg);//print data to the output file

	//final write to output file to mark end of data
	fprintf(Outfile,"\n\n End of data\n");

	//close input & output files
	fclose(Infile);
	fclose(Outfile);

	return 0;
}//main

DATA:

008 2011006+0000 pwl 1.423 1.424 1.429 1.432 1.436 1.439 1.443 1.446 1.447 1.452
008 2011006+0100 pwl 1.457 1.459 1.460 1.463 1.464 1.466 1.468 1.470 1.472 1.472
008 2011006+0200 pwl 1.473 1.476 1.477 1.479 1.482 1.486 1.492 1.496 1.495 1.497
008 2011006+0300 pwl 1.498 1.499 1.500 1.504 1.507 1.508 1.509 1.511 1.512 1.512
008 2011006+0400 pwl 1.513 1.514 1.514 1.513 1.513 1.516 1.516 1.517 1.517 1.518
008 2011006+0500 pwl 1.519 1.522 1.523 1.524 1.524 1.525 1.526 1.527 1.528 1.531
008 2011006+0600 pwl 1.531 1.531 1.531 1.531 1.531 1.532 1.532 1.533 1.534 1.534
008 2011006+0700 pwl 1.535 1.536 1.537 1.537 1.538 1.539 1.540 1.540 1.541 1.541
008 2011006+0800 pwl 1.542 1.543 1.543 1.543 1.542 1.542 1.542 1.541 1.541 1.541
008 2011006+0900 pwl 1.540 1.540 1.540 1.540 1.540 1.539 1.539 1.539 1.540 1.540
008 2011006+1000 pwl 1.540 1.539 1.538 1.537 1.537 1.536 1.536 1.534 1.533 1.531
008 2011006+1100 pwl 1.530 1.530 1.529 1.528 1.527 1.526 1.524 1.523 1.522 1.521
008 2011006+1200 pwl 1.519 1.517 1.515 1.514 1.512 1.510 1.508 1.506 1.504 1.502
008 2011006+1300 pwl 1.500 1.498 1.496 1.494 1.492 1.490 1.488 1.486 1.483 1.479
008 2011006+1400 pwl 1.475 1.474 1.472 1.470 1.468 1.466 1.464 1.462 1.459 1.457
008 2011006+1500 pwl 1.455 1.452 1.450 1.447 1.444 1.441 1.438 1.435 1.432 1.428
008 2011006+1600 pwl 1.426 1.422 1.419 1.416 1.414 1.411 1.408 1.405 1.403 1.400
008 2011006+1700 pwl 1.397 1.395 1.392 1.389 1.386 1.384 1.382 1.380 1.378 1.375
008 2011006+1800 pwl 1.374 1.369 1.368 1.367 1.366 1.363 1.361 1.359 1.355 1.353
008 2011006+1900 pwl 1.351 1.349 1.345 1.342 1.340 1.335 1.332 1.330 1.327 1.325
008 2011006+2000 pwl 1.323 1.320 1.320 1.319 1.319 1.319 1.317 1.317 1.317 1.315
008 2011006+2100 pwl 1.313 1.312 1.310 1.308 1.306 1.305 1.305 1.304 1.302 1.300
008 2011006+2200 pwl 1.300 1.304 1.306 1.310 1.313 1.314 1.316 1.317 1.318 1.320
008 2011006+2300 pwl 1.320 1.321 1.321 1.320 1.321 1.324 1.325 1.326 1.327 1.329
008 2011007+0000 pwl 1.331 1.333 1.336 1.339 1.341 1.343 1.346 1.348 1.349 1.351

008 2011007+0100 pwl 1.353 1.355 1.356 1.359 1.359 1.363 1.366 1.369 1.371 1.374
008 2011007+0200 pwl 1.375 1.378 1.381 1.385 1.388 1.389 1.392 1.394 1.396 1.397
008 2011007+0300 pwl 1.399 1.401 1.402 1.404 1.407 1.409 1.410 1.413 1.414 1.416
008 2011007+0400 pwl 1.418 1.420 1.423 1.424 1.425 1.426 1.427 1.427 1.429 1.430
008 2011007+0500 pwl 1.431 1.432 1.434 1.435 1.437 1.438 1.439 1.440 1.441 1.442
008 2011007+0600 pwl 1.442 1.443 1.443 1.444 1.443 1.445 1.446 1.446 1.448 1.448
008 2011007+0700 pwl 1.449 1.448 1.449 1.449 1.448 1.448 1.449 1.449 1.449 1.449
008 2011007+0800 pwl 1.449 1.449 1.449 1.449 1.449 1.449 1.448 1.447 1.447 1.447
008 2011007+0900 pwl 1.447 1.447 1.447 1.448 1.450 1.450 1.450 1.448 1.448 1.449
008 2011007+1000 pwl 1.449 1.450 1.451 1.451 1.450 1.450 1.450 1.450 1.449 1.448
008 2011007+1100 pwl 1.447 1.446 1.444 1.442 1.441 1.441 1.439 1.439 1.439 1.438
008 2011007+1200 pwl 1.437 1.436 1.435 1.434 1.433 1.431 1.430 1.429 1.428 1.427
008 2011007+1300 pwl 1.426 1.426 1.425 1.423 1.422 1.420 1.418 1.417 1.414 1.411
008 2011007+1400 pwl 1.408 1.406 1.404 1.402 1.400 1.398 1.397 1.395 1.393 1.391
008 2011007+1500 pwl 1.388 1.385 1.384 1.382 1.382 1.378 1.376 1.373 1.371 1.368
008 2011007+1600 pwl 1.367 1.363 1.360 1.358 1.356 1.351 1.350 1.347 1.344 1.340
008 2011007+1700 pwl 1.338 1.335 1.334 1.333 1.332 1.331 1.330 1.328 1.327 1.326
008 2011007+1800 pwl 1.326 1.324 1.322 1.320 1.319 1.317 1.315 1.312 1.310 1.308
008 2011007+1900 pwl 1.306 1.305 1.304 1.302 1.301 1.299 1.298 1.296 1.296 1.296
008 2011007+2000 pwl 1.295 1.295 1.295 1.295 1.294 1.293 1.293 1.292 1.291 1.289
008 2011007+2100 pwl 1.288 1.287 1.286 1.285 1.285 1.284 1.282 1.279 1.277 1.275
008 2011007+2200 pwl 1.276 1.279 1.282 1.283 1.285 1.286 1.289 1.291 1.293 1.295
008 2011007+2300 pwl 1.297 1.299 1.301 1.303 1.305 1.307 1.309 1.312 1.316 1.320
008 2011008+0000 pwl 1.324 1.327 1.330 1.333 1.337 1.340 1.344 1.346 1.349 1.352
008 2011008+0100 pwl 1.355 1.359 1.363 1.367 1.370 1.373 1.377 1.381 1.386 1.390
008 2011008+0200 pwl 1.393 1.397 1.400 1.405 1.410 1.414 1.413 1.415 1.416 1.418
008 2011008+0300 pwl 1.422 1.424 1.425 1.426 1.427 1.428 1.430 1.431 1.433 1.436
008 2011008+0400 pwl 1.434 1.435 1.436 1.436 1.437 1.437 1.439 1.438 1.441 1.440
008 2011008+0500 pwl 1.441 1.442 1.442 1.442 1.443 1.442 1.442 1.443 1.444 1.445
008 2011008+0600 pwl 1.447 1.448 1.448 1.449 1.451 1.452 1.454 1.455 1.457 1.457
008 2011008+0700 pwl 1.458 1.458 1.458 1.458 1.458 1.458 1.458 1.457 1.459 1.456
008 2011008+0800 pwl 1.457 1.455 1.454 1.453 1.454 1.454 1.455 1.455 1.455 1.458
008 2011008+0900 pwl 1.453 1.453 1.453 1.450 1.450 1.449 1.451 1.450 1.452 1.449
008 2011008+1000 pwl 1.449 1.449 1.448 1.449 1.451 1.450 1.448 1.448 1.448 1.448
008 2011008+1100 pwl 1.447 1.446 1.443 1.442 1.442 1.442 1.443 1.444 1.445 1.444
008 2011008+1200 pwl 1.444 1.445 1.445 1.445 1.445 1.445 1.446 1.445 1.444 1.443
008 2011008+1300 pwl 1.441 1.440 1.439 1.439 1.438 1.438 1.436 1.435 1.433 1.432
008 2011008+1400 pwl 1.431 1.431 1.430 1.431 1.430 1.429 1.429 1.429 1.428 1.427
008 2011008+1500 pwl 1.427 1.426 1.425 1.425 1.423 1.422 1.421 1.420 1.419 1.419
008 2011008+1600 pwl 1.418 1.418 1.418 1.417 1.416 1.415 1.415 1.416 1.417 1.417
008 2011008+1700 pwl 1.417 1.417 1.417 1.417 1.416 1.416 1.414 1.413 1.412 1.410
008 2011008+1800 pwl 1.410 1.408 1.407 1.406 1.405 1.406 1.408 1.408 1.404 1.404
008 2011008+1900 pwl 1.403 1.403 1.404 1.404 1.407 1.406 1.407 1.408 1.407 1.408
008 2011008+2000 pwl 1.410 1.410 1.410 1.411 1.410 1.412 1.412 1.413 1.413 1.413
008 2011008+2100 pwl 1.414 1.416 1.417 1.418 1.419 1.420 1.420 1.421 1.422 1.422
008 2011008+2200 pwl 1.423 1.424 1.425 1.426 1.428 1.430 1.432 1.434 1.436 1.437
008 2011008+2300 pwl 1.438 1.439 1.440 1.441 1.442 1.444 1.445 1.446 1.447 1.448
008 2011009+0000 pwl 1.450 1.452 1.454 1.456 1.458 1.459 1.461 1.463 1.465 1.468
008 2011009+0100 pwl 1.471 1.474 1.478 1.482 1.486 1.490 1.493 1.497 1.500 1.503
008 2011009+0200 pwl 1.506 1.509 1.511 1.514 1.518 1.522 1.526 1.530 1.535 1.539
008 2011009+0300 pwl 1.542 1.543 1.544 1.545 1.545 1.545 1.546 1.546 1.547 1.546
008 2011009+0400 pwl 1.545 1.545 1.542 1.542 1.543 1.543 1.545 1.547 1.550 1.553
008 2011009+0500 pwl 1.553 1.555 1.558 1.560 1.562 1.563 1.563 1.563 1.563 1.563
008 2011009+0600 pwl 1.563 1.563 1.563 1.564 1.567 1.570 1.573 1.575 1.577 1.579
008 2011009+0700 pwl 1.581 1.583 1.586 1.588 1.588 1.590 1.591 1.592 1.593 1.596
008 2011009+0800 pwl 1.597 1.601 1.602 1.607 1.613 1.618 1.623 1.628 1.633 1.637
008 2011009+0900 pwl 1.642 1.645 1.647 1.648 1.649 1.649 1.649 1.649 1.648 1.647
008 2011009+1000 pwl 1.646 1.646 1.655 1.641 1.619 1.601 1.586 1.576 1.568 1.567
008 2011009+1100 pwl 1.572 1.572 1.556 1.549 1.543 1.537 1.541 1.559 1.593 1.626
008 2011009+1200 pwl 1.652 1.664 1.666 1.665 1.668 1.676 1.684 1.685 1.683 1.680
008 2011009+1300 pwl 1.670 1.656 1.644 1.627 1.611 1.602 1.604 1.613 1.623 1.629
008 2011009+1400 pwl 1.631 1.628 1.627 1.628 1.633 1.637 1.638 1.640 1.641 1.640
008 2011009+1500 pwl 1.639 1.635 1.627 1.620 1.613 1.608 1.608 1.607 1.604 1.600
008 2011009+1600 pwl 1.595 1.591 1.590 1.588 1.586 1.583 1.578 1.574 1.570 1.566
008 2011009+1700 pwl 1.562 1.557 1.553 1.550 1.549 1.550 1.552 1.553 1.552 1.551
008 2011009+1800 pwl 1.549 1.548 1.548 1.548 1.548 1.547 1.546 1.545 1.545 1.545
008 2011009+1900 pwl 1.545 1.544 1.543 1.541 1.540 1.539 1.538 1.536 1.534 1.532
008 2011009+2000 pwl 1.531 1.530 1.530 1.529 1.528 1.526 1.523 1.521 1.519 1.518
008 2011009+2100 pwl 1.517 1.514 1.512 1.509 1.507 1.505 1.502 1.497 1.492 1.487
008 2011009+2200 pwl 1.483 1.483 1.484 1.485 1.485 1.486 1.488 1.490 1.492 1.493
008 2011009+2300 pwl 1.495 1.496 1.497 1.498 1.500 1.503 1.505 1.509 1.510 1.512
008 2011010+0000 pwl 1.514 1.516 1.519 1.520 1.520 1.526 1.529 1.533 1.538 1.542
008 2011010+0100 pwl 1.545 1.546 1.550 1.554 1.548 1.552 1.553 1.555 1.555 1.558
008 2011010+0200 pwl 1.561 1.562 1.565 1.564 1.566 1.571 1.572 1.575 1.580 1.581
008 2011010+0300 pwl 1.583 1.585 1.588 1.592 1.595 1.599 1.603 1.605 1.607 1.612
008 2011010+0400 pwl 1.607 1.609 1.608 1.609 1.611 1.613 1.616 1.618 1.619 1.619
008 2011010+0500 pwl 1.621 1.621 1.622 1.623 1.624 1.626 1.628 1.628 1.629 1.631
008 2011010+0600 pwl 1.631 1.633 1.633 1.632 1.631 1.629 1.629 1.628 1.627 1.622
008 2011010+0700 pwl 1.627 1.627 1.628 1.629 1.630 1.631 1.631 1.632 1.633 1.633
008 2011010+0800 pwl 1.633 1.636 1.635 1.633 1.634 1.634 1.635 1.635 1.635 1.635
008 2011010+0900 pwl 1.633 1.631 1.631 1.630 1.630 1.630 1.629 1.628 1.628 1.629
008 2011010+1000 pwl 1.629 1.629 1.627 1.626 1.625 1.625 1.625 1.624 1.623 1.621
008 2011010+1100 pwl 1.619 1.619 1.618 1.618 1.616 1.613 1.611 1.609 1.609 1.608
008 2011010+1200 pwl 1.607 1.605 1.604 1.604 1.603 1.603 1.603 1.603 1.602 1.601
008 2011010+1300 pwl 1.600 1.600 1.600 1.599 1.598 1.599 1.599 1.599 1.598 1.598
008 2011010+1400 pwl 1.596 1.597 1.596 1.596 1.596 1.595 1.595 1.595 1.594 1.594
008 2011010+1500 pwl 1.594 1.595 1.594 1.593 1.594 1.594 1.593 1.594 1.594 1.595
008 2011010+1600 pwl 1.595 1.595 1.595 1.596 1.595 1.596 1.596 1.598 1.599 1.600
008 2011010+1700 pwl 1.601 1.603 1.604 1.604 1.604 1.604 1.603 1.602 1.600 1.600
008 2011010+1800 pwl 1.598 1.598 1.597 1.596 1.597 1.600 1.596 1.597 1.597 1.598
008 2011010+1900 pwl 1.599 1.599 1.600 1.599 1.599 1.596 1.595 1.594 1.593 1.591
008 2011010+2000 pwl 1.588 1.586 1.585 1.583 1.584 1.583 1.583 1.582 1.582 1.583
008 2011010+2100 pwl 1.584 1.585 1.585 1.584 1.584 1.584 1.585 1.585 1.585 1.585
008 2011010+2200 pwl 1.584 1.584 1.585 1.585 1.585 1.585 1.584 1.584 1.585 1.585
008 2011010+2300 pwl 1.586 1.587 1.587 1.587 1.588 1.588 1.589 1.589 1.590 1.591
008 2011011+0000 pwl 1.591 1.592 1.593 1.593 1.593 1.593 1.593 1.593 1.593 1.594
008 2011011+0100 pwl 1.594 1.594 1.595 1.596 1.597 1.598 1.599 1.599 1.600 1.600
008 2011011+0200 pwl 1.601 1.601 1.601 1.601 1.601 1.602 1.601 1.601 1.601 1.600
008 2011011+0300 pwl 1.600 1.600 1.600 1.600 1.601 1.603 1.605 1.607 1.609 1.610
008 2011011+0400 pwl 1.612 1.613 1.612 1.615 1.615 1.617 1.616 1.616 1.612 1.614
008 2011011+0500 pwl 1.614 1.614 1.615 1.615 1.614 1.612 1.611 1.610 1.609 1.607
008 2011011+0600 pwl 1.606 1.605 1.601 1.601 1.599 1.597 1.595 1.594 1.593 1.593
008 2011011+0700 pwl 1.591 1.589 1.587 1.586 1.584 1.583 1.580 1.578 1.576 1.574
008 2011011+0800 pwl 1.573 1.572 1.572 1.572 1.571 1.570 1.569 1.569 1.568 1.566
008 2011011+0900 pwl 1.564 1.564 1.563 1.563 1.561 1.558 1.556 1.554 1.552 1.552
008 2011011+1000 pwl 1.550 1.547 1.545 1.543 1.541 1.540 1.538 1.535 1.531 1.528
008 2011011+1100 pwl 1.526 1.525 1.524 1.523 1.522 1.520 1.518 1.515 1.514 1.513
008 2011011+1200 pwl 1.511 1.510 1.508 1.507 1.506 1.505 1.505 1.506 1.505 1.504
008 2011011+1300 pwl 1.502 1.501 1.499 1.498 1.497 1.496 1.495 1.495 1.495 1.494
008 2011011+1400 pwl 1.495 1.495 1.495 1.493 1.490 1.488 1.487 1.487 1.487 1.488
008 2011011+1500 pwl 1.490 1.493 1.495 1.502 1.496 1.507 1.506 1.503 1.502 1.503
008 2011011+1600 pwl 1.504 1.502 1.501 1.496 1.491 1.488 1.486 1.484 1.481 1.478
008 2011011+1700 pwl 1.474 1.475 1.477 1.476 1.474 1.473 1.474 1.475 1.478 1.480
008 2011011+1800 pwl 1.481 1.478 1.476 1.475 1.475 1.475 1.474 1.472 1.471 1.471
008 2011011+1900 pwl 1.471 1.470 1.470 1.469 1.470 1.473 1.477 1.479 1.480 1.480
008 2011011+2000 pwl 1.480 1.477 1.475 1.474 1.473 1.471 1.471 1.472 1.471 1.470
008 2011011+2100 pwl 1.469 1.467 1.466 1.464 1.463 1.463 1.462 1.462 1.462 1.461
008 2011011+2200 pwl 1.461 1.462 1.460 1.459 1.459 1.459 1.459 1.459 1.459 1.457
008 2011011+2300 pwl 1.456 1.456 1.457 1.458 1.459 1.457 1.455 1.454 1.455 1.457
008 2011012+0000 pwl 1.457 1.454 1.454 1.452 1.451 1.451 1.457 1.457 1.461 1.459
008 2011012+0100 pwl 1.460 1.460 1.460 1.461 1.462 1.458 1.458 1.461 1.459 1.459
008 2011012+0200 pwl 1.460 1.457 1.455 1.454 1.453 1.453 1.453 1.454 1.453 1.452
008 2011012+0300 pwl 1.452 1.453 1.453 1.453 1.452 1.451 1.451 1.451 1.451 1.451
008 2011012+0400 pwl 1.451 1.451 1.452 1.452 1.454 1.456 1.456 1.457 1.457 1.457
008 2011012+0500 pwl 1.456 1.457 1.455 1.455 1.454 1.453 1.454 1.454 1.455 1.455
008 2011012+0600 pwl 1.454 1.453 1.452 1.451 1.450 1.448 1.447 1.445 1.443 1.442
008 2011012+0700 pwl 1.440 1.438 1.437 1.435 1.433 1.432 1.431 1.431 1.431 1.429
008 2011012+0800 pwl 1.428 1.427 1.425 1.424 1.423 1.421 1.419 1.417 1.416 1.414
008 2011012+0900 pwl 1.412 1.410 1.408 1.406 1.404 1.403 1.401 1.399 1.398 1.396
008 2011012+1000 pwl 1.395 1.395 1.393 1.391 1.389 1.387 1.385 1.383 1.381 1.379
008 2011012+1100 pwl 1.378 1.377 1.376 1.375 1.374 1.373 1.371 1.369 1.367 1.366
008 2011012+1200 pwl 1.364 1.363 1.363 1.363 1.363 1.362 1.362 1.361 1.361 1.361
008 2011012+1300 pwl 1.361 1.361 1.361 1.361 1.361 1.360 1.360 1.360 1.360 1.359
008 2011012+1400 pwl 1.359 1.359 1.360 1.360 1.362 1.362 1.361 1.361 1.361 1.363
008 2011012+1500 pwl 1.364 1.365 1.366 1.367 1.368 1.368 1.369 1.370 1.371 1.373
008 2011012+1600 pwl 1.375 1.377 1.380 1.383 1.385 1.388 1.390 1.391 1.392 1.393
008 2011012+1700 pwl 1.394 1.396 1.397 1.399 1.399 1.400 1.401 1.402 1.403 1.404
008 2011012+1800 pwl 1.405 1.407 1.409 1.411 1.413 1.415 1.416 1.417 1.418 1.419
008 2011012+1900 pwl 1.421 1.423 1.425 1.427 1.428 1.429 1.431 1.433 1.434 1.436
008 2011012+2000 pwl 1.437 1.439 1.441 1.443 1.445 1.447 1.449 1.450 1.452 1.454
008 2011012+2100 pwl 1.456 1.458 1.460 1.461 1.462 1.464 1.466 1.468 1.471 1.472
008 2011012+2200 pwl 1.473 1.473 1.473 1.474 1.475 1.476 1.477 1.478 1.478 1.479
008 2011012+2300 pwl 1.480 1.482 1.483 1.484 1.485 1.486 1.487 1.488 1.490 1.492
008 2011013+0000 pwl 1.493 1.493 1.494 1.495 1.496 1.497 1.498 1.498 1.499 1.500
008 2011013+0100 pwl 1.501 1.502 1.503 1.504 1.505 1.505 1.506 1.507 1.508 1.510
008 2011013+0200 pwl 1.510 1.512 1.513 1.514 1.515 1.516 1.515 1.520 1.521 1.523
008 2011013+0300 pwl 1.524 1.525 1.525 1.526 1.526 1.528 1.529 1.529 1.529 1.528
008 2011013+0400 pwl 1.526 1.526 1.526 1.527 1.527 1.527 1.527 1.526 1.526 1.527
008 2011013+0500 pwl 1.527 1.527 1.527 1.528 1.526 1.526 1.527 1.527 1.526 1.526
008 2011013+0600 pwl 1.525 1.525 1.524 1.524 1.523 1.521 1.519 1.518 1.516 1.515
008 2011013+0700 pwl 1.514 1.513 1.512 1.510 1.508 1.506 1.504 1.502 1.502 1.498
008 2011013+0800 pwl 1.495 1.493 1.490 1.488 1.485 1.483 1.480 1.479 1.476 1.472
008 2011013+0900 pwl 1.470 1.465 1.462 1.459 1.455 1.452 1.449 1.447 1.444 1.442
008 2011013+1000 pwl 1.439 1.437 1.435 1.433 1.432 1.431 1.431 1.431 1.432 1.432
008 2011013+1100 pwl 1.432 1.431 1.430 1.429 1.428 1.427 1.425 1.422 1.419 1.416
008 2011013+1200 pwl 1.412 1.409 1.406 1.403 1.401 1.400 1.399 1.398 1.397 1.396
008 2011013+1300 pwl 1.395 1.393 1.393 1.392 1.396 1.397 1.398 1.398 1.398 1.395
008 2011013+1400 pwl 1.395 1.394 1.395 1.396 1.399 1.401 1.402 1.401 1.400 1.397
008 2011013+1500 pwl 1.394 1.392 1.390 1.389 1.388 1.388 1.388 1.389 1.389 1.391
008 2011013+1600 pwl 1.394 1.396 1.398 1.401 1.403 1.404 1.405 1.406 1.407 1.408
008 2011013+1700 pwl 1.409 1.410 1.410 1.410 1.411 1.412 1.413 1.414 1.416 1.418
008 2011013+1800 pwl 1.421 1.425 1.429 1.432 1.435 1.438 1.441 1.444 1.446 1.448
008 2011013+1900 pwl 1.450 1.451 1.452 1.453 1.454 1.456 1.457 1.459 1.461 1.463
008 2011013+2000 pwl 1.465 1.468 1.467 1.471 1.474 1.475 1.479 1.481 1.483 1.484
008 2011013+2100 pwl 1.487 1.487 1.489 1.491 1.492 1.493 1.495 1.496 1.498 1.500
008 2011013+2200 pwl 1.502 1.504 1.505 1.509 1.510 1.512 1.515 1.516 1.518 1.519
008 2011013+2300 pwl 1.520 1.522 1.523 1.524 1.526 1.526 1.527 1.529 1.531 1.532

Recommended Answers

All 8 Replies

The phrase "C does not want to play ball", is not nearly as helpful in troubleshooting your program, as you seem to believe it is. ;)

Use your debugger or add some printf() statements in your code, and give us the real problem you're seeing with your program.

Ah sorry, my errors are compile errors (I am using the linux terminal to compile).

The compile errors are all around the string variables, but I do not know why because in c++ it works fine.

lab2.c: In function âmainâ:
lab2.c:20: error: âstringâ undeclared (first use in this function)
lab2.c:20: error: (Each undeclared identifier is reported only once
lab2.c:20: error: for each function it appears in.)
lab2.c:20: error: expected â;â before âdateCodeâ
lab2.c:21: error: expected â;â before âdataTypeâ
lab2.c:25: error: expected â;â before âstrâ
lab2.c:26: error: expected expression before â:â token
lab2.c:78: error: âdateCodeâ undeclared (first use in this function)
lab2.c:78: error: âdataTypeâ undeclared (first use in this function)
lab2.c:81: error: âstrâ undeclared (first use in this function)
lab2.c:82: error: âposâ undeclared (first use in this function)
lab2.c:83: error: expected â)â before â:â token
lab2.c:85: warning: passing argument 1 of âprintfâ from incompatible pointer type
/usr/include/stdio.h:339: note: expected âconst char * __restrict__â but argument is of type âstruct _IO_FILE *â

Right off, there are no "string" data types in C. We use char array[sizeofArray];, instead.

Also, you need to use strcmp() to compare string in C. This

if(pos == string::npos)

Is an error for the compare, for the data type, and for the ::. There are no classes in C, and no ::

Try that and see if it looks better.

Well that certainly helped! I am down to 4 compile errors (which are probably syntax errors on my part being new to C). I just replaced the strings with char is there a simple fix? Here is the code followed by the compiler errors

/***************************************
Devang N. Joshi
Homework Two
CSCI 325 - Averaging in C++
January 22nd, 2011

The purpose of this program is to serve
as basic review on simple file I/O in
C using a well structured data file
and performing simple math on it.
***************************************/
#include <stdlib.h>
#include <stdio.h>

int main()
{
	//declare all variables
	FILE *Infile, *Outfile;
	int stationNum = 0; //variable for the station number 
	char dateCode[13];  //variable for the date code
	char dataType[4];   /*variable that shows what "type" the data is
			    example...pwl stands for "primary water level"*/

	/*raw data variables*/
	float d1=0;
	float d2=0; 
	float d3=0;
	float d4=0;
	float d5=0;
	float d6=0;
	float d7=0;
	float d8=0;
	float d9=0;
	float d10=0;
			    //ten variables for water levels over an hour

	float daySum=0;    //sum of all the water levels over 24hrs
	float dayAvg=0;    //average water level for a given day

	float denominator=240;
			    //240 data points per day, used to cal dayAvg

	int rowCount=0;     //counter used to keep track of day change

	//open and test files for input & output
	Infile = fopen ("lab2.dat", "r");
   	if (Infile == NULL)
     	{
      		fprintf(stderr, "Error with input file....terminating program\n\n");
      		exit (1);
     	}
   	Outfile = fopen ("lab2.out", "w");
   	if (Outfile == NULL)
     	{
      		fprintf(stderr, "Error with output file....terminating program\n\n");
      		exit (1);
     	}

	//initial write to output file...write headings to output file
	fprintf(Outfile,"Data\n\n");
	fprintf(Outfile,"Date		Average Water Height\n");
	fprintf(Outfile,"-----------------------------\n\n");

	/*
	Logic to calculate average daily water levels and
	print them to the output files                   
	*/

	
	while (!feof(Infile)) //test against end of file marker
	{
		if(rowCount<24) //at 24 one day has elapsed
		{
			fscanf(Infile,"%d %c %c %f %f %f %f %f %f %f %f %f %f",&stationNum,&dateCode,&dataType,&d1,&d2,&d3,&d4,&d5,&d6,&d7,&d8,&d9,&d10);
			daySum=daySum+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10;

			/*str = dateCode;	//set string to the value of the dateCode
			pos = str.find_first_of("+");//set variable pos to find '+' marker
			if(pos == string::npos)	//simple logic to make sure the 'dateCode' is formated properly
			{
				printf(stderr, "string concatenation error\n");
			}*/

			rowCount++;
			

		}
		else //picked up when one days worth of data is read in, can be averaged now
		{
			dayAvg=daySum/denominator; //calculate the average
			fprintf(Outfile,"%c		%f \n", dateCode, dayAvg);//print data to the output file 
			daySum=0;    //reset daysum
			dayAvg=0;    //reset dayavg
			rowCount=0;  //reset rowcount
		
		}//if-else

		/* in the else portion of the loop the printing to file is done by
		   printing a substring of the whole string str. The length of the 
		   substring is given by the bounds in the () 0->pos. The pos variable
		   gives the location of the '+' marker in the string, and that gives
		   the needed string
		*/
	
	}//while

	/*when the end of file is picked up, the loop terminates
	  however this will cause the final set of averages to fail
	  that is why the final average is calculated below and the
	  the final write to the output file is done */

	dayAvg=daySum/denominator; //calculate the average
	fprintf(Outfile,"%c		%f \n", dateCode, dayAvg);//print data to the output file 

	//final write to output file to mark end of data
	fprintf(Outfile,"\n\n End of data\n");

	//close input & output files
	fclose(Infile);
	fclose(Outfile);

	return 0;
}//main
lab2.c: In function ‘main’:
lab2.c:74: warning: format ‘%c’ expects type ‘char *’, but argument 4 has type ‘char (*)[13]’
lab2.c:74: warning: format ‘%c’ expects type ‘char *’, but argument 5 has type ‘char (*)[4]’
lab2.c:91: warning: format ‘%c’ expects type ‘int’, but argument 3 has type ‘char *’
lab2.c:113: warning: format ‘%c’ expects type ‘int’, but argument 3 has type ‘char *’

I think you messed up the pots. Some characters must be reversed. And they forgot to delete some symbols.

commented: spammer -1

Seems to be the same error - datacode[] is an array of type char, so using %c in fscanf() for it, is an error or warning (at least). Also, the & in front of datacode, is wrong, since the name of the char array, serves as the address to the base of the array.

You should use %s for a string, IF the string is terminated by an end of string char: '\0'. If it's just a collection of letters, then it should be terminated either with a char to mark it (like a comma), or a space (white space).

Ok I just got it to work, I just rewrote it all so I could step through it, I do have one final question however...

My Code prints output as follows:

2011006+2300 1.450729 (it does this line by line, one is a date the other an avg)

Now, if I wanted to print only the 2011006 and not the +2300, is there a way to do that in C?

Thanks (my code is below)

/***************************************
Devang N. Joshi
Homework Two
CSCI 325 - Averaging in C
January 22nd, 2011

The purpose of this program is to serve
as basic review on simple file I/O in
C using a well structured data file
and performing simple math on it.
***************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ()
{
	//declare all variables
	FILE *Infile, *Outfile;
	int stationNum=0; //variable for the station number 
	char dateCode[13];    //variable for the date code
	char dataType[4];    /*variable that shows what "type" the data is
			    example...pwl stands for "primary water level"
			  */
	/*raw data variables*/
	float d1=0;
	float d2=0;         
	float d3=0;	    
	float d4=0;
	float d5=0;
	float d6=0;
	float d7=0;
	float d8=0;
	float d9=0;
	float d10=0;
			    //ten variables for water levels over an hour

	float daySum=0;    //sum of all the water levels over 24hrs
	float dayAvg=0;    //average water level for a given day

	float denominator=240;
			    //240 data points per day, used to cal dayAvg

	int rowCount=0;     //counter used to keep track of day change

   	/***** open input and output files *****/
   	Infile = fopen ("lab2.dat", "r");
   	if (Infile == NULL)
   	{
   		fprintf(stderr, "Error opening input file\n\n");
        	exit (1);
   	}
   	Outfile = fopen ("lab2.out", "w");
   	if (Outfile == NULL)
   	{
   		fprintf(stderr, "Error opening output file\n\n");
        	exit (1);
   	}

        //initial write to output file...write headings to output file
        fprintf(Outfile,"Date	Average Water Height \n");
	fprintf(Outfile,"----------------------------- \n\n");

	/*
	Logic to calculate average daily water levels and
	print them to the output files                   
	*/
   	while (!feof(Infile))
   	{
		if(rowCount<24) //at 24 one day has elapsed
		{

   			fscanf(Infile,"%d %s %s %f %f %f %f %f %f %f %f %f %f",&stationNum,dateCode,dataType,&d1,&d2,&d3,&d4,&d5,&d6,&d7,&d8,&d9,&d10);
			daySum=daySum+d1+d2+d3+d4+d5+d6+d7+d8+d9+d10;
			rowCount++;
		}
		else //picked up when one days worth of data is read in, can be averaged now
		{
			
			dayAvg=daySum/denominator; //calculate the average
			fprintf(Outfile,"%s	%f \n",dateCode,dayAvg); //print data to the output file 
			daySum=0;    //reset daysum
			dayAvg=0;    //reset dayavg
			rowCount=0;  //reset rowcount

        	}//end if/else

     	}// end while

	/* Print final line, and close files */
	fprintf(Outfile,"\n\n End of data \n");
	fclose(Infile);
	fclose(Outfile);

	return 0;

}//end main

never mind I got it figured out :) thanks again everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.