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?!!!
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.
// #include<Windows.h>
#include <iostream>
usingnamespace 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?
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.