C++ Help

Discussion in 'OT Technology' started by nfuNk, Apr 12, 2010.

  1. nfuNk

    nfuNk SPARTACUS!!!

    Joined:
    Nov 4, 2009
    Messages:
    5,273
    Likes Received:
    3
    Location:
    Northern Virginia
    I'm doing a programming assignment for my class where we have to use functions to read the student name and scores that you input, calculate the test average, program average, course average, and the letter grade earned and output the result to the monitor as well as to an output text file.

    Basically you need 6 functions

    source code : http://pastebin.com/7aPNVeTQ

    So far I have done these functions
    inputfn
    calcfn
    gradefn
    outputfn

    I haven't gotten to the error check function

    well anyways since I explained what I'm suppose to do. Right now I am having a problem with getting the output to to say the student name, total points, program average, test average, course average, and the letter grade.

    it comes out like this
    [​IMG]
     
    Last edited: Apr 12, 2010
  2. CodeX

    CodeX Guest

    Paste the snippet(s) of code that contains your console output...
     
  3. nfuNk

    nfuNk SPARTACUS!!!

    Joined:
    Nov 4, 2009
    Messages:
    5,273
    Likes Received:
    3
    Location:
    Northern Virginia
    the sourcecode?
     
  4. CodeX

    CodeX Guest

    Yeah but I am not looking through the entire thing on pastebin, just post the relevant parts. It's quite obvious the problem lies in the console output, it seems like you are using the wrong data types or something, or printing overlapping text, probably combined with an underflow/overflow at some point...

    *edit*

    nevermind, it's not that big, I'll get back to you...
     
  5. nfuNk

    nfuNk SPARTACUS!!!

    Joined:
    Nov 4, 2009
    Messages:
    5,273
    Likes Received:
    3
    Location:
    Northern Virginia
    okay thanks
     
  6. CodeX

    CodeX Guest

    Your inputfn function, which gets the data from the user, does not return it. Because of this you are printing uninitialized strings and integers in your print function.

    Here:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<conio.h>
    #include<string>
    
    using namespace std;
    
    //*******************************************************
    //Global Variables, Global Constants, Function Prototypes
    //*******************************************************
    void headerfn();
    void inputfn(string &fName, string &lName, int &prog1, int &prog2, int &prog3, int &test1, int &test2, int &test3);
    void calcfn(int &total, float &pAvg, float &tAvg, float &cAvg,
    int program1, int program2, int program3, int test1, int test2, int test3);
    float gradefn(int cAvg, char&grade);
    void outputfn(string fName, string lName, int total, float pAvg, float tAvg, float cAvg, char grade);
    
    //*******************************************************
    //function definitions
    //*******************************************************
    int main()
    {
        system("color f0");
        string fName = "", lName = "";
        int program1 = 0, program2 = 0, program3 = 0, test1 = 0, test2 = 0, test3 = 0, total = 0;
        float pAvg = 0, tAvg = 0, cAvg = 0;
        char grade = ' ';
    
        headerfn();//function call
        inputfn(fName, lName, program1, program2, program3, test1, test2, test3);
        calcfn(total, pAvg, tAvg, cAvg, program1, program2, program3, test1, test2, test3);  
       
        outputfn(fName, lName, total, pAvg, tAvg, cAvg, grade);
        
        system("pause");
        return 0;
    }//end of main
    
    //*******************************************************
    //functions
    //*******************************************************
    void headerfn()
    {
         cout<<"123456789012345678901234567890123456789012345678901234567890"<<endl;
         cout<<setw(60)<<setfill('*')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"IT 210 Business Applications with C++"
         <<right<<setw(10)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"Programmer: -----"
         <<right<<setw(10)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"Date: April 4th, 2010"
         <<right<<setw(10)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"IT 210 Business Applications with C++"
         <<right<<setw(10)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(59)<<setfill(' ')<<"*"<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"Program Assignment 3: Student Grades III"
         <<right<<setw(10)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(59)<<setfill(' ')<<"*"<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"This program uses functions to read student"
         <<right<<setw(7)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"information from the keyboard and outputs"
         <<right<<setw(9)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(10)<<setfill(' ')<<"*"<<setw(40)<<setfill(' ')<<"the results to the monitor and a text file"
         <<right<<setw(8)<<setfill(' ')<<"*"<<endl;
         cout<<left<<setw(59)<<setfill(' ')<<"*"<<"*"<<endl;
         cout<<setw(60)<<setfill('*')<<"*"<<endl;
    }//end of headerfn
    
    void inputfn(string &fName, string &lName, int &program1, int &program2, int &program3, int &test1, int &test2, int &test3)
    {
         cout << fixed << showpoint << setprecision(2);
         cout << "Please enter your first name: ";
         cin >> fName;
         cout << "Please enter your last name: ";
         cin >> lName;
         cout << "Please enter your first program grade: ";
         cin >> program1;
         cout << "Please enter your second program grade: ";
         cin >> program2;
         cout << "Please enter your third program grade: ";
         cin >> program3;
         cout << "Please enter your first test grade: ";
         cin >> test1;
         cout << "Please enter your second program grade: ";
         cin >> test2;
         cout << "Please enter your third program grade: ";
         cin >> test3;
    }//end of inputfn
    
    int errorckfn(int program1, int program2, int program3, int test1, int test2, int test3)
    {
        return 0;
    }
    
    void calcfn(int &total, float &pAvg, float &tAvg, float &cAvg, int program1, int program2, int program3, int test1, int test2, int test3)
    {
         total = program1 + program2 + program3 + test1 + test2 + test3;
         pAvg = (program1 + program2 + program3) / 3.00;
         tAvg = (test1 + test2 + test3) / 3.00;
         cAvg = total / 6.00;
    }//end of calculation function
         
    float gradefn(int cAvg, char &grade)
    {
        if(grade > 89) cout<<"A"<<endl;
        else if(grade > 79) cout<<"B"<<endl;
        else if(grade > 69) cout<<"C"<<endl;
        else cout<<"F"<<endl;
        return grade;
    }//end of grade function
    
    void outputfn(string fName,string lName,int total, float pAvg, float tAvg, float cAvg, char grade)
    {
         cout<<fixed<<showpoint<<setprecision(2);
         cout<<"1234567890123456789012346578901234567890123456789012345678901234567890"<<endl;
         cout<<setfill('=')<<setw(70)<<"="<<endl;
         cout<<left<<setw(20)<<setfill(' ')<<"Student Grades"
         <<setw(10)<<"Total"
         <<setw(10)<<"Program"
         <<setw(10)<<"Test"
         <<setw(10)<<"Course"
         <<setw(10)<<"Grade"<<endl;
         cout<<setfill(' ')<<setw(20)<<" "
         <<setw(10)<<"Points"
         <<setw(10)<<"Average"
         <<setw(10)<<"Average"
         <<setw(10)<<"Average"<<endl;
         
         cout << setfill('-') << setw(70) << "-" << endl;
         
         cout << setw(20)<<setfill(' ') << fName << setw(10) << pAvg << setw(10) << tAvg << setw(10) << cAvg << setw(10) << grade << endl;
         cout<<setfill('=') << setw(70) << "=" << endl;
         
         return;
    }//end of output
    
     
  7. CodeX

    CodeX Guest

    Is there a reason you pass a half dozen parameters to each of those functions instead of using globals? It's not like memory usage is an issue here or anything.

    Another note, it is good practice to initialize your variables, but that is just being picky (though I suspect you would have found your error more easily if you had), also because visual studio threw about a dozen first chance exceptions at run time because of it...
     
  8. nfuNk

    nfuNk SPARTACUS!!!

    Joined:
    Nov 4, 2009
    Messages:
    5,273
    Likes Received:
    3
    Location:
    Northern Virginia
    I'm new to c++ and that's just how I've been doing it since I started
     

Share This Page