#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;


}
    }

Recommended Answers

All 9 Replies

wrong code post...this is my code...help me covert to switch case...
case 1 input file name
case 2 print result
case 3 print bar chart
case 4 output file then cout << "File Created" <endl;
case 5 exit

#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;


}
    }

The switch statement isn't well suited to ranges, and it also doesn't support floating point cases, so you're SOL in converting the grade calculating from an if chain to a switch.

May I ask why you need to do this? Is it a requirement from your teacher?

Yup requirement from my teacher....Can u help me to convert ??? because i got another program to do.....

if( 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:

 if( marks >=0.00 && marks < 40.00 ){
    // blabla
} else  if( marks >= 40.00 && marks < 50.00 ){   
   // blabla

ancient dragon can u help me to convert to switch case ???

Can u help me to convert ???

Like I said, you can't do it with floating point. If you want to do this with a switch, which would be stupid in my opinion, you really have little choice but to handle all marks as integers. Even then you'd have something insane like this:

switch ((int)round(marks)) {
    case 0: case 1: case 2: case 3: case 4: case 5:
    case 6: case 7: case 8: case 9: case 10: case 11:
    // ...
    case 34: case 35: case 36: case 37: case 38: case 39:
        ++student->getF;
        student->marks[n] = marks;
        *sumOfData += marks;

    // Repeat for the other marks
}

It's hideously verbose and very easy to make mistakes.

[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:
   case 95: case 94: case 93: case 92: case 91: case 90:
       // blabla
       break;

// etc etc
}

This is my original code

#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(){
    Grade student;
    float sumOfData=0;
    int n;
    init( &student );
    n=readFile( &sumOfData, &student);
    system("cls");
    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;
    }
    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 );
}

This 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 off.

I think I'm done with this thread, since it's becoming more and more apparent that you're a leech. kthxbye.

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.