0

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;
}
Bart
  • 19,692
  • 7
  • 68
  • 77
user1527216
  • 1,143
  • 2
  • 13
  • 18
  • I'm not sure what you are asking, but I don't see any object value assignment going on. Maybe it would be better if you say what the code is supposed to do and what actually happens. – juanchopanza Aug 13 '12 at 04:48
  • 1
    You've allocated zero elements in your array. Who told you you could just extend arrays on the fly like that? [Read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Benjamin Lindley Aug 13 '12 at 04:49
  • [About `void main`.](http://stroustrup.com/bs_faq2.html#void-main) – chris Aug 13 '12 at 05:26
  • Your code won't even compile, as there are undeclared variables (`numOfStudents` for example). – Some programmer dude Aug 13 '12 at 06:06

2 Answers2

1

Your design is seriously flawed.

The Student class manages the list of Students which is actually just an array in "main()" ? That makes no sense!

how about a StudentList to manage a collection (can be an array) of Student ?

To clarify your understanding, what do YOU think this is doing?:

Student* pStudents = new Student[0];

John3136
  • 28,809
  • 4
  • 51
  • 69
1

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:

cout << "Enter Students name-->";
getline(cin,studentName);

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.

Daniel Haro
  • 341
  • 1
  • 2
  • 11
  • This is helpful, but I'm not sure how to set name and id using constructor for each thing in the array. – user1527216 Aug 13 '12 at 05:16
  • ok this is easy all you need to do is assign the vars so example class student { Student(String Name, String sID) { studentName = Name; studentID = sID; } – Daniel Haro Aug 13 '12 at 05:55
  • I know that part, the one I'm wondering about is how do I use that so pStudent[1] will have the name John and pStudent[2] would be Kim for example. – user1527216 Aug 13 '12 at 06:11
  • Ok, I have added to the answer above – Daniel Haro Aug 13 '12 at 06:25
  • studentGenerator = new Student(name, id) does not work, but studentGenerator = Student(name, id) does. However, when I want to print my array of students, all students get the latest name and ID entered. – user1527216 Aug 14 '12 at 06:05
  • Ok, check your counters to make sure your using them correctly. It's been a while since I had to do this in C so not sure about the new command not working. Try tracing your code to see if the values in the student generator are being update at all? Also can you put your new code in your post so I can take a look at it? – Daniel Haro Aug 14 '12 at 19:38
  • Oooh there's my updated code but I think I figured out my problem. It's the way I call my print function – user1527216 Aug 15 '12 at 04:23
  • ok I see your problem,at least witjh your printing your using StudentGenerator to print. This var is only used to generate new variables. you want to print out your array. so it should read pStudents[i].PrintStudents(); – Daniel Haro Aug 15 '12 at 04:29