Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,515 people online right now. Registration is fast and FREE... Join Now!




C++ Project

 
Reply to this topicStart new topic

C++ Project, spent 10 hours on this and i STILL cant figure it out

pngrafxx126
7 Nov, 2006 - 08:50 AM
Post #1

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
Hey guys I need help. In my project, I have to use void calculateaverage and void calculategrade with &'s in the parameters..

basically the code has to read a students name with his/her 5 test scores
then it has to compute the average test score for each student and assign an appropriate grade letter A B C D E where A is 90 - 100, B is 80-89, C is 70-79.. D is 60-69.. and D is 0-59..

the function void calculateaverage has to determine the average of the five test scores using a loop and sum the five scores.. this function cant output the average because the average has to be computer in the main function..

the calculategrade function has to determine and return each students grade and again it has to be outputted in the main function..

the people in the class are in a seperate file .txt listed like this
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
.. etc..
there are 10 students with random grades..

CODE
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void calculateAverage(ifstream& inp, double& courseAvg);
char calculateGrade(int score);
void printResult(ofstream&outp, string courseId, int groupNo, double avg);

int main()
{
    string sname;
    double avg;
    double classavg;
    ifstream inFile;
    ofstream outFile;
    int grade1, grade2, grade3, grade4, grade5;

    if (!inFile) {
              cout << "File cannot be opened" << endl;
              return 1;
    }
    
    inFile.open("C:\\Documents and Settings\\kfg5\\Desktop\\gpadata.txt");
    outFile << fixed << showpoint << setprecision(2);
    
    int numberofGrades = 5;
    
    outFile << "Name           Grade 1   Grade 2    Grade 3   Grade 4   Grade 5          Average     Grade" << endl;
    
    while (!inFile.eof())
    {
        inFile >> sname >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
        avg = calculateAverage(ifstream& inFile, double& avg);
        letterg = calculateGrade(avg);
        inFile >> sname;
        outFile << sname << "           ";

         inFile >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
          outFile << grade1 << "        " << grade2 << "        " << grade3 << "        " << grade4 << "        " << grade5 << "        " << avg << "        " << letterg;
        
    }
    
void calculateAverage(ifstream& inp, doublt& courseAvg)
{
     double totalScore = 0.0;
     int numberofStudents = 0;
     int score;
    
     inp >> score;
     while (score != -1)
         {
           totalScore = totalScore + score
           numberofStudents++;
           inp >> score;
     }
    
     courseAvg = totalScore / numberofStudents;
}

char calculateGrade(int score)
{
     if (avg >= 90 && avg <= 100)
        return 'A';
     else (avg >= 80 && avg <= 89)
          return 'B';
     else (avg >= 70 && avg <= 79)
          return 'C';
     else (avg >= 60 && avg <= 69)
          return 'D';
     else (avg >= 50 && avg <= 59)
          return 'F';
}

void printResult(ofstream&outp, string courseId, int groupNo, double avg)
{
     if (groupNo == 1)
       outp << "  "  << courseID << "  ";
     else
       outp << "     ";
     outp << setw(8) << groupNo << setw(17) << avg << endl;
}


the problem with this code is that it has errors when i try to compile it and i dont understand how to fix them

This post has been edited by pngrafxx126: 7 Nov, 2006 - 09:32 AM
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: C++ Project
7 Nov, 2006 - 09:15 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 533



Thanked: 3 times
Dream Kudos: 125
My Contributions
you have confused brackets.

Every function body should be starting with { after the heading (which they do in your code) and end with a }
That last } is where some functions are failing.
User is offlineProfile CardPM
+Quote Post

okyup
RE: C++ Project
7 Nov, 2006 - 09:25 AM
Post #3

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
You also have typos in both functions. Can you fix those and post what errors you are getting?
User is offlineProfile CardPM
+Quote Post

pngrafxx126
RE: C++ Project
7 Nov, 2006 - 09:34 AM
Post #4

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
arrite i fixed it up a bit.. the code should only contain a main function a calcaverage and a calcgrade function..

im at a computer in school and it has no compiler so i cant compile and get those errors listed for u

