[prog] School problem, C++ and cout.precision(2);

Davis, Jennifer JDavis at JUSTICE.GC.CA
Fri Nov 22 12:33:10 EST 2002


Hi, this is a problem I am dealing with in an assignment at school.  For
some reason, I just can't seem to get the output of this to display
properly.

For some reason, when I try to get the floats to print out, the average
comes out with no decimal places and the total score comes out in scientific
notation.  If it helps, I was using the Borland C++ compiler 5.5 (I think)
on Windows 2000.  

Any help in figuring out why this is happening would be greatly
appreciated...


Jenn


#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>

main(){

struct studentrec{
	int   istudentNo;
	float fstudentMark;
	};//end struct

int   istudentNo[35];
int   count=0, count2=0;
float fstudentMark[35];
float average,totalScore=0;

clrscr();

FILE *studentfile;
struct studentrec student;

	studentfile=fopen("A:\\student.dat","rb");

	while(fread(&student,sizeof(student),1,studentfile)){
	
		istudentNo[count]  =student.istudentNo;
		fstudentMark[count]=student.fstudentMark;
		
		totalScore=totalScore+fstudentMark[count];
		cout << istudentNo[count] << "\t" << fstudentMark[count] <<
endl;
		count++;
	} // end while
		
	fclose(studentfile);

	cout << "This program will display the ID numbers\n";
	cout << "and marks of all students with a greater\n";
	cout << "than average mark.\n\n";

	cout << endl << "Stu.No." << "\t" << "Mark" << endl;
	cout << "=======" << "\t" << "====" << endl;
	average=totalScore/count;

	while (count2<count){
		if (fstudentMark[count2]>average){
		  cout << istudentNo[count2] << "\t" << fstudentMark[count2]
<< endl;
			
		} //endif
		count2++;
	} // end while

	cout << endl << endl;

	cout << "Average    : "<< average << endl;
	cout << "Total Marks: "<< totalScore << endl;
	cout << "Students   : "<< count << endl;

	cout << "Enter any to to quit";
	getch();

	return 0;
}// end main


> Jennifer Davis
> Constitutional & Administrative Law - Droit administratif &
> constitutionnel
> Department of Justice Canada - Ministère de la Justice du Canada
> *(613) 957-4963 - fx (613) 941-1937
> *jdavis at justice.gc.ca
Public Law - http://jusnet.justice.gc.ca/plca_e/default.htm
Droit Public - http://jusnet.justice.gc.ca/plca_f/default.htm




More information about the Programming mailing list