Desperately need help with functions and arrays

Hello, can someone explain how functions and arrays interact? Specifically, I am writing a program where I need to use several functions, each of which takes a name, a GPA, and a year attended at school, compiles that data into an index of an array, and then generates a random 5 digit student ID for the student. I have a good bit of the program done, albeit not working. I am really unsure about how to get my functions to interact with my array properly to get this code up and running.

Please, any help would be appreciated!

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
  #include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
using namespace std;
void print_menu();
void print_all(string student_name, int r_identifier, double gpa_entry, int index);
void input_new_student();
int get_selection(int menu_Selection);
double get_gpa();
int get_year();
int print_by_year();
string get_name();
int print_statistics();

int main() //Handles the if statements concerning the menu of the program
{
	int r_identifier[42]; //Variable Declaration
	int year_entry[42];
	double gpa_entry[42];
	string student_name[42];
	int* index;
	bool reg_run = false; //Sets program innately to false
	do
	{
		int menuchoice; //Variable Declaration
		int gpa_level;
		print_menu(); //Calls function to print menu
		get_selection(menuchoice); //Calls function to get the menu selection
		if (menuchoice == 1) //Calls the function to input a new user
		{
			input_new_student();
		}
		if (menuchoice == 2) //Prints all
		{
			print_all (student_name, r_identifier, gpa_entry, index);
		}
		if (menuchoice == 3) //Prints statistics about all students in a particular year
		{
			print_by_year();
		}
		if (menuchoice == 4) //Prints statistics about all entered users
		{
			print_statistics();
		}
		if (menuchoice == 5) //Quits the program
		{
			cout << "Have a good summer! ";
			cout << endl;
			reg_run = true; //Sets reg run to true, ending the program
		}
	} while (reg_run = false);

	return 0;
}
void print_menu() //Prints Registrar menu
{
	cout << "RCNJ Registrar Menu: " << endl;
	cout << "\n[1] Add a student" << endl;
	cout << "[2] Display all students" << endl;
	cout << "[3] Display by year" << endl;
	cout << "[4] Display statistics" << endl;
	cout << "[5] Quit" << endl;
}
void input_new_student() //Calls all necessary functions to input a new student
{
	for (int i = 0; i < 42; i++) //For loop to run 42 times as to set limit on student
	{
		string student_name[42];
		int year_entry[42];
		double gpa_entry[42];
		int index;
		student_name[index] = get_name(); //Sets student name entry to index allocation
		year_entry[index] = get_year(); //Sets year entry to index allocation
		gpa_entry[index] = get_gpa(); //Sets gpa entry to index allocation
	}

}
int get_selection(int menu_Selection) //Gets the menu selection
{
	cout << "\nSelection: "; 
	cin >> menu_Selection;
	while (menu_Selection < 1 || menu_Selection > 5) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> menu_Selection;
	}
	return menu_Selection; //Returns the user inputted menu selection
}
double get_gpa() //Function to get the GPA 
{
	double gpa_entry;
	cout << "Please enter the GPA: ";
	cin >> gpa_entry;
	while (gpa_entry < 0.0 || gpa_entry > 4.0) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> gpa_entry;
	}
	return gpa_entry; //Returns the gpa entry to input new student
}
int get_year() //Function to get year
{
	int year_entry;
	cout << "Please enter the year: ";
	cin >> year_entry;
	while (year_entry < 1972 || year_entry > 2016) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> year_entry;
	}
	return year_entry; //Returns year entry to input new student
}
int print_by_year() //Function to print student information by year attended
{
	int year_view;
	cout << "What year would you like to display?";
	cin >> year_view;
	while (year_view < 1972 || year_view > 2016) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> year_view;
	}

}
string get_name() //Function to get student name
{
	string student_name;
	cout << "Please enter the student's name: ";
	cin >> student_name;
	return student_name; //Returns student name to input new student function
}
int print_statistics() //Function which prints all statistics about students
{
	
}
void print_all(string & student_name[42], int & r_identifier[42], double & gpa_entry[42], int index)
{
	for (int i = 0; i < 42; i++)
	{
		cout << student_name[index] << " (" << r_identifier[index] << ") - " << gpa_entry[index] << endl;
	}
}


I am not allowed to use structures, classes, objects, etc for this program as a side note.
Well the most obvious thing when something isn't passing into a function is to make sure you're passing it correctly, and that what you're passing has a value.

For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14

void print_all(string student_name, int r_identifier, double gpa_entry, int index);
//This function looks like it is taking a string, int, double, and int.

//However down in main you declare these variables like so
	int r_identifier[42]; //Variable Declaration
	int year_entry[42];
	double gpa_entry[42];
	string student_name[42];
//These are arrays! But the function doesnt say that it wants arrays

//then you call the function 
print_all (student_name, r_identifier, gpa_entry, index);
//and pass it what YOU think it wants! But it can't receive Arrays! 


So when passing arrays to functions it should be
1
2
3
4
5
6
7
8
9
void function (int array[], double List[], int StudentList[]);

//...in main
//declare arrays
//pass arrays

function (array, List, StudentList);

Last edited on
So in my case should it be?

1
2
3
4
5
6
//Function 
void print_all(string student_name[42], int r_identifier[42], double gpa_entry[42], int index);

//Calling the function
print_all(student_name, r_identifier, gpa_entry, index);


And I should remove the array declaration from main?
No, Keep the array declaration since you are trying to use the arrays. Changing your function to accept the arrays was correct
Though the [42] size is not necessary
Thanks, I have it as [42] since the program cannot accept more than 42 students as input.

So, the logic of this program with the revisions would be

1
2
3
//In main, declare arrays
//In function for inputting new student, calls functions for getting student info which stores in the arrays from main
//Arrays can now be accessed 


