Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:55:50+00:00 2026-06-09T19:55:50+00:00

main #include <stdio.h> #include Student.h #include <stdlib.h> void main() { Student* pStudents = new

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:55:52+00:00Added an answer on June 9, 2026 at 7:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> void main(){ int x,y,z; x=y=z=1; z=++x||++y&&++z; printf(%d %d %d \n,x,y,z); getch(); } the
#include stdio.h #include conio.h int main(void) { if(printf(ABC)) { } else { printf(XYZ); }
My program is like this ( main.c ): #include <stdlib.h> #include <stdio.h> void main(){
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())
#include stdio.h #include conio.h #include <iostream> using namespace std; int main (void) { char
#include stdio.h int main (void) { char xx[1000] = hello; sprintf (xx, xyzzy plugh
#include stdio.h void Square(int num, int *myPointer); int main(int argc, const char *argv[]) {
could someone explain this phenomenon. #include stdio.h #include stdlib.h int main() { char foo[]=foo;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.