[Error] expected unquailified-id before 'while'

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;
}
}
Look at the end of this line:

 
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.
Last edited on
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.

Thanks for pointing this out, dutch.
In reply to dutch

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.
Last edited on
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.
I've never heard of system.clear() so I can't help you with that. Where did you hear of it from?

Here's an example of the kind of thing you seem to want. I avoided using the Windows libraries since doing so makes the code non-portable.

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
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
using namespace std;

void delay_ms(int ms) {
    std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}

int main () {
    cout << setfill('0');

    for (int hr = 0, mn = 0; ; ) { // infinite loop (ctrl-c to stop)
        if (++mn == 60) {
            mn = 0;
            if (++hr == 24)
                hr = 0;
        }
        cout << hr << ":" << setw(2) << mn << '\n';
        delay_ms(1000);
    }

    return 0;
}

Last edited on
In reply to dutch

Is what you have either the entire program without the Win32 API, or is it just the problematic code that you rewritten? I used the Win32 API for:

* using the MessageBox command to display a "About" message on boot-up (the closest thing I could get to a splash-screen)

* using the System.clear(); command to clear the output display (does the cout << setfill('0'); do that?)

If it is the entire program without the Win32 API, then I could sacrifice those two features for the trade-off of only 25 lines of code.
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.


I see a messagebox. That to me implies gui or graphics code

It's obviously a console program. You can pop up a message box from a console program if you want (although it's a somewhat strange thing to do).

you also had z=1 instead of z==1 in a condition

I already said that. (No wonder your post count is so high!)
In reply to jonnin and dutch

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.
Hold old @Claudia1969,

x = 1;
Is assignment.

x == 1;
Is COMPARISON.

x == 5/6;
Compares x to the quotient 5/6, returning a true or false, false if x is not 5/6.

It makes no assignment.

I know of no C compiler that does not support ++x or x++. It is a basic operator that's been part of C since the very beginning.
Try this @Claudia1969

I've sped it up by a factor of 60, or you are going to get REALLY bored!!!

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
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace 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;
}

Last edited on
Topic archived. No new replies allowed.