Is this correct?

Last edited on
I hope you're working with a much larger algorithm you've written, but yes.

You declare a function that accepts arrays.
You declare the arrays you wish to send to that function in main
You send the arrays by calling the function in main
You use the arrays inside the function

Note: You cannot return C style arrays from functions, You will need to return a pointer or use Dynamic Arrays
Yes I have a longer notepad file with my algorithm lol

How do I use a pointer? I am getting this compiler error, for example.

test.cpp: In function 'int main()':
test.cpp:33: error: invalid conversion from 'int*' to 'int'
test.cpp:33: error: invalid conversion from 'int*' to 'int'
test.cpp:10: error: too few arguments to function 'void input_new_student(sd::string*, int, double*, int, int*)'
test.cpp:33: error: at this point in file

Edit: Fixed the too few arguments error but idk about the other ones
Last edited on
http://www.cplusplus.com/doc/tutorial/pointers/

This covers pointers and dynamically allocated arrays.

I would work with passing an array to and from a function on a separate file so you can learn how before you attempt to apply it on a larger scale.

the error sounds like youre passing more than just the array name.

make sure you didnt write array[42] inside your function call. It only needs the name (array)
Last edited on
What is the difference between the (*) deference and (*) pointer operator use?

Also, I tried writing a small example for myself but even this won't compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int foo(int myArray[10]);
int main()
{
	int average;
	int myArray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	average = foo(myArray);
	cout << "The average of the array is " << average;
	cout << endl;
	return 0;
}
int foo( int * (myArray)[10])
{
	int avg;
	
	avg = myArray[0] + myArray[1] + myArray[2] + myArray[3] + myArray[4] + myArray[5] + myArray[6] + myArray[7] + myArray[8] + myArray[9] / 10;
	return avg;
}


Edit: Nevermind I got that program above working, still working on the main program
Last edited on
I've got my primary program compiling but it not does not respond to the initial menu choice of the user at all, it just prints the menu over and over.

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
using namespace std;
void print_menu();
void print_all();
void input_new_student(string student_name[42], int r_identifier[42], double gpa_entry[42], int index, int year_entry[42]);
int get_selection(int menu_Selection);
double get_gpa();
int get_year();
void print_by_year();
string get_name();
void print_statistics();

int main() //Handles the if statements concerning the menu of the program
{
	int r_identifier[42]; //Variable Declaration
	int year_entry[42];
	double gpa_entry[42];
	string student_name[42];
	int index;
	int menuchoice; //Variable Declaration
	do
	{
		print_menu(); //Calls function to print menu
		get_selection(menuchoice); //Calls function to get the menu selection
		if (menuchoice == 1) //Calls the function to input a new user
		{
			input_new_student(student_name, r_identifier, gpa_entry, index, year_entry);
		}
		else if (menuchoice == 2) //Prints all
		{
			print_all ();
		}
		else if (menuchoice == 3) //Prints statistics about all students in a particular year
		{
			int year_view;
			cout << "What year would you like to display?";
			cin >> year_view;
			while (year_view < 1972 || year_view > 2016) //Validates input
			{
				cout << "Error: Invalid input, please try again: ";
				cin >> year_view;
			}
			print_by_year();
		}
		else if (menuchoice == 4) //Prints statistics about all entered users
		{
			print_statistics();
		}
		else if (menuchoice == 5) //Quits the program
		{
			cout << "Have a good summer! ";
			cout << endl;
		}
	} while (menuchoice != 5);

	return 0;
}
void print_menu() //Prints Registrar menu
{
	cout << "RCNJ Registrar Menu: " << endl;
	cout << "\n[1] Add a student" << endl;
	cout << "[2] Display all students" << endl;
	cout << "[3] Display by year" << endl;
	cout << "[4] Display statistics" << endl;
	cout << "[5] Quit" << endl;
}
void input_new_student(string student_name[42], int r_identifier[42], double gpa_entry[42], int index, int year_entry[42]) 
//Calls all necessary functions to input a new student
{
	get_name();
	get_year();
	get_gpa();
		student_name[index] = get_name(); //Sets student name entry to index allocation
		year_entry[index] = get_year(); //Sets year entry to index allocation
		gpa_entry[index] = get_gpa(); //Sets gpa entry to index allocation

}
int get_selection(int menu_Selection) //Gets the menu selection
{
	cout << "\nSelection: "; 
	cin >> menu_Selection;
	while (menu_Selection < 1 || menu_Selection > 5) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> menu_Selection;
	}
	return menu_Selection; //Returns the user inputted menu selection
}
double get_gpa() //Function to get the GPA 
{
	double gpa_entry;
	cout << "Please enter the GPA: ";
	cin >> gpa_entry;
	while (gpa_entry < 0.0 || gpa_entry > 4.0) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> gpa_entry;
	}
	return gpa_entry; //Returns the gpa entry to input new student
}
int get_year() //Function to get year
{
	int year_entry_date;
	cout << "Please enter the year: ";
	cin >> year_entry_date;
	while (year_entry_date < 1972 || year_entry_date > 2016) //Validates input
	{
		cout << "Error: Invalid input, please try again: ";
		cin >> year_entry_date;
	}
	return year_entry_date; //Returns year entry to input new student
}
string get_name() //Function to get student name
{
	string student_name_entry;
	cout << "Please enter the student's name: ";
	cin >> student_name_entry;
	return student_name_entry; //Returns student name to input new student function
}
void print_by_year() //Function to print student information by year attended
{
	
}
void print_statistics() //Function which prints all statistics about students
{
	
}
void print_all()
{

}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/204040/

Don't double post and waste people's time by duplicating helper effort. Close multiple copies down.
Topic archived. No new replies allowed.