Why can't I call main();?

Here is my 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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream> 
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>

using namespace std;

void again()
{
	string answer;
	cout << "Would you like to enter another set of data? Yes or No?" << endl;
	cin >> answer;
	string yes = "Yes";
	string no = "No";
	
	if(answer == yes)
	{
		main();
	}
}

int main()
{
   cout << "Kaitlin Stevers" << endl;
   cout << "Exercise 11 - Vectors" << endl;
   cout << "November 12, 2016" <<endl;
   cout << endl;
   cout << endl;
   int size;
   cout << " How many numbers would you like the vector to hold? " << endl;
   cin >> size;
   vector<int> numbers;
   int bnumbers;

   for (int count = 0; count < size; count++)
   {
       cout << "Enter a number: " << endl;
       cin >> bnumbers;
       numbers.push_back(bnumbers); // Adds an element to numbers
    }
    //display the numbers stored in order
    cout << "The numbers in order are: " << endl;
    for(int bcount = 0; bcount < size; bcount++)
    {
        cout << numbers[bcount] << " ";
    }
    cout << endl;
    //display the numbers stored reversed
    cout << "Here are the numbers in reverse order: " << endl;
    
    reverse(numbers.begin(), numbers.end());
    for(int rcount = 0; rcount < size; rcount++)
    {
		cout << numbers[rcount] << " ";
	}
	cout << endl;
	again();
	return 0;
}


I keep getting the error: use of undeclared identifier 'main'
I don't understand why I can't call the function main!
Please help me understand and tell me what to do in order to get it to return to main.
The compiler looks at your code only once, reading from the top down, in order.

When you write your call to main, the compiler doesn't yet know that it exists -- and so it immediately complains. In order to solve that problem, you will need to tell the compiler that main() exists before you use it.

Since your functions are mutually recursive, you need to introduce a function declaration (sometimes a "prototype") before it's been used.

Add the line int main(); somewhere before the definition of again() on line 9.

Unfortunately, this is all moot, because you must not call the main function in C++.

Quoting ISO/IEC 14882:2011, 3.6.1 [basic.start.main]:
The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill-formed.

The standard uses the wording shall, shall not to indicate requirements or prohibitions.

So the solution is to use a loop instead of recursively invoking main().
Last edited on
Ahhhh. Thank you. That makes since! I guess I'm going to write a loop lol
This is gonna be a long night..

vola!
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
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream> 
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>

using namespace std;

void again()
{
	void first();
	string answer;
	cout << "Would you like to enter another set of data? Yes or No?" << endl;
	cin >> answer;
	string yes = "Yes";
	string no = "No";
	first();
}
void first()
{
   int size;
   cout << " How many numbers would you like the vector to hold? " << endl;
   cin >> size;
   vector<int> numbers;
   int bnumbers;

   for (int count = 0; count < size; count++)
   {
       cout << "Enter a number: " << endl;
       cin >> bnumbers;
       numbers.push_back(bnumbers); // Adds an element to numbers
    }
    //display the numbers stored in order
    cout << "The numbers in order are: " << endl;
    for(int bcount = 0; bcount < size; bcount++)
    {
        cout << numbers[bcount] << " ";
    }
    cout << endl;
    //display the numbers stored reversed
    cout << "Here are the numbers in reverse order: " << endl;
    
    reverse(numbers.begin(), numbers.end());
    for(int rcount = 0; rcount < size; rcount++)
    {
		cout << numbers[rcount] << " ";
	}
	cout << endl;
	again();
}
int main()
{
   cout << "Kaitlin Stevers" << endl;
   cout << "Exercise 11 - Vectors" << endl;
   cout << "November 12, 2016" <<endl;
   cout << endl;
   cout << endl;
   first();
}
Last edited on
Topic archived. No new replies allowed.