can any of u email me ur contact AIM or something.. id rather talk on that
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: C++ Project
7 Nov, 2006 - 09:42 AM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
WoW...That's a lot of mistakes.
I'm glad I had the patience to fix the code.
Here goes.
Get yourself a cola and read on.
  • CODE

    if (!inFile) {
                  cout << "File cannot be opened" << endl;
                  return 1;
        }
        
        inFile.open("C:\\Documents and Settings\\kfg5\\Desktop\\gpadata.txt");

    This makes no sense. You should check if the file can be opened or not after an attempt has been made to open it. Interchange the positions of the two statements.
  • CODE

    avg = calculateAverage(ifstream& inFile, double& avg);

    Firstly, for passing arguments, you don't have to mention the datatypes of the passed variables. Second, since the calculateAverage() function returns void (ie. doesnt return anything) remove the avg= portion. avg is passed by reference and will contain the average after the function call.
    The corrected line of the code shown above will look like this:
    CODE
    calculateAverage(inFile,avg);
  • CODE

    letterg = calculateGrade(avg);

    letterg is not defined. Add the char letterg; declaration in the beginning of main().
  • CODE
    }...

    Simple enough...remove the "..."
  • CODE

    while (!inFile.eof())
        {
            inFile >> sname >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
            calculateAverage(inFile,avg);
            letterg = calculateGrade(avg);
            inFile >> sname;
            outFile << sname << "           ";

             inFile >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
              outFile << grade1 << "        " << grade2 << "        " << grade3 << "        " << grade4 << "        " << grade5 << "        " << avg << "        " << letterg;
            
        }
        
    void calculateAverage(ifstream& inp, doublt& courseAvg) {
         double totalScore = 0.0;
         int numberofStudents = 0;
         int score;
        
         inp >> score;
         while (score != -1) {
               totalScore = totalScore + score
               numberofStudents++;
               inp >> score;
         }
        
         courseAvg = totalScore / numberofStudents;
    }

    double calculateAverage(int score1, int score2, int score3, int score4, int score5) {
        return ((s1+s2+s3+s4+s5)/5);
    }

    char calculateGrade(int score){
         if (avg >= 90 && avg <= 100)
            return 'A';
         else (avg >= 80 && avg <= 89)
              return 'B';
         else (avg >= 70 && avg <= 79)
              return 'C';
         else (avg >= 60 && avg <= 69)
              return 'D';
         else (avg >= 50 && avg <= 59)
              return 'F';
         }

    void printResult(ofstream&outp, string courseId, int groupNo, double avg) {
         if (groupNo == 1)
           outp << "  "  << courseID << "  ";
         else
           outp << "     ";
         outp << setw(8) << groupNo << setw(17) << avg << endl;

    You are nesting all these functions within main() which is not allowed.
    Add a closing brace after the end of the while loop.
  • CODE
    void calculateAverage(ifstream& inp, doublt& courseAvg)

    This is a typo. Replace doublt with double.
  • CODE
    double classavg;

    Not used in the program. Remove the declaration.
  • CODE
    totalScore = totalScore + score

    Semicolon missing.
  • CODE

    double calculateAverage(int score1, int score2, int score3, int score4, int score5) {
        return ((s1+s2+s3+s4+s5)/5);
    }

    Replace score1,score2,score3,score4,score5 with s1,s2,s3,s4,s5 respectively.
  • CODE

    char calculateGrade(int score){
         if (avg >= 90 && avg <= 100)
            return 'A';
         else (avg >= 80 && avg <= 89)
              return 'B';
         else (avg >= 70 && avg <= 79)
              return 'C';
         else (avg >= 60 && avg <= 69)
              return 'D';
         else (avg >= 50 && avg <= 59)
              return 'F';
         }

    Firstly, avg does not exist. Replace the argument variable name 'score' with avg.
    Secondly, after an else keyword, if you choose to give another condition, you have to add the keyword if.
  • CODE

    void printResult(ofstream&outp, string courseId, int groupNo, double avg) {
         if (groupNo == 1)
           outp << "  "  << courseID << "  ";

    C/C++ is Case Sensitive. courseId and courseID are not the same.
  • CODE

    outp << setw(8) << groupNo << setw(17) << avg << endl;
         }}

    Since, you nested all these functions inside main, we have to remove the last brace which you maintained as the end of main().
And finally, the code compiles.
You have made some very silly errors. Study C++ thoroughly before you attempt to write any program.

Here's the final (edited) code that compiles correctly.
CODE

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void calculateAverage(ifstream& inp, double& courseAvg);
char calculateGrade(int score);
void printResult(ofstream&outp, string courseId, int groupNo, double avg);

