main
#include <stdio.h>
#include "Student.h"
#include <stdlib.h>
void main()
{
Student* pStudents = new Student[0];
int arraySize = 0;
int input = 0;
while(input != 3)
{
printf("Press 1 to add student, 2 to print and 3 to quit.\n");
scanf_s("%d", &input);
switch(input)
{
case 1:
pStudents[numOfStudents].AddStudent(pStudents, arraySize);
arraySize++;
numOfStudents++;
break;
case 2:
break;
case 3:
break;
default:
printf("Invalid input\n");
break;
}
}
}
Student.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student(void);
~Student(void);
void AddStudent(Student* pStudent, int arraySize);
private:
string name;
int ID;
};
Student.cpp
#include "Student.h"
Student::Student(void)
: name("N/A")
, ID(0)
{
}
Student::~Student(void)
{
}
void Student::AddStudent(Student* pStudent, int arraySize)
{
if(arraySize == 0)
{
delete[] pStudent;
pStudent = NULL;
pStudent = new Student[1];
}
else
{
Student* temp = new Student[(arraySize+1)];
for(int i = 0; i < arraySize; ++i)
{
temp[i] = pStudent[i];
}
delete[] pStudent;
pStudent = temp;
}
printf("Enter name of student.\n");
cin >> name;
printf("Enter ID of student. \n");
cin >> ID;
}
Okay I’ve been trying to figure this out but I can’t. I’m not sure what I’m doing wrong but I keep getting a write violation when I try to enter a name. I tried looking up the answer, some things I saw say try to use vectors, but I have not been taught how to use them yet.
Updated
main.cpp
void main()
{
Student* pStudents = new Student[100];
Student studentGenerator;
int numOfStudents = 0;
string name;
int ID;
int input = 0;
while(input != 3)
{
printf("Press 1 to add student, 2 to print and 3 to quit.\n");
scanf_s("%d", &input);
switch(input)
{
case 1:
printf("Enter name of student.\n");
cin >> name;
printf("Enter ID of student. \n");
cin >> ID;
studentGenerator = Student(name, ID);
pStudents[numOfStudents] = studentGenerator;
numOfStudents++;
break;
case 2:
for(int i = 0; i < numOfStudents; ++i)
{
studentGenerator.PrintStudents(pStudents[i]);
}
break;
case 3:
break;
default:
printf("Invalid input\n");
break;
}
}
}
student.cpp
#include "Student.h"
Student::Student(void)
: name("N/A")
, ID(0)
{
}
Student::~Student(void)
{
}
Student::Student(string studentName, int studentID)
{
name = studentName;
ID = studentID;
}
void Student::PrintStudents(Student student)
{
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
}
you initalized your array size to 0. You can not do this as the array size is static. You need to set to a larger number I would suggest somthing like at least 100 so it should read
Student* pStudents = new Student[100];
Also your student class could be improved
should create a constructor that takes in the student name and id and sets them to the class variables. Also you should not be concered about the array at all inside the student class. The array functions should be handled at the test class level or in main. The student name and id ashould also be asked for in the main or testing function.v
Also I better way to get data from the user would look like this:
follow up to your additional questions:
ok, I can tell your new at this you need to get a handle on opp in general. so to put this in simple term read your book or get a better on because you are totaly lost!
To help you out a little bit you want to do this : In main create a dummy var of type Student lets call it studentGenerator. From here you have two options:
option 1: ask the user to input the name and id into to seperate varaible. Then use these varabiles to generate the new varaible so
StudentGenerator = new Student(name,id)
option 2: Have new mutator in the student class and use the mutaros to set the data : this option is kind of crapy I would use option 1 instead!
so now that you have generated this new student put it into your array.ie StundentArray[counter] = StudentGenerator.
Now that this is done studentGenerator is garabage therefor you can reuse the command (in a loop) StudentGenerator = new Student(name,id) to add additonal students.