I know, that there is already a topic discussing this error which says to take the while loop and put it in the main function, but I do have the while loop in the main function, and I still get the same error:
[Error] expected unqualified-id before 'while'
Here is my code:
#include <iostream>
#include <windows.h>
using namespace std;
int main {
MessageBox(0, " TimeKeeper \n Windows version", "About", MB_OK)};
int z = 1;
int hr = 0;
int mn = 00;
while (z = 1) { // this is the bad line
system.clear();
mn = mn + 1;
if (mn == 60) {
mn = 00;
hr = hr + 1;
}
if (mn == 60 && hr == 24) {
hr = 0;
mn = 00;
}
cout << hr << ":" << mn ;
delay(60000);
return 0;
}
}
MessageBox(0, " TimeKeeper \n Windows version", "About", MB_OK) } ;
That extraneous closing brace ends the main function.
After that, the definitions of ints z, hr, and mn are okay outside a function, but the compiler complains when it hits the while, which is not allowed to be outside a function.
BTW, I don't see where delay or system are declared.
Oh, dutch, I now know what I did. It appears that I did not see the } after the ). I put that } after the ) there to overcome another issue, which is what caused this issue.
Actually, it earlier said "expected '}' before ';'" on the line of the MessageBox(); command, and after the } was removed after the ), it still throws the same "expected unqualified-id before 'while'", and the "expected '}' before ';'" reappeared.
A "system.clear();" line is right after the line that starts the while loop; and the delay line is before the "return 0;" line right before the end of the while loop, which delays the while loop for 60000 milliseconds (1 minute).
You are missing the ( ) right after the word main. All function definitions need a parameter list even if it's empty.
There looks like there still might be some other problems, though. E.g., the test (z = 1) should be (z == 1). However, z is never changed in the loop body so the loop will still be infinite. And assigning 00 to something is the same as assigning 0, so it's kind of pointless.
Thanks, that fixed both 2 errors I were facing, but I got a error saying "request for member 'clear' in 'system', which is of non-class type 'int(const char*)'" on the system.clear(); line. I also faced a error saying "'delay' was not declared in this scope", which I fixed on my own by changing the "delay" to "Delay". It appears that C++ commands are case-sensitive.
I can't find any threads on this website or over on Stack Overflow about the "request for member 'clear' in 'system', which is of non-class type 'int(const char*)'" error.
Setting cout's fill character to '0' has nothing to do with clearing the screen. It just uses '0' instead of ' ' to space things out when you use setw.
There is no portable way to clear the screen. You may as well just use system("cls"). (Maybe that's what you were trying to do in the first place?)
??? I see a messagebox. That to me implies gui or graphics code... a floodfill of zero will make that portion of the screen all black, if you are filling a graphical thingy with RGB values of {0,0,0}. Is this remotely what you are asking or is this a console program?? I know you can make a confused console program drop a message box in windows too?
you also had z=1 instead of z==1 in a condition, which could have been intentional, I can't tell.
When I ran the compiled .exe program based on dutch's version that doesn't use the Win32 API, it opened in a Command Prompt window instead of a regular window, probably because it knows the program only uses text, and no graphics or a GUI.
I used x = 1; instead of x == 1; because one = sign indicates that this variable will be assigned this value, as x = 1; will assign the value of 1 to the variable x. A double = sign will indicate a mathematical operation, as x == 5 / 6; will assign the quotient of 5 divided by 6 (which is 0.8333) to the variable x.
I do not believe that all compilers / devices support x++, so that is why I used x = x + 1; instead.
#include <iostream>
#include <cstdlib>
#include <windows.h>
usingnamespace std;
int main() // <===== Remember the () after main
{
MessageBox(0, " TimeKeeper \n Windows version", "About", MB_OK); // <===== Don't put } at the end here
int z = 1;
int hr = 0;
int mn = 0;
while ( z ) // <===== Keeps looping until z is changed to 0
{
system( "cls" ); // <===== Expect some flak for this
mn = mn + 1;
if (mn == 60)
{
mn = 0; // <===== 00 is pointless; doesn't know about multiple zeroes
hr = hr + 1;
if (hr == 24) // <===== Can't be mn == 60 here, 'cos you just set it to 0
{
hr = 0;
mn = 0;
z = 0; // <===== This will kill it at next loop
}
}
cout << hr << ":" << mn ;
Sleep(1000); // <===== Nearest I could get from windows.h
} // <===== My best interpretation of where this brace should be
return 0;
}