0
#include <iostream>
#include <conio.h>

using namespace std;
#define COUNT 10
void main()
{
 void print_NUM(void);
 int add_values(void);
 void print_out(int);
 int SUM;

 print_NUM();
 SUM=add_values();
 print_out(SUM);
}

void print_NUM()
{
 cout << "This program adds " << COUNT << " integers\n";
 cout << "Please enter " << COUNT << " integers to be added\n";
}

I can't run it because it said that the main should be int but this is a direct copy from my lecture notes and the only way to get the answer that I need is when I use int main instead. Why is that?

given159
  • 7
  • 1
  • 3
    Because lecture notes tend to be full of errors? – EOF May 27 '21 at 09:01
  • 1
    Your lecture notes are ancient and have been incorrect for decades, but whoever wrote them can't be bothered to do anything about it. – molbdnilo May 27 '21 at 09:02

3 Answers3

3

C++ standard says that main function has to be of form int main() or int main(int, char**) (of course auto with trailing return types works as well). Any other form is implementation defined http://eel.is/c++draft/basic.start.main#2. Lectures tend to be full of errors and schools teach bad practices/not standard compliant code all the time.

B4mbus
  • 31
  • 2
0

The C langs (C and C++) return an integer from the main function. This is mainly for exit codes. Try changing "void" to "int".

0

Why can't i use the void main?

Because the rules of the language say that main must return int. Violating that rule makes your program ill-formed, which means that compilers are required to issue a diagnostic message, and are allowed to refuse to compile the program. Hence, the error.

but this is a direct copy from my lecture notes

Your lecturer didn't teach you standard C++.

This used to be allowed in some old C++ dialects that pre date the standardisation. That was a long time ago.

eerorika
  • 232,697
  • 12
  • 197
  • 326