Cannot access members in my class C++ ISSUE

Hey! i'm relatively new to programming and i am trying to make a clock using a structure and various for loops, its a clock that retains the time for all planets in our solar system. I got to my 5th planets loop and for some reason i got this error "error: request for member 'hr' in 'j', which is of non-class type 'int'" along with any of the members in my j class. Please somebody explain why this occurs?!!!

code is as follows:
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
[code]
#include<Windows.h>
#include <iostream>
using namespace std;

struct time{
    int hr,mine,sec;
};
int main()
{
    time e;
    e.hr = 0;
    e.mine = 0;
    e.sec = 0;
    time m;
    m.hr = 0;
    m.mine = 0;
    m.sec = 0;
    time mer;
    mer.sec =0;
    mer.mine = 0;
    mer.hr = 0;
    time v;
    v.sec =0;
    v.mine = 0;
    v.hr =0;
    time j;
    j.sec =0;
    j.mine =0;
    j.hr =0;
    {
    for(int i = 0; i<24; i++)
    {
        if(e.hr == 24)
        {
            e.hr = 0;
        }
        for(int j = 0; j<60; j++)
        {
            if(e.mine == 60)
            {
                e.mine = 0;
            }
            for(int k = 0; k<60; k++)
            {
                if(e.sec == 60)
                {
                    e.sec = 0;
                }//Earth
     for(int f = 0; f<25; f++)
     {
        if(m.hr == 25)
        {
            m.hr = 0;
        }
        for(int l = 0; l<61; l++)
        {
            if(m.mine == 61)
            {
                m.mine = 0;
            }
            for(int t = 0; t<62; t++)
            {
                if(m.sec == 62)
                {
                    m.sec = 0;
                }// Mars
      for(int g = 0; g<1408; g++)
    {
        if(mer.hr == 1408)
        {
            mer.hr = 0;
        }
        for(int d = 0; d<128; d++)
        {
            if(mer.mine == 128)
            {
                mer.mine = 0;
            }
            for(int h = 0; h<12; h++)
            {
                if(mer.sec == 12)
                {
                    mer.sec = 0;
                } // Mercury
       for(int g = 0; g<1408; g++)
      {
        if(v.hr == 1408)
        {
            v.hr = 0;
        }
        for(int d = 0; d<128; d++)
        {
            if(v.mine == 128)
            {
                v.mine = 0;
            }
            for(int h = 0; h<12; h++)
            {
                if(v.sec == 12)
                {
                    v.sec = 0;
                } // Venus
    for(int g = 0; g<10; g++)
    {
        if(j.hr == 10)
        {
            j.hr = 0;
        }
        for(int d = 0; d<60; d++)
        {
            if(j.mine == 60)
            {
                j.mine = 0;
            }
            for(int h = 0; h<60; h++)
            {
                if(j.sec == 60)
                {
                    j.sec = 0;
                } // Jupiter
                cout << "Earth Time:" << endl;
                cout<<e.hr<<" : "<<e.mine<<" : "<<e.sec<<endl;
                cout << "Mars Time:" << endl;
                cout<<m.hr<<" : "<<m.mine<<" : "<<m.sec<<endl;
                cout << "Mercury Time:" << endl;
                cout<<mer.hr<<" : "<<mer.mine<<" : "<<mer.sec<<endl;
                cout << "Venus Time:" << endl;
                cout<<v.hr<<" : "<<v.mine<<" : "<<v.sec<<endl;
                cout << "Jupiter Time:" << endl;
                cout<<j.hr<<" : "<<j.mine<<" : "<<j.sec<<endl;
                e.sec++;
                m.sec++;
                mer.sec++;
                v.sec++;
                j.sec++;
                system("color 04");
                system ("title UniverseClock");
                Sleep(1000);
                system("Cls");
    }
        j.mine++;
    }
        j.hr++;
    }
    }
        v.mine++;
    }
        v.hr++;
    }
    }
        mer.mine++;
    }
        mer.hr++;
    }
    }
        m.mine++;
    }
        m.hr++;
    }
    }
        e.mine++;
    }
        e.hr++;
    }
    }
}

