C++ Class Program

I've been getting some compiler errors for my class code for the setWeather[i] lines in the main program, and "request for member setWeather in inputWeather . which is a non-class type" errors.
Any assistance would be appreciated.
Thank you for your time.


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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//Date class
class Weather
{
  private:
        string name;
        int temperature;
        int dewPoint;
        string winds;
        float pressure;
        string weather;
    public:
        void setWeather(string, int, int, string, float, string);
        void weatherFormat();
        Weather(string, int, int, string, float, string);
};

//Default constructor
Weather::Weather(string n, int t, int dP, string wi, float p, string we)
{
    name = n;
    dewPoint = dP;
    winds = wi;
    pressure = p;
    weather = we;
}

//Function to set the weather variables
void Weather::setWeather(string n, int t, int dP, string wi, float p, string we)
{
    name = n;
    dewPoint = dP;
    winds = wi;
    pressure = p;
    weather = we;
}

//Function to print weather data
void Weather::weatherFormat()
{
    string name;
    int temperature;
    int dewPoint;
    string winds;
    float pressure;
    string weather;
      
    cout << "At ";
    cout << name << ", the temperature is ";
    cout << temperature << " degrees Fahrenheit, the dew point is ";
    cout << dewPoint << "degrees Fahrenheit, winds are from the";
    cout << winds << " , pressure is ";
    cout << pressure << " inches of mercury, and it is ";
    cout << weather;
    cout << endl << endl;
}

//Main program
main()
{
    const int size = 25;
    string name;
    int temperature;
    int dewPoint;
    string winds;
    float pressure;
    string weather;
    int i = 0;
    Weather inputWeather();
   
    fstream inputFile;
    inputFile.open("c:\\weatherObs.txt");
    for (i = 0; i < size; i++)
    {
        inputFile >> setWeather[i].name;
        inputFile >> setWeather[i].temperature;
        inputFile >> setWeather[i].dewPoint;
        inputFile >> setWeather[i].winds;
        inputFile >> setWeather[i].pressure;
        inputFile >> setWeather[i].weather;
    }
   
    inputWeather.setWeather(name, temperature, dewPoint, winds, pressure, weather);
    inputWeather.weatherFormat();
    
    cout << endl;
    cout << "\nNormal job termination\n" << endl;
    system("pause");
    return 0;
}
Last edited on
In the function weatherFormat you are declaring some variables local to the function with the same names as the ones in the class the function is a member of. As such, the local variables are hiding the class ones (I believe, since local scope should take precedence over class scope.) This would be ok if you are wanting the function to actually output something other then the class data members, but you haven't initialized the local variables to anything either. I'm guessing you simply want that function to output the objects internal data so you should remove the local variables. Then it will print the calling objects data.

Also, please use the code tags (the <> button to the right when posting or editing) to post code as it makes it much easier to read and preserves indentation (sort of.)
Last edited on
Thank you for the response.
Now I'm getting some different errors.
Something about members being a non-class type.
It's all from the main function.

Here's the code that I have now:

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
//Main program
main()
{
   const int size = 25;
   Display();
   Weather inputWeather();
   fstream inputFile;
   Weather setWeather(string name, int temperature, int dewPoint, string winds, float pressure, string weather);
   inputFile.open("c:\\weatherObs.txt");
   for (int i = 0; i < size; i++)
   {
      inputFile >> setWeather[i].name;
      inputFile >> setWeather[i].temperature;
      inputFile >> setWeather[i].dewPoint;
      inputFile >> setWeather[i].winds;
      inputFile >> setWeather[i].pressure;
      inputFile >> setWeather[i].weather;
      cout << endl;
   }
   
   Weather inputWeather();
   inputWeather.setWeather();
   inputWeather.weatherFormat();
   
   cout << endl;
   cout << "\nNormal job termination\n" << endl;
   system("pause");
   return 0;
}
You haven't instantiated an object of the Weather class. Before you can use the functions that are members of the class you need an object for them to operate from and on. You can create an object of the Weather class just like an integrated type like so:

Weather weatherObj1;

