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."
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?
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