C++ compiling error?

Hello! I've come across an error while trying to test run my program. It(NetBeans) says Build Failed, but does not point out anything wrong with my program like it usually would. Any thoughts on why it isn't working?
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

class PhoneBook //declaring class phonebook
{
	static string name[]; //declaring a static string array so can use in many functions
	static int number[]; //declaring a static int array so can use in many functions
        
	main() //function main
	{
            menu(); //function main linking to function menu
	}; //end main

	menu() //function menu
	{
		int menuchoice = 0; //declaring int menuchoice
		cout << "---Menu---"; //output
		cout << "1. Input data";
		cout << "2. Sort data Ascending";
		cout << "3. Sort data Descending";
		cout << "4. Print all data";
		cout << "5. Search for an individual";
		cout << "6. End program";
		cout << "Please type a choice number";
		cin >> menuchoice; //input
		switch (menuchoice) //switch statement because an if statement seemed unappropriate for this menu
		{
			case 1: //case 1 of switch
			inputData(); //case 1 links to inputData function
			case 2: //case 2 of switch
			sortingAsc(); //case 2 links to sortingAsc function
			case 3: //case 3 of switch
			sortingDesc(); //case 3 links to sortingDesc function
			case 4: //case 4 of switch
			printing(); //case 4 links to printing function
			case 5: //case 5 of switch
			searching(); //case 5 links to searching function
			case 6: //case 6 of switch
			ending(); //case 6 links to ending function
		} //end switch
	}; //end menu

	inputData() //inputData function
	{
		cout << "Please input 10 names and their corresponding numbers"; //output
		for (int i = 0; i < 10; i++) //for statement
		{
			cout << "Name?"; //output
			cin >> name[i]; //input
		} //end for

		for (int i = 0; i < 10; i++) //for statement
		{
			cout << "Number?"; //output
			cin >> number[i]; //input
		} //end for
		menu(); //links back to menu
	}; //end inputData

	sortingAsc() //sortingAsc function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			for (int j = i + 1; j < 10; j++) //for statement
			{
				if (number[j] < number[i]) //if statement
				{
					const int tempnum = number[i]; //swapping values
					number[i] = number[j]; //swapping values
					number[j] = tempnum; //swapping values

					const string tempstring = name[i]; //swapping values
					name[i] = name[j]; //swapping values
					name[j] = tempstring; //swapping values
				} //end if
			} //end for
		} //end for
		printing(); //links to printing function
	}; //end sortingAsc

	sortingDesc() //sortingDesc function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			for (int j = i + 1; j < 10; j++) //for statement
			{
				if (number[j] > number[i]) //if statement
				{
					const int tempnum = number[j]; //swapping values
					number[j] = number[i]; //swapping values
					number[i] = tempnum; //swapping values

					const string tempstring = name[j]; //swapping values
					name[j] = name[i]; //swapping values
					name[i] = tempstring; //swapping values
				} //end if
			} //end for
		} //end for
		printing(); //links to printing function
	}; //end sortingDesc
        
        printing() //printing function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			cout << name[i] << ": " << number[i] << "\n"; //output
		} //end for
		menu(); //links back to menu function
	}; //end printing

	searching() //searching function
	{
		string searchname; //initializing variable
		cout << "Search for a name"; //output
		cin >> searchname; //input
	        while (searchname != "9999") //while loop
	        {
			int rowName = 0; //declaring variable
			while(rowName < 10 && name[rowName] != searchname) //while loop
			{
				rowName++; //variable increment
			} //end while
			if (rowName >= 10) //if statement
			{
				cout << "That is an invalid search."; //output
				menu(); //links back to menu function
			} //end if
			cout << name[rowName] << ": " << number[rowName]; //output
			menu(); //links back to menu function
	        } //end while
	}; //end searching

	ending() //ending function
	{
		cout << "Thank you! Goodbye"; //output
	}; //end ending 
}; //end class phonebook 
One can't have the main function inside a class. There is a need to instantiate the class with an object, then use that to call the class functions.

Have a look at the tutorial on this site.

Edit:

You have to have types for your functions, void if they don't return anything.
Last edited on
Lines 9-10: How big do you think these arrays are? Hint: The size is undefined.

Lines 31-42: Each case needs a break; statement. Otherwise each case will fall through to the next.

Line 60, 111,129,132: You're calling menu recursively. This is a poor design. For example, if you call menu() at line 129, when you return from menu() you fall through to 131. No telling how times menu() had recursed. What you output on line 131 has no bearing on the nested call to menu().




All of your functions need a return type. In this case void.

declaring a static string array so can use in many functions
All of the class member functions can access them without static.
As AbstractionAnon stated: Arrays require a size [in C++].
I fixed everything, except the part about instantiating the class with an object, and calling the class functions. I'm really bad with functions. How would I do that?
I read the tutorial on this site about classes, but still am unsure how to get my program to run through the menu class. Now my main is after all of my class stuff, after class I put phonebook; and called that in main and now it is saying run successful but I don't know how to get my program to go through the functions?
Can't comment since you haven;t posted your current 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