Also, one line 6 it looks like you are trying to call the inputWeather function so why did you add a type to it? And your attempted call on line 8 to setWeather is a declaration not a function call.
Last edited on
I apologize, but I still do not understand this program.
Now after I've worked on the program, it will compile, but it will output the wrong values and the program will crash.

Here's the updated 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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Date class
class Weather
{
    public:
        string name;
        int temperature;
        int dewPoint;
        string winds;
        float pressure;
        string weather;
};

// Function prototypes
void InputFromFile(Weather[], int);
int DisplayInfo(Weather[], int);

//Main program
main()
{
    const int size = 25;      
    Weather inputWeather[size];
    InputFromFile(inputWeather, size);
    DisplayInfo(inputWeather, size);
    cout << endl;
    cout << "\nNormal job termination\n" << endl;
    system("pause");
    return 0;
}

// Functon that inputs the data from a file into the program
void InputFromFile(Weather inputWeather[], int size)
{ 
    fstream inputFile;
    inputFile.open("c:\\periodic.txt");
    
    for (int i = 0; i < size; i++)
    {
       inputFile >> inputWeather[i].name;
       inputFile >> inputWeather[i].temperature;
       inputFile >> inputWeather[i].dewPoint;
       inputFile >> inputWeather[i].winds;
       inputFile >> inputWeather[i].pressure;
       inputFile >> inputWeather[i].weather;
    }
}

// Displays the data
int DisplayInfo(Weather inputWeather[], int size)
{
   
   cout << inputWeather[size].name << endl;
   cout << inputWeather[size].temperature << endl;
   cout << inputWeather[size].dewPoint << endl;
   cout << inputWeather[size].winds << endl;
   cout << inputWeather[size].pressure << endl;
   cout << inputWeather[size].weather << endl;
}
Last edited on
In DisplayInfo, you are using inputWeather[size].name, etc, to output the info. That is outside of the array of inputWeather[size]. Remember, an array of size 25 has elements numbered from 0 -24. Also, that function only outputs a single element. If you want to output the data from all the element you'll need to use a for loop like you did from the input function.

Also, one of the major points of classes is data hiding. By making all the data members public you are skipping this point of classes and could have simply used a struct. I'm not sure how far you are into learning about classes, and have yet to learn about making data members private and accessing them using member functions, but if you have that'd be the better way to go. Of course, that'd mean taking the for loops out of the input and display functions and instead write a loop in main that would iterate across each object and input to it by calling the input function of each object itself.

Here I reworked your program a bit to make the functions members. I also cleaned up a few other things (no int for main's return type, also DisplayInfo listed as returning int when it had no return statement and doesn't need to return anything, I also had to inlcude <cstdlib> to make the system pause statement work. Usually, you shouldn't use system statements, but it's not a HUGE concern atm.)

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>


using namespace std;

const int SIZE = 25;

//Date class
class Weather
{
    private:
        string name;
        int temperature;
        int dewPoint;
        string winds;
        float pressure;
        string weather;

    public:
        void InputFromFile(fstream&); // moved these 2 function into the class def making them member functions
        int DisplayInfo();
};



//Main program
int main()  // You forgot the return type on main here
{
    Weather inputWeather[SIZE];
    fstream inputFile;
    inputFile.open("c:\\periodic.txt");

    for (int i = 0; i < SIZE; ++i)
        inputWeather[i].InputFromFile(inputFile);

    for (int i = 0; i < SIZE; ++i)
        inputWeather[i].DisplayInfo();

    cout << endl;
    cout << "\nNormal job termination\n" << endl;
    system("pause");
    return 0;
}

// Functon that inputs the data from a file into the program
void Weather::InputFromFile(fstream& infile)
{
    infile >> name;
    infile >> temperature;
    infile >> dewPoint;
    infile >> winds;
    infile >> pressure;
    infile >> weather;
}

// Displays the data
void Weather::DisplayInfo() // changed return type to void
{
    cout << name << endl; 
    cout << temperature << endl;
    cout << dewPoint << endl;
    cout << winds << endl;
    cout << pressure << endl;
    cout << weather << endl;
}


Note: I don't have your text file so I can't test the output. Therefore I can't guarantee that it will work with your input. But if you have any further problems post here and we'll see if we can help you more.
Last edited on
Topic archived. No new replies allowed.