Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read student data from a struct-data.txt file and print out data. I have to complete the definition of my student class by adding fields relating to the individual majors as well as a print method, which I have. I have to print the major as a string which I'm trying to do. I cannot make any changes to the code except for the lines where it has my class, which I've been getting errors for.
The class is student
Public; I placed char*name; which is the names of the students and major_type major; which represents the major types in this case which are EE, CMPE, CSE and BME. I made it public since I know it has to correspond to a function.
I made private the string name since it's a data type.
I just receive errors after my void print_student line that says
ISO C++ forbids converting a string constant to ‘char*’ 
.
printz_students.cpp:21:103: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
   21 | { char *major[] = { "Electrical Engineering", "Computer Engineering", "Computer Science Engineering", "Biomedical Engineering" }; //To Print Major As a String


Along with
‘struct student’ has no member named ‘print’
   63 |   students[i].print();

error: ‘string’ does not name a type; did you mean ‘stdin’?
  18 |


This is in C++. Any pointers will be appreciated. Thanks.

What I have tried:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

	typedef enum
	{
		EE, CMPE, CSE, BME
	}

major_type;
using namespace std;
class student
{	public:
	char *name;
	major_type major;
	
	private:
    string name;
};
void print_student(struct student *sp)
{	char *major[] = { "Electrical Engineering", "Computer Engineering", "Computer Science Engineering", "Biomedical Engineering" };	//To Print Major As a String
	printf("%s- %s\n-", sp->name, major[sp->major]);
	printf("\n");
}

int main(int argc, char **argv)
{
	if (argc != 2)
	{
		printf("%s filename\n", argv[0]);
		exit(0);
	}

	FILE *fp = fopen(argv[1], "r");
	if (fp == NULL)
	{
		perror("Could not open file");
		exit(0);
	}

	int num_students = 0;
	char str[11];
	int major;
	student *students = (student*) calloc(10, sizeof(student));
	while (fscanf(fp, "%10s %d\n", str, &major) != EOF)
	{
		student *sp = &students[num_students];
		sp->name = str;
		if (major == 0) sp->major = EE;
		else if (major == 1) sp->major = CMPE;
		else if (major == 2) sp->major = CSE;
		else if (major == 3) sp->major = BME;
		num_students++;
		if ((num_students % 10) == 0)
		{
			students = (student*)
			realloc(students, (num_students + 10) *sizeof(student));
		}
	}

	for (int i = 0; i < num_students; i++)
	{
		students[i].print();
	}
}
Posted
Updated 2-Apr-21 22:12pm

There are several issues with your code, as posted, but to answer the question you asked, you have:
C++
char *major[] = {
     "Electrical Engineering",
     "Computer Engineering", 
     "Computer Science Engineering", 
     "Biomedical Engineering" }; 
In C++ a string literal, e.g. "Electrical Engineering" is a constant, so has the type const char *. To get rid of the error message, use
C++
const char *major[] = {
     "Electrical Engineering",
     "Computer Engineering", 
     "Computer Science Engineering", 
     "Biomedical Engineering" }; 
As noted, there are several issues with your code, but to me, the biggest issue is that you seem to be trying to use a strange mixture of C and C++. If you're going to be using C++, then you should probably use <iostream> (e.g. std::cout, std::cin) for console I/O, <fstream> for reading and/or writing files, and use a std::vector<student> to maintain the list of students, so you don't have to realloc() (which you are using incorrectly), the list as it grows.

What's the problem with realloc()? realloc returns NULL if it can't find the memory you need, so code like
C++
array = (array_type *)realloc(array, newsize)
runs the risk of clobbering the good pointer to array that you did have, and replacing it with NULL. The block of memory you did have is now lost, as is all the data you stored there. The memory will stay "lost" until the program ends, when (usually), resources acquired by the program are released.
 
Share this answer
 
Quote:
This is in C++. Any pointers will be appreciated.

Apart from a using and a class statement it is all pure C. So remove the line
C++
using namespace std;

since you are not using any of the standard template libraries. And change the student class to a struct:
C++
struct student
{
	char *name;
	major_type major;
    string name;
};
 
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