class PhoneBook //declaring class phonebook
{
	string name[10]; //declaring a static string array so can use in many functions
	int number[10]; //declaring a static int array so can use in many functions
public:

	void menu() //function menu
	{
		int menuchoice = 0; //declaring int menuchoice
		cout << "---Menu---"; //output
		cout << "1. Input data";
		cout << "2. Sort data Ascending";
		cout << "3. Sort data Descending";
		cout << "4. Print all data";
		cout << "5. Search for an individual";
		cout << "6. End program";
		cout << "Please type a choice number";
		cin >> menuchoice; //input
		switch (menuchoice) //switch statement because an if statement seemed unappropriate for this menu
		{
			case 1: //case 1 of switch
			inputData(); //case 1 links to inputData function
                        break;
			case 2: //case 2 of switch
			sortingAsc(); //case 2 links to sortingAsc function
                        break;
			case 3: //case 3 of switch
			sortingDesc(); //case 3 links to sortingDesc function
                        break;
			case 4: //case 4 of switch
			printing(); //case 4 links to printing function
                        break;
			case 5: //case 5 of switch
			searching(); //case 5 links to searching function
                        break;
			case 6: //case 6 of switch
			ending(); //case 6 links to ending function
                        break;
		} //end switch
	}; //end menu

	void inputData() //inputData function
	{
		cout << "Please input 10 names and their corresponding numbers"; //output
		for (int i = 0; i < 10; i++) //for statement
		{
			cout << "Name?"; //output
			cin >> name[i]; //input
		} //end for

		for (int i = 0; i < 10; i++) //for statement
		{
			cout << "Number?"; //output
			cin >> number[i]; //input
		} //end for
		menu(); //links back to menu
	}; //end inputData

	void sortingAsc() //sortingAsc function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			for (int j = i + 1; j < 10; j++) //for statement
			{
				if (number[j] < number[i]) //if statement
				{
					const int tempnum = number[i]; //swapping values
					number[i] = number[j]; //swapping values
					number[j] = tempnum; //swapping values

					const string tempstring = name[i]; //swapping values
					name[i] = name[j]; //swapping values
					name[j] = tempstring; //swapping values
				} //end if
			} //end for
		} //end for
		printing(); //links to printing function
	}; //end sortingAsc

	void sortingDesc() //sortingDesc function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			for (int j = i + 1; j < 10; j++) //for statement
			{
				if (number[j] > number[i]) //if statement
				{
					const int tempnum = number[j]; //swapping values
					number[j] = number[i]; //swapping values
					number[i] = tempnum; //swapping values

					const string tempstring = name[j]; //swapping values
					name[j] = name[i]; //swapping values
					name[i] = tempstring; //swapping values
				} //end if
			} //end for
		} //end for
		printing(); //links to printing function
	}; //end sortingDesc
        
        void printing() //printing function
	{
		for (int i = 0; i < 10; i++) //for statement
		{
			cout << name[i] << ": " << number[i] << "\n"; //output
		} //end for
		menu(); //links back to menu function
	}; //end printing

	void searching() //searching function
	{
		string searchname; //initializing variable
		cout << "Search for a name"; //output
		cin >> searchname; //input
	        while (searchname != "9999") //while loop
	        {
			int rowName = 0; //declaring variable
			while(rowName < 10 && name[rowName] != searchname) //while loop
			{
				rowName++; //variable increment
			} //end while
			if (rowName >= 10) //if statement
			{
				cout << "That is an invalid search."; //output
				break;
			} //end if
			cout << name[rowName] << ": " << number[rowName]; //output
			break;
	        } //end while
                menu(); //links back to menu function
	}; //end searching

	void ending() //ending function
	{
		cout << "Thank you! Goodbye"; //output
	}; //end ending and since there is no linking the program should stop here
} phoneBook; //end class phonebook
        
int main()
{
    phoneBook;
}
Actually, I figured it out. Thank y'all for helping me, I honestly doubt I would have figured it out without the help! Greatly appreciated! :)
Have you tried running your program?

Line 147: That line does nothing. You need to call function members of your class.
147
148
  phoneBook.menu();
  return 0;


Edit:
I see you're still making recursive calls to menu. There are a couple of reasons why this is a bad idea.
1) Every time you make a recursive call to menu(), a additional stack frame is pushed onto the stack. If your program is running for a long time, you will eventually fill the stack and your program will crash.
2) menu() has nothing to do with printing(), sortingAscd(), sortingDesc(), etc. Calls to menu() do not belong in those functions.

I would suggest embedding lines 16-45 inside a while loop. At line 43, after calling ending(), return to exit the menu function back to main.

Line 143: phoneBook should not be global. It should be a local within main().
Last edited on
Topic archived. No new replies allowed.