[prog] School problem, C++ and cout.precision(2);
Ganesh Swami
ganesh at rapidtech.bc.ca
Thu Nov 21 20:25:36 EST 2002
hello,
Some observations:
1. You mix C and C++. C++ has its own file handling mechanism.
2. If you were to use C, you could use printf with formatting chars.
3. See note in the code.
Davis, Jennifer wanted us to know:
> Hi, this is a problem I am dealing with in an assignment at school. For
>
[snip]
>
> #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;
<offtopic>
There is no need for the two arrays to hold student number and mark. You
could use an array of structures.
struct studentrec {
int istudentNo;
float fstudentMark;
};
studentrec rec[35];
</offtopic>
> 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;
You could do with a cast here. Use (float) in front of
fstudentMark[count]
> 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;
For safety, use
average =(float)(totalScore/count);
>
> while (count2<count){
> if (fstudentMark[count2]>average){
> cout << istudentNo[count2] << "\t" << fstudentMark[count2]
> << endl;
Same here.
>
> } //endif
> count2++;
> } // end while
>
> cout << endl << endl;
>
> cout << "Average : "<< average << endl;
> cout << "Total Marks: "<< totalScore << endl;
Same here.
> cout << "Students : "<< count << endl;
>
> cout << "Enter any to to quit";
> getch();
>
> return 0;
> }// end main
>
--
: _ /~\'_
(o o)
=====oooO==U==Oooo================[Ganesh Swami]==========
A seminar on Time Travel will be held two weeks ago.
More information about the Programming
mailing list