0
#include<iostream>
using namespace std;

int main() 
{
    cout << "Welcome to this video"<<"\n";

    int a;
    a = 10;

    switch (a) {
        default:
        cout << "default is executed whatever happens";  
        case 10:
        cout << "a is 10" << "\n";
        case 5:
        cout << "a is now 5" << "\n";
    }
    return 0;
}

This is my code. When I am trying to comment line:

default:
cout << "default is executed whatever happens"; 

and then when I am saving and running the code I am getting the error

C:/Program Files/mingw-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

But when I am closing and opening my terminal 2-3 times and then running the code, my code is working fine after 2-3 attempts. Similarly when I am uncommenting the commented code and when I am trying to run it I am getting error:

tempCodeRunnerFile.cpp:1:7: error: expected unqualified-id before 'default'
       default:

Can anyone please tell me why C++ is working like this. I am using VS code as my code editor and my GCC version is 8.1.0.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Ash
  • 35
  • 6
  • 1
    I don't know if that's what causing your main problem, but you should have a `break` in every case – alonkh2 Dec 20 '21 at 15:48
  • Even if I add break above errors are still coming and I have to execute my code 2-3 times after saving it so that it can run properly – Ash Dec 20 '21 at 15:50
  • Unrelated note: ["using namespace std;", bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – digito_evo Dec 20 '21 at 15:51
  • Have a look at this - https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw – alonkh2 Dec 20 '21 at 15:53
  • I am a beginner in C++ .I do not have much understanding of bad practices in C++. Thankyou for telling me that – Ash Dec 20 '21 at 15:53
  • @alonkh2 Why is it working after not working for 2-3 times? – Ash Dec 20 '21 at 15:56
  • I really don't know. Might have to do with your system architecture? I see that the directory is called mingw-w64 - do you maybe have a 32bit architecture? That's my best guess – alonkh2 Dec 20 '21 at 15:59
  • No I have 64bit architecture – Ash Dec 20 '21 at 16:01
  • 2
    @Ash There is no problem with being a beginner. Most of the beginners use bad practices unintentionally. But as long as you keep learning and start to avoid bad practices then you're good to go. In this case, you should not get used to using `using namespace std;` since it will be hard for you to stop writing it later in your career. Better to grow some of the good habits from the start than to try to ditch them a year later. – digito_evo Dec 20 '21 at 16:01
  • 1
    You are correct – Ash Dec 20 '21 at 16:02
  • 1
    Yes, like @digito_evo mentioned, you'll thank yourself if you stop `using namespace std` right now. **IF** the reason you're using it is to save time writing things like `cout` you could instead `using namespace std::cout, std::string;`. Also another quick note, `\n` counts as a single character, so you could very well write something like this: `std::cout << "Text" << '\n';`. Doesn't really change anything but I thought I'd mention it. – avighnac Dec 20 '21 at 18:16

0 Answers0