Please help with this code.

My class is having us make a class in a separate header file, use the prototypes in a separate cpp file from where main is. The data has to be read from another file that the teacher will create and store those values into the class. I am having troubles getting my code to understand what is going on.

Here is my header file, I think everything here is alright because I am not getting errors connected straight to this file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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

ifstream fin;

class Month_Info
{
    private:
        float   rainfall,
                hi_temp,
                lo_temp,
                average_temp;

    public:
        void    set_values (Month_Info, ifstream &fin);
}


Here is the file that I am trying to use the function from the prototype in the header file. I am getting errors and I will post those after the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include "cMonthlyRainfall.h"
using namespace std;

void set_values (Month_Info month[], ifstream &fin)
{
    int count = 0,
        i = 12;

    for (count = 0, i = 12; count <= month[i]; count++)
    {
        fin >> month[i].rainfall;
        fin >> month[i].hi_temp;
        fin >> month[i].lo_temp;
    }
}

The errors that I am getting are as follows:
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|11|error: expected unqualified-id before 'using'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp||In function 'void set_values(Month_Info*, std::ifstream&)':|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|18|error: no match for 'operator<=' in 'count <= *(month + ((unsigned int)(((unsigned int)i) * 16u)))'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.h|19|error: 'float Month_Info::rainfall' is private|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|20|error: within this context|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.h|20|error: 'float Month_Info::hi_temp' is private|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|21|error: within this context|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.h|21|error: 'float Month_Info::lo_temp' is private|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|22|error: within this context|
||=== Build finished: 8 errors, 0 warnings ===|

Here is my main cpp, it is not pretty yet but I haven't finished it. I know it breaks so many rules of coding but here it is:

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include "cMonthlyRainfall.h"
using namespace std;

int main()
{
    ifstream fin;

    fin.open ("weather.dat");

    if (!fin)
    {
        cout << "ERROR: Unable to find weather.dat";
        return 1;
    }

    Month_Info  month[12];   //Creates an object for each month

    Month_Info::set_values (month, fin);

    return 0;
}



EDIT: Removed because I wasn't looking closely enough. Looking closer.
Last edited on
Look at this and see how to apply it to your issue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class foo
{
   public:
      void setVar1(float);
   private:
      float var1;
};

// Notice the scope :: operator
void foo::setVar1(float v){
  var1 = v;
  return;
}

int main()
{
   foo arrayFoo[12];
   // Set all of the var1s of each object
   // Or read file information and populate objects
   for (int i(0); i < 12; i++){
      arrayFoo[i].setVar1(i);
   }
   return 0;
}
You want to set each month with the set function. You could use a set_values function to set all of the months but it would have to be a friend of Month_Info (I don't think that is what you want).
Bump, still not working
Not knowing what the total project is, start small and read in the contents of the input file and print them out. Once you have that, I would then make getters and setters for each of the attributes (rainfall, hi_temp, etc ..) in your class. Because your attributes are private you can not access them by using the . operator(month.rainfall will not work). Use those to set the values in your objects.

Look at this and see how to apply it to your issue
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
#include <fstream>
#include <iostream>
using namespace std;

class foo
{
   public:
      void setVar1(float);
      float getVar1(){return var1;}
   private:
      float var1;
};

// Notice the scope :: operator
void foo::setVar1(float v){
  var1 = v;
}

#define NUM_OBJECTS 12
int main()
{
   int      i(0);
   // Open the file
   ifstream inStrm("input.txt");
   // Check to see if all is well
   if ( inStrm ) {
      // Setup some vars
      float  var1(0);
      foo    arrayFoo[NUM_OBJECTS];
      // Start to read the file
      while (inStrm >> var1){
         // Debug
         cout << "Read: " << var1 << endl;
         // Set the members of each object
         arrayFoo[i].setVar1(var1);
         // Move the pointer and check limit
         if (++i == NUM_OBJECTS ) {
            break;
         }
      }
      // Check to see if there was an error reading the file
      if (!inStrm.eof() && i != NUM_OBJECTS) {
         cout << "Error at line: " << i+1 << " in input file" << endl;
      }
      else {
         for ( int j(0); j < i; j++) {
            cout << "Array[" << j << "] var1=" << arrayFoo[j].getVar1() << endl;
         }
      }
      inStrm.close();
   }
   else {
      cout << "Unable to open file input.txt" << endl;
   }
   return 0;
}
$ cat input.txt
1 
2
3
4
5
6
7
8
9
10
11
12
$ ./a.out
Read: 1
Read: 2
Read: 3
Read: 4
Read: 5
Read: 6
Read: 7
Read: 8
Read: 9
Read: 10
Read: 11
Read: 12
Array[0] var1=1
Array[1] var1=2
Array[2] var1=3
Array[3] var1=4
Array[4] var1=5
Array[5] var1=6
Array[6] var1=7
Array[7] var1=8
Array[8] var1=9
Array[9] var1=10
Array[10] var1=11
Array[11] var1=12
Topic archived. No new replies allowed.