W skrócie, mam klasę studentów, która przechowuje dane takie jak imię, nr albumu, średnia ocen, ilość przedmiotów oraz te przedmioty.
Ilość studentów wyznaczamy przy starcie programu (tworzymy tablice obiektów), następnie każdemu z obiektów (każdemu studentowi) po kolei wpisujemy parametry jak imię, nr albumy, średnia ocen. Dotąd jest ok.
Problem pojawia się gdy dla danego obiektu ustalam ilość przedmiotów, a następnym krokiem jest utworzenie tablicy typu string tych przedmiotów o zadanej wcześniej wielkości (ilość przedmiotów). Całkowicie się już w tym pogubiłem i nie mam pojęcia jak to rozwiązać.
Funkcja main.
Kod :
#include <iostream>
#include "student.h"
#include <string>
using namespace std;
int main(int)
{
int k = 0;
cout << "Ile studentow chcesz utworzyc?" << endl;
cin >> k;
student* newStudent;
newStudent = new student[k];
string newName;
int newNr;
float newGrade;
int newN;
string subject;
for(int i=0;i<k;i++)
{
cout << endl << "Podaj imie studenta nr " << (i+1) << endl;
cin >> newName;
newStudent[i].setName(newName);
cout << "Podaj numer albumu studenta nr " << (i+1) << endl;
cin >> newNr;
newStudent[i].setNr(newNr);
cout << "Podaj srednia ocen studenta nr " << (i+1) << endl;
cin >> newGrade;
newStudent[i].setGrade(newGrade);
cout << "Ile przedmiotow chcesz dodac studentowi nr " << (i+1) << endl;
cin >> newN;
newStudent[i].setN(newN);
for(int j=0; j<newN ;j++)
{
cout << "Jakie przedmioty chcesz dodac studentowi nr " << (i+1) << endl;
cin >> subject;
newStudent[i].setSubject(newN, subject);
}
}
for(int i=0;i<k;i++)
{
cout << endl <<"Imie studenta nr " << (i+1) << ": " << newStudent[i].getName() << endl;
cout << "Numer albumu studenta nr " << (i+1) << ": " << newStudent[i].getNr() << endl;;
cout << "Srednia ocen studenta nr " << (i+1) << ": " << newStudent[i].getGrade() << endl;;
}
system("Pause");
}
Kod :
#pragma once
#include <iostream>
#include <iomanip>
#include <ctype.h>
using namespace std;
class student
{
private:
string name;
int nr;
float grade;
string* subjects;
int N;
public:
student(){
}
~student(){
}
//metody pobierajace dane
void setName(string newName){
name = newName;
}
void setNr(int newNr){
nr = newNr;
}
void setGrade(float newGrade){
grade = newGrade;
}
void setN(int newN){
N = newN;
}
void setSubject(int newN, string &newSubject)
{
subjects[newN] = newSubject;
}
// metody wyswietlajace dane
string getName()
{
return name;
}
int getNr()
{
return nr;
}
float getGrade()
{
return grade;
}
int getN()
{
return N;
}
};

Jakieś pomysły? Ktokolwiek?
Zakładki