#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
struct Grade{
int getA;
int getB;
int getC;
int getD;
int getF;
float marks[100];
};
void init( Grade* student ){
student->getA=0;
student->getB=0;
student->getC=0;
student->getD=0;
student->getF=0;
for( int i=0; i < 100 ; i++)
student->marks[i]=NULL;
}
int readFile( float* sumOfData, Grade* student){
float marks=0.00;
int n=0;
string getName;
ifstream inFile;
cout << "Input file name(include file type): ";
cin >> getName;
inFile.open(getName);
if( !inFile )
cout << "File not exists!!" << endl;
else{
while( inFile >> marks ){
if( marks >=0.00 && marks <= 39.99 ){
++student->getF;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=40.00 && marks <=49.99 ){
++student->getD;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=50.00 && marks <=69.99 ){
++student->getC;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=70.00 && marks <=79.99 ){
++student->getB;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=80.00 && marks <=100 ){
++student->getA;
student->marks[n]=marks;
*sumOfData += marks;
}
n++;
}
inFile.close();
return n;
}
}
void printBarChart( int grade ){
for( ; grade > 0; grade-- )
cout << "\3";
cout << endl;
}
float meanCal( int sumOfData, int n ){
return float(sumOfData)/float(n);
}
float strdDeviationCal( int n, Grade student, int sumOfData){
float sumOfSquareroot=0.0;
for(int i=0;student.marks[i]!=NULL;i++){
sumOfSquareroot+=(student.marks[i]*student.marks[i]);
}
return sqrt(((sumOfSquareroot/n)-(meanCal(sumOfData,n)*meanCal(sumOfData,n))));
}
void main(){
int selection;
cout<< "Please choose one selection of the following below : "<<endl;
cout<< "1. Please enter files name"<<endl;
cout<< "2. Print result"<<endl;
cout<< "3. Print bar chart "<<endl;
printf("Enter your selection : ");
scanf("%d",&selection);http://www.daniweb.com/community/syntax
while(selection != 5) {
switch(selection) {
case 2 :
Grade student;
float sumOfData=0;
int n;
init( &student );
n=readFile( &sumOfData, &student);
system("cls");
cout << "\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4" <<endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout << " Results"<<endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout <<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF<<endl;
cout << "Mean: " << meanCal( sumOfData, n ) << endl;
cout << "Standard Deviation: "<<strdDeviationCal( n, student, sumOfData ) << endl;
for( int i=0; student.marks[i]!=NULL ; i++ ){
cout << student.marks[i] << endl;
}
break;
case 3 :
cout << "-------------------------------------------------------------------------"<<endl;
cout << " Barchart:" << endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout <<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF <<endl;
cout << "Student get A:";
printBarChart( student.getA );
cout << "Student get B:";
printBarChart( student.getB );
cout << "Student get C:";
printBarChart( student.getC );
cout << "Student get D:";
printBarChart( student.getD );
cout << "Student get F:";
printBarChart( student.getF );
ofstream outFile;
outFile.open("output.txt");
outFile<<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF<<endl;
outFile<<"Students get A: "<< student.getA<<endl;
outFile<<"Students get B: "<< student.getB<<endl;
outFile<<"Students get C: "<< student.getC<<endl;
outFile<<"Students get D: "<< student.getD<<endl;
outFile<<"Students get F: "<< student.getF<<endl;
outFile.close();
MessageBox(0,"Thanks for Using My Program!", "THE END", MB_OK );
break;
}
}
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
struct Grade{
int getA;
int getB;
int getC;
int getD;
int getF;
float marks[100];
};
void init( Grade* student ){
student->getA=0;
student->getB=0;
student->getC=0;
student->getD=0;
student->getF=0;
for( int i=0; i < 100 ; i++)
student->marks[i]=NULL;
}
int readFile( float* sumOfData, Grade* student){
float marks=0.00;
int n=0;
string getName;
ifstream inFile;
cout << "Input file name(include file type): ";
cin >> getName;
inFile.open(getName);
if( !inFile )
cout << "File not exists!!" << endl;
else{
while( inFile >> marks ){
if( marks >=0.00 && marks <= 39.99 ){
++student->getF;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=40.00 && marks <=49.99 ){
++student->getD;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=50.00 && marks <=69.99 ){
++student->getC;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=70.00 && marks <=79.99 ){
++student->getB;
student->marks[n]=marks;
*sumOfData += marks;
}else if( marks >=80.00 && marks <=100 ){
++student->getA;
student->marks[n]=marks;
*sumOfData += marks;
}
n++;
}
inFile.close();
return n;
}
}
void printBarChart( int grade ){
for( ; grade > 0; grade-- )
cout << "\3";
cout << endl;
}
float meanCal( int sumOfData, int n ){
return float(sumOfData)/float(n);
}
float strdDeviationCal( int n, Grade student, int sumOfData){
float sumOfSquareroot=0.0;
for(int i=0;student.marks[i]!=NULL;i++){
sumOfSquareroot+=(student.marks[i]*student.marks[i]);
}
return sqrt(((sumOfSquareroot/n)-(meanCal(sumOfData,n)*meanCal(sumOfData,n))));
}
void main(){
int selection;
cout<< "Please choose one selection of the following below : "<<endl;
cout<< "1. Please enter files name"<<endl;
cout<< "2. Print result"<<endl;
cout<< "3. Print bar chart "<<endl;
printf("Enter your selection : ");
scanf("%d",&selection);
while(selection != 5) {
switch(selection) {
case 2 :
Grade student;
float sumOfData=0;
int n;
init( &student );
n=readFile( &sumOfData, &student);
system("cls");
cout << "\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4\4" <<endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout << " Results"<<endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout <<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF<<endl;
cout << "Mean: " << meanCal( sumOfData, n ) << endl;
cout << "Standard Deviation: "<<strdDeviationCal( n, student, sumOfData ) << endl;
for( int i=0; student.marks[i]!=NULL ; i++ ){
cout << student.marks[i] << endl;
}
break;
case 3 :
cout << "-------------------------------------------------------------------------"<<endl;
cout << " Barchart:" << endl;
cout << "-------------------------------------------------------------------------"<<endl;
cout <<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF <<endl;
cout << "Student get A:";
printBarChart( student.getA );
cout << "Student get B:";
printBarChart( student.getB );
cout << "Student get C:";
printBarChart( student.getC );
cout << "Student get D:";
printBarChart( student.getD );
cout << "Student get F:";
printBarChart( student.getF );
ofstream outFile;
outFile.open("output.txt");
outFile<<"Total of Students: "<< student.getA + student.getB + student.getC + student.getD + student.getF<<endl;
outFile<<"Students get A: "<< student.getA<<endl;
outFile<<"Students get B: "<< student.getB<<endl;
outFile<<"Students get C: "<< student.getC<<endl;
outFile<<"Students get D: "<< student.getD<<endl;
outFile<<"Students get F: "<< student.getF<<endl;
outFile.close();
MessageBox(0,"Thanks for Using My Program!", "THE END", MB_OK );
break;
}
}
kmlilo 0 Newbie Poster
Recommended Answers
Jump to Postif( marks >=0.00 && marks <= 39.99 ){
That may not work in all situations. What if marks == 39.991 (which could easily happen with floating-point math on compters). None of the conditions apply so no grade will be assigned. What you should do is something like this:
…
Jump to Post[edit]^^^ LOL exactly the same approach I was suggesting.
One thing you could do is to typecast marks to int and switch on that, but that's kind of long-winded and isn't very appealing.
switch( (int)marks ) { case 100: case 99: case 98: case 97: case 96: …
Jump to PostThis is my original code
That's also the third time you posted your original code; we get it. If you're not willing to read our replies and do the work suggested, while instead asking for "help" with the conversion that clearly means "do it for me", kindly piss …
All 9 Replies
kmlilo 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
kmlilo 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
kmlilo 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
kmlilo 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
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.