Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

class studentinfo{

public:
	struct student{
		int sID;
		string name;
		int g1,g2,g3;
		float t;
	}s[4];

void intro(){
	cout << "Input 5 Student Record\n";
	
	for(int i=0;i<=4;i++){
	cout << "Enter student ID number: ";
		cin  >> s[i].sID;
	cout << "\nEnter student name: ";
		cin.ignore(1,'n');
		getline(cin,s[i].name);
	cout << "\nEnter grades in English, Math, Science:\n";
		cin >> s[i].g1 >> s[i].g2 >> s[i].g3;
	
	s[i].t = (s[i].g1+s[i].g2+s[i].g3)/3;
	}
}

void showresult(){
	for(int i=0;i<=4;i++){
	cout << "Student ID number: " << s[i].sID <<
	"\nStudent name: " << s[i].name << 
	"\nEnglish: " << s[i].g1 <<
	"\nMath: " << s[i].g2 <<
	"\nScience: " << s[i].g3 <<
	"\nAverage: " << s[i].t << "\n\n";
}
};};


int main(){
	studentinfo num1;
	num1.intro();
	num1.showresult();
	
	cout << "program ended.\n\n";
	return 0;
}


What I have tried:

I have searched google for the error but I get no solution with it.
Posted
Updated 17-Nov-22 14:38pm
v2
Comments
CHill60 17-Nov-22 8:43am    
That error usually means an access violation i.e. that you are attempting to assign values to a memory location that doesn't exist. Often this means that you have not declared arrays with the correct dimensions. I don't get the error when I run your code but I'm using an on-line compiler.
Use the debugger to check your variables as you run the program.
MrMonsieur 17-Nov-22 8:56am    
may I ask what online IDE compiler do you use? I have just tried it now using onlinegdb it had an error as well during 5th student records name..
CHill60 17-Nov-22 9:26am    
I was using https://www.mycompiler.io/online-c++-compiler[^] - but I've now realised why no error - my inputs were wrong. My bad and my apologies.
I also noticed the [4] instead of [5] but got my languages mixed up and thought it was ok (highest index rather than number of elements). I'm not doing so well at this today!!
Member 15627495 17-Nov-22 8:51am    
int g1=0,g2=0,g3=0;
if you don't init with a value, random value could happen
CHill60 17-Nov-22 9:24am    
Except those values are being input explicitly
cout << "\nEnter grades in English, Math, Science:\n";
		cin >> s[i].g1 >> s[i].g2 >> s[i].g3;

You are trying to access the fifth item of a four-items array.
replace
Quote:
struct student{
int sID;
string name;
int g1,g2,g3;
float t;
}s[4];

with
struct student{
    int sID;
    string name;
    int g1,g2,g3;
    float t;
}s[5];
 
Share this answer
 
v2
Comments
MrMonsieur 17-Nov-22 9:05am    
It worked thanks I may have confused the number of declaration of array with the counting of its elements eg. s[4] = 0, 1, 2, 3, 4 = 5 elements
CPallini 17-Nov-22 9:10am    
You are welcome.
CPalini has already written a solution for exactly 5 students.
But there would be much better approaches that would not have this problem in the first place and would also be much more flexible. Data in a class should better not be public, but rather private. The name of the member function intro() doesn't seem to fit well either. More suitable would be getdata(). In order to be able to use the data at any time, a constructor would be recommended, which takes over this. One possibility would be to manage only the (private) data of ONE student in the class studentinfo.

C++
class studentinfo {
	int sID;
	string name;
	int g1, g2, g3;
	float t;
public:
	studentinfo();
	bool getdata();
	void showresult();
};

studentinfo::studentinfo():
sID(0), g1(0), g2(0), g3(0), t(0.0){};

const int MAX_INFO = 5;

int main() {
	studentinfo num[MAX_INFO];

	cout << "Input 5 Student Record\n";
	for (int i = 0; i < MAX_INFO; i++) {
		num[i].getdata();
		num[i].showresult();
	}

	cout << "program ended.\n\n";
	return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900