Help with add function

Hello everyone, my professor gave us a project to do and I'm stuck on one of my functions

He gives us instructions on what to do it reads:

"If Add new class is selected, pass the array of pointers to an add() function, which dynamically allocates memory for a new Class, reads the class information (title, units and grade) from the user, saves its pointer in the array of pointers and displays the menu again."

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
 #include <iostream>
#include <conio.h>
using namespace std;

struct Class {
	char title[50];
	int units;
	char grade;
};
void menu();
void addClass(Class* ptr);

int main()
{
	char choice;
	Class* ptr = new Class[10];
	do
	{
		menu();
		cin >> choice;
		if (choice == '1')
			addClass(ptr);
		else if (choice == '2')
			cout << "choice works" << endl;
		else if (choice == '3')
			cout << "choice works" << endl;
		else if (choice == '4')
			cout << "choice works" << endl;
		else if (choice == '5')
			cout << "choice works" << endl;
		else if (choice == '6')
			cout << "choice works" << endl;
		else if (choice == '7')
		{
			cout << "quitting...." << endl;
			choice = 7;
		}
		else
			cout << "invalid input" << endl;

	} while (choice != 7);
}
void menu()
{
	cout << "1. Add new class." << endl;
	cout << "2. Edit an existing class" << endl;
	cout << "3. Display a class" << endl;
	cout << "4. List all classes " << endl;
	cout << "5. Display GPA" << endl;
	cout << "6. Delete all classes" << endl;
	cout << "7. Quit." << endl;
}
void addClass(Class* ptr)
{
	for (int i = 0; i < 1; i++)
	{
		cout << "Enter class name: ";
		cin.getline(ptr[i].title, 50);
		cout << "Enter number of units: ";
		cin >> ptr[i].units;
		cout << "Enter grade recieved: ";
		cin >> ptr[i].grade;
	}
}


Everytime I run it, it keeps in looping? Is it because I didn't dynamically allocate the array of pointers like he asked in the addClass function?
Last edited on
You don't need a for loop inside the function.
saves its pointer in the array of pointers and displays the menu again.

Instead of a void return, I think the return should be of Class*.
dynamically allocates memory for a new Class

You need to use the new operator, so you need to declare it: ptr = new Class inside the addClass function.
closed account (48T7M4Gy)
Just in case we get another OP "deletion_after_solution" anomaly
Hello everyone, my professor gave us a project to do and I'm stuck on one of my functions

He gives us instructions on what to do it reads:

"If Add new class is selected, pass the array of pointers to an add() function, which dynamically allocates memory for a new Class, reads the class information (title, units and grade) from the user, saves its pointer in the array of pointers and displays the menu again."

#include <iostream>
#include <conio.h>
using namespace std;

struct Class {
char title[50];
int units;
char grade;
};
void menu();
void addClass(Class* ptr);

int main()
{
char choice;
Class* ptr = new Class[10];
do
{
menu();
cin >> choice;
if (choice == '1')
addClass(ptr);
else if (choice == '2')
cout << "choice works" << endl;
else if (choice == '3')
cout << "choice works" << endl;
else if (choice == '4')
cout << "choice works" << endl;
else if (choice == '5')
cout << "choice works" << endl;
else if (choice == '6')
cout << "choice works" << endl;
else if (choice == '7')
{
cout << "quitting...." << endl;
choice = 7;
}
else
cout << "invalid input" << endl;

} while (choice != 7);
}
void menu()
{
cout << "1. Add new class." << endl;
cout << "2. Edit an existing class" << endl;
cout << "3. Display a class" << endl;
cout << "4. List all classes " << endl;
cout << "5. Display GPA" << endl;
cout << "6. Delete all classes" << endl;
cout << "7. Quit." << endl;
}
void addClass(Class* ptr)
{
for (int i = 0; i < 1; i++)
{
cout << "Enter class name: ";
cin.getline(ptr[i].title, 50);
cout << "Enter number of units: ";
cin >> ptr[i].units;
cout << "Enter grade recieved: ";
cin >> ptr[i].grade;
}
}
Everytime I run it, it keeps in looping? Is it because I didn't dynamically allocate the array of pointers like he asked in the addClass function?
Last edited on
Thanks kemort but how do we put a sustainable solution in place to this problem? Several such instances have been reported recently and prior to that but no checks have initiated. I'm not sure the moderators are considering this seriously or realize how frustrating it can be to regular users.
PS: OP - don't take this personally, I'm sure you're a thoroughly decent person who wouldn't dream of doing anything like this
Everytime I run it, it keeps in looping?

Instead of trying to assign a number (7) to choice you should just use a character constant in your while() statement ('7').


Note there are several other problems with this code that you'll need to solve but this should help solve the question you're asking about.
Topic archived. No new replies allowed.