Last edited on
Can you please edit your post and add [code] and [/code] around your code, which will hopefully properly indent it?

Edit:
1
2
3
4
5
6
7
8
for(int j = 0; j<60; j++)
{
    ...
        if(j.hr == 10)
        {
            j.hr = 0;
        }
}

j is an int.
It doesn't have a property called "hr" because it's just a built-in type. I'm confused by all those for loops.
Last edited on
.
Last edited on
Below here is your code, indented to reflect the block structure. Note that I had to change struct time to struct Time to avoid a conflict with function time(). See more comments after 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// #include<Windows.h>
#include <iostream>
using namespace std;

struct Time
{
    int hr, mine, sec;
};
int
main()
{
    Time e;
    e.hr = 0;
    e.mine = 0;
    e.sec = 0;
    Time m;
    m.hr = 0;
    m.mine = 0;
    m.sec = 0;
    Time mer;
    mer.sec = 0;
    mer.mine = 0;
    mer.hr = 0;
    Time v;
    v.sec = 0;
    v.mine = 0;
    v.hr = 0;
    Time j;
    j.sec = 0;
    j.mine = 0;
    j.hr = 0;
    {
	for (int i = 0; i < 24; i++) {
	    if (e.hr == 24) {
		e.hr = 0;
	    }
	    for (int j = 0; j < 60; j++) {
		if (e.mine == 60) {
		    e.mine = 0;
		}
		for (int k = 0; k < 60; k++) {
		    if (e.sec == 60) {
			e.sec = 0;
		    }				 //Earth
		    for (int f = 0; f < 25; f++) {
			if (m.hr == 25) {
			    m.hr = 0;
			}
			for (int l = 0; l < 61; l++) {
			    if (m.mine == 61) {
				m.mine = 0;
			    }
			    for (int t = 0; t < 62; t++) {
				if (m.sec == 62) {
				    m.sec = 0;
				}		 // Mars
				for (int g = 0; g < 1408; g++) {
				    if (mer.hr == 1408) {
					mer.hr = 0;
				    }
				    for (int d = 0; d < 128; d++) {
					if (mer.mine == 128) {
					    mer.mine = 0;
					}
					for (int h = 0; h < 12; h++) {
					    if (mer.sec == 12) {
						mer.sec = 0;
					    }	 // Mercury
					    for (int g = 0; g < 1408; g++) {
						if (v.hr == 1408) {
						    v.hr = 0;
						}
						for (int d = 0; d < 128; d++) {
						    if (v.mine == 128) {
							v.mine = 0;
						    }
						    for (int h = 0; h < 12; h++) {
							if (v.sec == 12) {
							    v.sec = 0;
							}	// Venus
							for (int g = 0; g < 10; g++) {
							    if (j.hr == 10) {
								j.hr = 0;
							    }
							    for (int d = 0; d < 60; d++) {
								if (j.mine == 60) {
								    j.mine = 0;
								}
								for (int h = 0; h < 60;
								     h++) {
								    if (j.sec == 60) {
									j.sec = 0;
								    }	// Jupiter
								    cout << "Earth Time:"
									<< endl;
								    cout << e.
									hr << " : " << e.
									mine << " : " <<
									e.sec << endl;
								    cout << "Mars Time:"
									<< endl;
								    cout << m.
									hr << " : " << m.
									mine << " : " <<
									m.sec << endl;
								    cout <<
									"Mercury Time:" <<
									endl;
								    cout << mer.
									hr << " : " <<
									mer.
									mine << " : " <<
									mer.sec << endl;
								    cout << "Venus Time:"
									<< endl;
								    cout << v.
									hr << " : " << v.
									mine << " : " <<
									v.sec << endl;
								    cout <<
									"Jupiter Time:" <<
									endl;
								    cout << j.
									hr << " : " << j.
									mine << " : " <<
									j.sec << endl;
								    e.sec++;
								    m.sec++;
								    mer.sec++;
								    v.sec++;
								    j.sec++;
								    system("color 04");
								    system
									("title UniverseClock");
								    Sleep(1000);
								    system("Cls");
								}
								j.mine++;
							    }
							    j.hr++;
							}
						    }
						    v.mine++;
						}
						v.hr++;
					    }
					}
					mer.mine++;
				    }
				    mer.hr++;
				}
			    }
			    m.mine++;
			}
			m.hr++;
		    }
		}
		e.mine++;
	    }
	    e.hr++;
	}
    }
}

