Program won't compile with structures

My program wont compile and its giving me a lot weird errors. The program is supposed to take occupations and salaries from an input file and sort them. then it puts them into a table and prints it all out into an output file with the average and median. here is the code:

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int MAX_OCCUPATIONS = 10;

bool openFile( ifstream& in );
void sortOccupations( occupation myOccupations );
double calcSalaries( double myOccupations.salary[MAX_OCCUPATIONS] );
void printSalaries( occupation myOccupations, double average, double median );

struct occupation
{
char jobs[MAX_OCCUPATIONS][31];   // titles of occupations and number of occupations
double salary[MAX_OCCUPATIONS];  //   salary per occupations
};

int main()
{
        occupation myOccupations;
        char input_file[50];
        double average, median;  // average and median of all the salaries
        ifstream in;  // input file
         
        while( openFile( input ) )
        {
                sortOccupations( myOccupations );
        }
        calcSalaries( myOccupations.salary );
        printSalaries( myOccupations, average, median );
        return( 0 );
}

/* openFile: prompts user for file and loads structure
   Parameters:
in: ifstream object
   Returns: true if input file is successfully opened
*/
bool openFile( ifstream& in )
{
        occupation myOccupations; // structure where info is put in
        char input_file[50];  // input file used to get info
        int i;

        // prompt for input file
        cout << "Enter the name of the input file\n";
        cin.getline( input_file, 50 );
        input.open( input_file );

        if( input.fail() )
        {
              cout << "Input file " << input_file << "does not exist\n";
              return false;
        }
        while( i < MAX_OCCUPATIONS && !input.eof() )
        {
                in.get( myOccupations.jobs[i] );
                in.get( myOccupations.salary[i] );
        }
}

/* sortOccupations: sorts occupations and puts them in a table
   Parameters:
        myOccupations: structure that holds all the info
   Pre-conditions
        Input file was successfully opened
   Returns nothing
*/
void sortOccupations( occupation myOccupations )
{
        int top, min, temp, i;
        ifstream in;

        // sort occupations
        for( top = 0; top < myOccupations.jobs[i] - 1; top++ )
        {
                min = top;
                for( i = top + 1; i < myOccupations.jobs[i]; i++ )
                {
                        if( myOccupations.jobs[min] > myOccupations.jobs[i] )
                        {
                                min = 1;
                        }
                }
                swap( temp, myOccupations.jobs[top] );
                swap( myOccupations.jobs[top], myOccupations.jobs[min] );
                myOccupations.jobs[min] = temp;
        }

        // put info in table
        while( myOccupations.jobs[i] < MAX_OCCUPATIONS )
        {
        cout << setw(11) << myOccupations.jobs[i] << "     $" << setw(11) << myOccupations.salary[$
        in >> myOccupations.jobs[i] >> myOccupations.salary[i];
        }
}

/* calcSalaries: calculates average and median of salaries
   Parameters:
        myOccupations.salary: salary of each profession
   Pre-Conditions
        structure has been loaded with information
   Returns: average and median of salaries
*/
double calcSalaries( double myOccupations.salary[MAX_OCCUPATIONS] )
{
        double average, median, total;
         int n, middle, middle1;

        for( int j = 0; j < MAX_OCCUPATIONS; j++ )
        {
                total = total + myOccupations.salary[j];
        }
        average = total / myOccupations.salary[j];
        if( n % 2 == 0 )
        {
                middle = n / 2;
                middle1 = (n / 2) - 1;
                median = (myOccupations.salary[middle] + myOccupations.salary[middle1]) / 2;
        }
        else
                middle = n / 2;
                median = myOccupations.salary[median] + 0.5;
}

/* printSalaries: prints the average and median into output file
   Parameters:
        myOccupations: structure with info
        average: average of salaries
        median: median of salaries
   Pre-conditions:
        average and median were successfully calculated
   Returns: nothing
*/
void printSalaries( occupation myOccupations, double average, double median )
{
        ofstream out;

        // write to output file
        out.open( "occupations.out" );
        out << "Average salary: $ " << average << endl;
        out << "Median salary: $ "<< median << endl;
        out.close ( );
}


Any ideas?
My program wont compile and its giving me a lot weird errors


If you have compiler errors - then post them here in full. That saves us having to do "in brain compiling", or loading the file into our own compiler - IMO that is your job.

Aside from that - I have these ideas:

Prefer to use std::string instead of char arrays. That way you can read in a whole string instead of chars. std::string is better all round I think.

Use references rather than pass by value (passing the whole object). Your functions pass arrays, but functions use a local copy of them - so they have no effect on the data. So use references so that they do.

lines 85 - 87 why have you got 2 swap operations & 1 assignment, when you could have 1 swap. That is what swap does - swap things.

line 112 can be better written like this:
total += myOccupations.salary[j];

In the openFile function, you have the return statement inside an if, so the function only returns bool sometimes. Put another return after the while. The function does more than open the file, so consider re-naming it.

the calcSalaries function doesn't return anything. Maybe you meant it to be void, but the arguments need to be references in order for the values in main to change.

Hope all goes well :)



these are all the errors im gettin:

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
lab7.cpp: In function 'bool openFile(std::ifstream&)':
lab7.cpp:62: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::get(char [31])'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:280: note: candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:294: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:321: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:332: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:355: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:365: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]
lab7.cpp:63: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::get(double&)'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:280: note: candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:294: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:321: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:332: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:355: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:365: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]
lab7.cpp: In function 'void sortOccupations(occupation)':
lab7.cpp:81: error: ISO C++ forbids comparison between pointer and integer
lab7.cpp:84: error: ISO C++ forbids comparison between pointer and integer
lab7.cpp:91: error: no matching function for call to 'swap(int&, char [31])'
lab7.cpp:93: error: incompatible types in assignment of 'int' to 'char [31]'
lab7.cpp:97: error: ISO C++ forbids comparison between pointer and integer
lab7.cpp:99: error: 'setw' was not declared in this scope
lab7.cpp: In function 'double calcSalaries(occupation)':
lab7.cpp:120: error: name lookup of 'j' changed for ISO 'for' scoping
lab7.cpp:120: note: (if you use '-fpermissive' G++ will accept your code)
lab7.cpp:129: error: invalid types 'double [10][double]' for array subscript
The lines in your posted code don't seem to match those in the compiler output. You can edit your post with the code and either: 1 Paste all the code; or 2 put a firstline=x in the opening code tag, so the line numbers match up.

Do you have all the compiler warnings turned on? I would have expected more warnings. I use the -Wall -Wextra -pedantic options.

The first 2 lots of errors lines 1-8 & 9-15 in the compiler output seem to be calling the ifstream constructor. This could be because you refer to the variable input which is out of scope here.

There is a bit of confusion with variable names & scope:

1
2
3
        ifstream in;  // input file
         
        while( openFile( input ) )


Error lines 16-21 are about the swap function calls. First, just have 1 swap call like I mentioned earlier, and use strings so you don't have to deal with individual chars. Then your errors should go away. The error was due to you referring to a char array which is really a pointer.

This error :

lab7.cpp:99: error: 'setw' was not declared in this scope


is due to not including the iomanip header file. Always read the documentation for stuff you use.

http://www.cplusplus.com/reference/iomanip/setw/


lab7.cpp:120: error: name lookup of 'j' changed for ISO 'for' scoping


j is out of scope here (line 114) declare it before the for loop.

line 123 has a double as an array subscript - which is pretty obvious from the compiler error.

Hope this helps - let us know how you go.
Topic archived. No new replies allowed.