1

I'm totally new with c++ and I'm using eclipse.

but... I don't know why I get this error at the main function:

ERROR: ::main must return int

My code is:

void main()
{
char a;
while (a!='q')
{
    string ln = "enter option: ";
    cout<< ln;

    switch(a)
    {
    case 1:
        if (a=='1')
            func1();
        break;
    case 2:
        if (a=='2')
            break;
        break;
    }
}
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Bogdan Maier
  • 663
  • 2
  • 13
  • 19

4 Answers4

5

Because in C++, the main function must have a return type of int.

Your version with a return type of void is incorrect and is being correctly rejected by your compiler.

Just change the declaration from

void main()

to

int main()

There is an alternative form that allows you to process arguments passed on the command line to your program. It looks like this:

int main (int argc, char *argv[])

but when you're just learning C++ and trying to print "hello world" on the screen, this is probably not something you need to worry about. You'll get there eventually.

And consider updating the book you're using to learn C++, too. If it's getting the function signature of the entry point wrong, what other more complicated things might it also be getting wrong?! No point in learning the language wrong the first time around. A list of suggested books is available here.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Wait a second, you min Main cant be a void type function? i`m pretty sure i`ve seen void main() before – Bogdan Maier Mar 11 '12 at 11:47
  • @Bogdan: That would not be correct C++. Other languages might use `void` as the type of the main function, like C#. Additionally, some very old compilers supported `void main`, but it was never standard or correct. – Cody Gray - on strike Mar 11 '12 at 11:49
  • You'll see it often enough, usually in bad text books, and some compilers will even allow it, but it's still wrong. – Paul R Mar 11 '12 at 11:50
  • Thank you, you made it clear, sry for keep asking but i want to learn things the good way :), i learned python 1 semster at university and c seems easy now beside pointer thing, but the differnces ar not many but still pretty big :p i work on a 2 array sort than merg them stil sorted :) thanks – Bogdan Maier Mar 11 '12 at 11:56
  • The C standard permits return types other than int but few compilers actually support that. It's possible that you have seen C code with a void main. – David Heffernan Mar 11 '12 at 17:17
2

You have to change your void main() to int main().

You can't have a main function without any return in C++.

ChapMic
  • 26,954
  • 1
  • 21
  • 20
2
char a;
while (a!='q')

You are comparing an uninitialized variable with the letter q. Reading from an uninitialized variable invokes undefined behavior. If you're unlucky, a!='q' might be false. Change char a; to char a = 0; (or any other non-q value) or replace the while loop with a do-while loop.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
1

The return type should be int:

int main (void)

int main (int argc, char *argv[])

phoxis
  • 60,131
  • 14
  • 81
  • 117