At line 28 you define Time j. But at line 37 you define int j. This int hides Time j. So at lines 82, 83, etc. you're referring to the int. To fix this, change the name of one variable or the other.

Note that these loops are all nested. If I did the multiplication right, that means the inner statements execute about 1.38*1027 times. I suspect that isn't what you wanted.
I applied the change you have provided and there is no longer a compiling error so thank you very much for that! As for the executing statement, i think i want it to as the clock should ideally count up forever so the number of times the loops execute would be absurdly large as you have stated? But again thank you for helping me get rid of the errors!
..... i think i want it to as the clock should ideally count up forever so the number of times the loops execute would be absurdly large as you have stated?


But you have all the times inside each other: for every second of Earth time, there is a whole day of Mars time; for every second of Mars time there is a whole day of Mercury time .... See the problems?

I wouldn't use a for loop to do a clock, instead write a function to increment the time by one second. Then write conversion functions from Earth time to each of the other Planet's time. This leads to the question: Wouldn't the universal time be the same everywhere, ignoring any relativistic considerations? Did you mean local times on each planet?
Oh i'm sorry, I see what you are saying. I do mean local times but i get
for every second of Earth time, there is a whole day of Mars time; for every second of Mars time there is a whole day of Mercury time
So I guess you are going to do an entirely different approach now?
.
Last edited on
.
Last edited on
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
#include<iostream>
#include <ctime>

using std::cin;
using std::cout;

struct Time {
    int hr, min, sec;
};

// Convert seconds to Time
Time hms(int sec)
{
    Time result;
    result.sec = sec % 60;
    sec /= 60;
    result.min = sec % 60;
    result.hr = sec / 60;
    return result;
}

struct Planet {
    int day;			// seconds in a day
    const char *name;
};

int main()
{
    Planet planets[] = {
	{ 50*3600*24 + 15*3600 + 30*60, "Mercury" },
	{ 116*3600*24 + 18*3600, "Venus" },
	{ 24*3600, "Earth" },
	{ 1*3600*24 + 37*60, "Mars" },
	{ 9*3600 + 56*60, "Jupiter"}
    };

    for (int sec = 0; ; ++sec) {    // count up seconds
	for (Planet &p : planets) { // for each planet
	    // get the seconds within a day and convert to hr, min, sec
	    Time t = hms(sec % p.day);
	    int day = sec / p.day;
	    // Print the result. Formatting isn't perfect.
	    cout << "On " << p.name << " the time is "
		 << "day " << day + 1 << ' '
		 << t.hr << ':' << t.min << ':' << t.sec << '\n';
	}
    }
}

.
Last edited on
That's a "ranged based for loop." It was added in C++11 I think. Maybe you have an older compiler. Try this for main instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    Planet planets[] = {
        { 50*3600*24 + 15*3600 + 30*60, "Mercury" },
        { 1*3600*24 + 37*60, "Mars" },
        { 116*3600*24 + 18*3600, "Venus" },
        { 24*3600, "Earth" },
        { 9*3600 + 56*60, "Jupiter"},
        { 0, NULL } // Must be last entry
    };

    for (int sec = 0; ; ++sec) {    // count up seconds
        for (Planet *p = planets; p->day != 0; ++p) { // for each planet
            // get the seconds within a day and convert to hr, min, sec
            Time t = hms(sec % p->day);
            int day = sec / p->day;
            // Print the result. Formatting isn't perfect.
            cout << "On " << p->name << " the time is "
                 << "day " << day + 1 << ' '
                 << t.hr << ':' << t.min << ':' << t.sec << '\n';
        }
    }

.
Last edited on
The calculations are the length of a day on each planet. So a Mars day is 1 (earth) day plus 37 minutes. I found it in that format so rather than doing the calculation, which I might get wrong, I had the compiler do the calculation.
Thanks for that, i ended up making some code based of yours and it worked pretty well!
Topic archived. No new replies allowed.