'main' must return 'int'

Hey there

Whenever I compile this (see below) with gcc on Cygwin it returns with:
test.cpp:25: error: 'main' must return 'int';

Here is the source code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

// Use the standard namespace
using namespace std;

// Define the question class
class Question {
private:
  string Question_Text;
  string Answer_1;
  string Answer_2;
  string Answer_3;
  string Answer_4;
  int Correct_Answer;
  int Prize_Amount; //How much the question is worth
public:
  void setValues (string, string, string, string, string, int, int);
};

void main() {
  // Show the title screen
  cout << "****************" << endl;
  cout << "*The Quiz Show*" << endl;
  cout << "By Peter" << endl;
  cout << "****************" << endl;
  cout << endl;

  // Create instances of Question
  Question q1;

  // Set the values of the Question instances
  q1.setValues("What does cout do?", "Eject a CD", "Send text to the printer", "Print text on the screen", "Play a sound", 3, 2500);

}

// Store values for Question variables
void Question::setValues (string q, string a1, string a2, string a3, string a4, int ca, int pa) {
  Question_Text = q;
  Answer_1 = a1;
  Answer_2 = a2;
  Answer_3 = a3;
  Answer_4 = a4;
  Correct_Answer = ca;
  Prize_Amount=pa;

}
The error message is trying to tell you that main must return int.
i.e. Line 21 should be int main() and at the end of your program you must return 0; // obviously only if the program executed successfully
return 0; is superfluous - main (and only main) returns 0 by default when the end of the function is reached.
For people who are new to programming who may not know that it is a good habbit to get into. Also, although somwhat a small amount, it makes the code more readable.
This can be confusing since some compilers allow void main() and you see examples that use it in books and on the web ALL the time (by people who should know better). By the official rules of C++ though, it is wrong.

Topic archived. No new replies allowed.