int main() {
    string sname;
    double avg;    
    ifstream inFile;
    char letterg;
    ofstream outFile;
    int grade1, grade2, grade3, grade4, grade5;

    inFile.open("C:\\Documents and Settings\\kfg5\\Desktop\\gpadata.txt");

    if (!inFile) {
              cout << "File cannot be opened" << endl;
              return 1;
    }
    
    
    outFile << fixed << showpoint << setprecision(2);
    
    int numberofGrades = 5;
    
    outFile << "Name           Grade 1   Grade 2    Grade 3   Grade 4   Grade 5          Average     Grade" << endl;
    
    while (!inFile.eof())
    {
        inFile >> sname >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
        calculateAverage(inFile,avg);
        letterg = calculateGrade(avg);
        inFile >> sname;
        outFile << sname << "           ";

         inFile >> grade1 >> grade2 >> grade3 >> grade4 >> grade5;
          outFile << grade1 << "        " << grade2 << "        " << grade3 << "        " << grade4 << "        " << grade5 << "        " << avg << "        " << letterg;
        
    }
}
    
void calculateAverage(ifstream& inp, double& courseAvg) {
     double totalScore = 0.0;
     int numberofStudents = 0;
     int score;
    
     inp >> score;
     while (score != -1) {
           totalScore = totalScore + score;
           numberofStudents++;
           inp >> score;
     }
    
     courseAvg = totalScore / numberofStudents;
}

double calculateAverage(int s1, int s2, int s3, int s4, int s5) {
    return ((s1+s2+s3+s4+s5)/5);
}

char calculateGrade(int avg){
     if (avg >= 90 && avg <= 100)
        return 'A';
     else if(avg >= 80 && avg <= 89)
          return 'B';
     else if(avg >= 70 && avg <= 79)
          return 'C';
     else if(avg >= 60 && avg <= 69)
          return 'D';
     else if(avg >= 50 && avg <= 59)
          return 'F';
     }

void printResult(ofstream&outp, string courseId, int groupNo, double avg) {
     if (groupNo == 1)
       outp << "  "  << courseId << "  ";
     else
       outp << "     ";
     outp << setw(8) << groupNo << setw(17) << avg << endl;
     }


P.S : You have mentioned the function prototype of only one of the overloaded functions. While this compiles with VC++, you should still write it's function prototype like this:
CODE

double calculateAverage(int s1, int s2, int s3, int s4, int s5);


Hope you understood where you went wrong.
User is offlineProfile CardPM
+Quote Post

pngrafxx126
RE: C++ Project
7 Nov, 2006 - 11:24 AM
Post #6

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 33


My Contributions
ok I just tried your new code and still get 2 errors

(46) : warning C4715: 'main' : not all control paths return a value

(78) : warning C4715: 'calculateGrade' : not all control paths return a value

This code doesn't read the file and output it though, did you just fix it or did u fix it and try it with an actual file?

create a file gpadata.txt on your desktop with this inside it:

Balto 85 83 77 91 76 -1
Mickey 80 90 95 93 48 -1
Minnie 78 81 11 90 73 -1
Doc 92 83 30 69 87 -1
Goofy 23 45 96 38 59 -1
Duckey 60 85 45 39 67 -1
Grumpy 27 31 52 74 83 -1
Sunny 93 94 89 77 97 -1
Piggy 79 85 28 93 82 -1
Pluto 85 72 49 75 63 -1

____

The calculate average has to be a void function and both functions calcaverage and calcgrade can only output the grade and average in the main function..
User is offlineProfile CardPM
+Quote Post

Jayman
RE: C++ Project
7 Nov, 2006 - 12:22 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
The reason you are getting the warning about main is that you need a return 0; at the end of your main.

As for the other warning, well sounds like you are going to revise that one to not return a value.

But basically you need to account for all possiblites, and this statement limits it. else if(avg >= 50 && avg <= 59)

Should be like this:
CODE

char calculateGrade(int avg){
     if (avg >= 90 && avg <= 100)
        return 'A';
     else if(avg >= 80 && avg <= 89)
          return 'B';
     else if(avg >= 70 && avg <= 79)
          return 'C';
     else if(avg >= 60 && avg <= 69)
          return 'D';
     else
          return 'F';
     }

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month