How can I correctly call this function?

I'm having trouble calling this function. It's supposed to reformat a name from "First Mid Last"
to "Last, First MidInitial" (If a middle name is available, else just "Last, First)). Though if I change
the function type to string, it gives a different list of errors:

In function âvoid getData(StudentType*, int)â:
71:43: error: invalid use of void expression


Anyone have any suggestions?
************
PROGRAM FILE
************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using namespace std;
#include "StudentType.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <climits>
#include <string>

const int MAX_STDS = 20;

void getData(StudentType[], int&);

void sortData(StudentType[], int&);

void getFormat(string& name);

void computeAverages(StudentType[], int&);

void printData(StudentType[], int&);


int main () {

StudentType students[MAX_STDS];
StudentType();
int n;

getData(students, n);
sortData(students, n);
computeAverages(students, n);
printData(students, n);

return 0;
}

void getData(StudentType students[], int n){
    ifstream fin;
    int grade;
    string filename, name;
    bool done = false;

    cout << "Enter filename: ";
    cin >> filename;
    fin.open(filename.c_str());
    while(true) {
       try {
         fin.open(filename.c_str());
         if(!fin) {
            throw(string("Could not open " + filename + "."));
         }
         break;
       }
       catch (string s) {
          cout << s << endl;
          cout << "Enter a different file name: ";
          cin >> filename;
       }
    }
    n=0;
    while(n<MAX_STDS && getline(fin, name)) {
       students[n].setName(getFormat(name));
       for(int i = 0; i < NUM_GRDS; ++i) {
          fin >> grade;
          students[n].setGrade(grade, i);
       }
       getline(fin, name);
       ++n;
    }
}

void printData(StudentType students[], int n) {
    for(int i = 0; i < n; ++i) {
       students[i].printLine();
    }
}

void computeAverages(StudentType students[], int n) {
    for(int i = 0; i < n; ++i) {
       students[i].computeAverage();
    }
}

void sortData(StudentType students[], int n) {
    for(int i=0; i<n-1; i++) {
       for(int j=0; j < n-1-i; ++j) {
          if(students[j].getName() > students[j+1].getName()) {
             swap(students[j], students[j+1]);
          }
       }
    }
}

void getFormat(string name) {
    string first;
    string middle;
    string last;
    char n, m;
    int size = 0;
    n = name.find(' ');
    first = name.substr(0, n);
    m = name.find(' ', n + 1);
    size = name.size();
    if (m != string::npos) {
       middle = name.substr(n+1, m-(n+1));
       last = name.substr(m+1, size - (m+1));
    }
    else {
       middle = "";
       last = name.substr(n + 1, size - (n + 1));
    }
    name = last + ", " + first;
    if (middle != "") {
       name = (name + ' ') + middle[0];
    }
}

***********
HEADER FILE
***********
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef STUDENTTYPE__H
#define STUDENTTYPE__H

#include <string>
#include<iostream>

const int NUM_GRDS = 10;

class StudentType {
   public:
      StudentType();
      void setName(std::string);
      void setGrade(int, int);
      void computeAverage();
      std::string getName() const;
      void printLine(std::ostream& = std::cout) const;
   private:
      std::string name;
      int grades[NUM_GRDS];
      float avg;
};

#endif 

*******************
IMPLEMENTATION FILE
*******************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "StudentType.h"
#include <iomanip>

StudentType::StudentType(){
    name = "";
    for(int i =0; i <NUM_GRDS; ++i){
       grades[i] = 0;
    }
    avg = 0.0;
}

void StudentType::setName(std::string newName){
    name = newName;
}

void StudentType::setGrade(int grade, int num){
    grades[num] = grade;
}

void StudentType::computeAverage(){
    float total = 0;
    for(int i = 0; i<NUM_GRDS; ++i){
       total += grades[i];
    }
    avg = total/NUM_GRDS;
}

std::string StudentType::getName() const{
    return name;
}

void StudentType::printLine(std::ostream& out) const {
//    out.setf(ios::left);
//    out.setf(ios::fixed);
//    out.setf(ios::showpoint);
    out << "\n" << std::setw(25) << "Student" << std::setw(50)
        << "Grades" << std::setw(10) << "Average" << std::endl;
    out << "_____________________________________________________________________________________" << std::endl;
    out << std::left << std::setw(25) << name << std::right << ' ';
    for(int i = 0; i < NUM_GRDS; ++i){
    out << std::setw(5) << grades[i] << ' ';
    }
    out << std::setprecision(2) << std::setw(6) << avg << std::endl;
}

int main (){

return 0;
}

**************
My output should look like this after the program compiles...
**************

Enter file name: grades.dat

Student                  Grades                                            Average
________________________________________________________________________________________
Last, First              90   80   70   60   50   40   30   20   10   0    45.00
Last, First              40   40   40   40   40   40   40   40   40   40   40.00
Last, First              54   98   65   32   21   87   54   65   98   32   60.60
Flames, Blood A          9    8    7    6    5    4    3    2    1    0    4.50
Bottoms, Car             32   65   98   87   54   24   56   89   78   68   65.10
Guitars, Dean            10   10   10   10   10   10   10   10   10   10   10.00
Honer, Ruth T            78   56   12   23   45   89   31   64   97   79   57.40
Hot, Zepher R            12   54   87   89   56   32   51   46   97   31   55.50
.
.
.



The input file should have this format and include over 20 students for testing purposes:
g0, g1,...g9 should be 10 grades ranging from 0 to 100

First Middle Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9
First Last
g0 g1 g2 g3 g4 g5 g6 g7 g8 g9
I think you need to initialize n before you pass it in. I know that in C# you need to initialize reference variables first else you use out variables.

Also, your definition doesn't match you declaration signature. You only put & in the declarations.
Last edited on
n is initialized on Line 69: n=0;
I was looking at this code:


1
2
3
4
5
6
7
8
9
10
11
12
13
int main () {

StudentType students[MAX_STDS];
StudentType();
int n;

getData(students, n);
sortData(students, n);
computeAverages(students, n);
printData(students, n);

return 0;
}



Either way, you might want to add & to your function definitions.
I didn't read the problem, nor am I here for a useful reason, but I just couldn't resist posting this
StudentType students[MAX_STDS]; the max amount of STDs those poor students can have?
Oh, okay. Could you give an example of the suggestion?
You are absolutely correct, Vidminas... I'm sure some of the rich ones have them too....
Last edited on
Poor not poor :D I don't mean how rich or poor they are, but how unlucky... :P
Last edited on
You didn't quite catch the sarcasm/rebutal joke. :D
Last edited on
added &'s, changed getFormat to string type, deleted my main function within the implement file, linked using g++ programfile.cpp implementationFile.cpp,
Topic archived. No new replies allowed.