c++ - Memory Heap, Set up an array dynamically - c++

i have a problem, i think it has to do with heap memory.
would appreciate help.
this is in tribe header:
Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];
it's my problem.
when i write
Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];
i get this problem:
EXE3.exe has triggered a breakpoint.
this is take my in code to:
if (_crtheap == 0) {
#if !defined (_CRT_APP) || defined (_DEBUG)
_FF_MSGBANNER(); /* write run-time error banner */
_NMSG_WRITE(_RT_CRT_NOTINIT); /* write message */
#endif /* !defined (_CRT_APP) || defined (_DEBUG) */
__crtExitProcess(255); /* normally _exit(255) */
}
return HeapAlloc(_crtheap, 0, size ? size : 1);
}
but if i write:
Survivor* SurvivorsArray = new Survivor[100];
so it's work without error.
Currently it only works if I set up a preset array but if I want to get the array size from the user I get an error.
main:
#include <iostream>
#include <string>
using namespace std;
#include "survivor.h"
#include "tribe.h"
int main(){
Tribe t1;
Tribe t2;
char nameTribe1[20];
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;
t1.tribe(nameTribe1, maxTribe1);
char nameTribe2[20];
int maxTribe2;
cout << "Enter name of second tribe: " << endl;
cin >> nameTribe2;
cout << "Enter max of survivors to second tribe(max 100): " << endl;
cin >> maxTribe2;
t2.tribe(nameTribe2, maxTribe2);
return 0;
}
survivor headr:
class Survivor{
public:
char NameOfSurvivor[20];
int Age;
double StartWidgth;
double FinalWidgth;
void survivor(char name[20], int age, double sWidgth);
};
#endif
survivor cpp:
#include "survivor.h"
#include <iostream>
using namespace std;
void Survivor::survivor(char name[20], int age, double sWidgth){
for (int i = 0; i < 20; i++)
NameOfSurvivor[i] = name[i];
Age = age;
StartWidgth = sWidgth;
FinalWidgth = -1;
}//end survivor
tribe header:
class Tribe{
public:
char NameOfTribe[20];
int MaxSurvivorsInTribe;
**Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];**
int NumbersOfSurvivorsInTribe;
void tribe(char name[20], int maxSurvivor);
};
#endif
tribe cpp:
#include "tribe.h"
#include <string>
#include <iostream>
using namespace std;
void Tribe::tribe(char name[20], int maxSurvivor){
for (int i = 0; i < 20; i++)
NameOfTribe[i] = name[i];
for (int i = 0; i < maxSurvivor; i++){
for (int j = 0; j < 20; j++){
SurvivorsArray[i].NameOfSurvivor[j] = ' ';
}//end for
SurvivorsArray[i].Age = 0;
SurvivorsArray[i].StartWidgth = 0;
SurvivorsArray[i].FinalWidgth = 0;
MaxSurvivorsInTribe = maxSurvivor;
NumbersOfSurvivorsInTribe = 0;
}//end for
}//end tribe
thank's.

You can't perform dynamic allocations inside a class declaration. Move array allocation to to the Tribe constructor.
By the way, unless you're forbidden from using the STL for some reason, it will be much better to use a vector instead of an array.

MaxSurvivorsInTribe seems to be not initialized, when you allocate memory for array. It can have any integer value by default. You can use pointer as a field and initialize it in constructor:
class Tribe{
...
int MaxSurvivorsInTribe;
Survivor* SurvivorsArray;
Tribe(char* name, int maxSurvivor){
MaxSurvivorsInTribe = maxSurvivor;
Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];
....
}
And don't forget to free memory in destructor:
~Tribe(){
delete Survivor;
}
You need to reed more about classes in c++ and dynamic arrays.

Your constructors aren't formatted correctly, you put:
void tribe(char name[20], int maxSurvivor);
but it should be
Tribe(char name[20], int maxSurvivor);
Since it couldn't be initialized correctly, MaxSurvivorsInTribe wasn't set and couldn't be used.
Then in main, you'll initialize it like this:
char nameTribe1[20];
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;
Tribe t1(nameTribe1, maxTribe1);

Your code is failing because it is trying to call the new function to create the buffer with an undefined size. When you first declare your tribe variables like this:
Tribe t1;
Tribe t2;
You are triggering the implicit default constructor, which is going to attempt to create an object. The value you are using for the array size, MaxSurvivorsInTribe is a local field of the Tribe class that is not then initialized to any value.
You should probably do some code restructuring where you collect the size and name of each tribe before you instantiate your tribe objects, and then provide a constructor that accepts the max size and name values that also allocates the space needed for your array. Something like:
int main(){
char nameTribe1[20];
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;
char nameTribe2[20];
int maxTribe2;
cout << "Enter name of second tribe: " << endl;
cin >> nameTribe2;
cout << "Enter max of survivors to second tribe(max 100): " << endl;
cin >> maxTribe2;
Tribe t1(nameTribe1,maxTribe1);
Tribe t2(nameTribe2,maxTribe2);
}
And for tribe:
class Tribe{
public:
char NameOfTribe[20];
int MaxSurvivorsInTribe = 0;
Survivor* SurvivorsArray = nullptr;
int NumbersOfSurvivorsInTribe = 0;
Tribe(char name[20], int maxSurvivor) : MaxSurvivorsInTribe(maxSurvivor)
{
SurvivorArray = new Survivor[MaxSurvivorsInTribe ];
}
};
As others have noted, you would be even better served if you used a std::vector as your container and even a std::string to hold your name to avoid potential errors.
class Tribe{
public:
std::string NameOfTribe;
int MaxSurvivorsInTribe = 0;
std::vector<Survivor> SurvivorsArray;
int NumbersOfSurvivorsInTribe = 0;
Tribe(std::string name, int maxSurvivor) : MaxSurvivorsInTribe(maxSurvivor), NameOfTribe(name)
{
}
};

Related

accessing an array through pointer

I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
class subject{
public:
int numOfStudents;
string subjectId;
int * marksArray;
int * indexArray;
char * gradeArray;
int index ; // index for inserting data
subject(int students , string subjectName){
numOfStudents = students;
subjectId = subjectName;
this->index =0 ;
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;
}
void insert(int studentId , int mark , char grade){
indexArray[index] = studentId;
marksArray[index] = mark;
gradeArray[index] = grade;
this->index = this->index +1;
}
int getNumberOfEntries(){
return index ;
}
void printEntries(){
cout<< indexArray[0] << " O" << marksArray[0] << " " << gradeArray[0] << endl;
cout<< indexArray[1] << " OOO" << marksArray[1] << " " << gradeArray[1] << endl;
cout<< indexArray[2] << " OO" << marksArray[2] << " " << gradeArray[2] << endl;
}
};
int main(int argc, char const *argv[]){
subject S(10,"Mathematics");
cout<<S.subjectId<<endl;
cout<<S.numOfStudents<<endl;
S.insert(35,34,'A');
S.insert(33,34,'B');
S.insert(54,34,'C');
S.insert(21,34,'D');
S.insert(14,34,'F');
S.printEntries();
return 0;
}
output is :
Mathematics
10
35 O34 A
0 OOO0
As pointed out by #paddy in the comments to your question, your issue is within the constructor.
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;
What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"
Replacing the pointers with std::vectors and inserting you elements there will be must easier.
class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...
void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);
// No longer need to worry about index for next insert
}
};

Passing array of strings to a function

I am trying to create a program that uses class, arrays, and functions to show information about two students(Name, id#, classes registered). The part I am struggling with is passing arrays to a function. How do I do that?
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class Student // Student class declaration.
{
private:
string name;
int id;
string classes;
int arraySize;
public:
void setName(string n)
{
name = n;
}
void setId(int i)
{
id = i;
}
void setClasses(string c, int num)
{
classes = c;
arraySize = num;
}
string getName()
{
return name;
}
int getId()
{
return id;
}
void getClasses()
{
for (int counter=0; counter <arraySize; counter++) {
cout << classes[counter] << endl;
}
}
};
int main()
{
//Student 1
string s1Name = "John Doe";
int s1Id = 51090210;
int const NUMCLASSES1 = 3;
string s1Classes[NUMCLASSES1] = {"C++","Intro to Theatre","Stagecraft"};
//Student 2
string s2Name = "Rick Harambe Sanchez";
int s2Id = 666123420;
int const NUMCLASSES2 = 2;
string s2Classes[NUMCLASSES2] = {"Intro to Rocket Science","Intermediate Acting"};
//
Student info;
info.setName(s1Name);
info.setId(s1Id);
//info.setClasses(s1Classes, NUMCLASSES1);
cout << "Here is Student #1's information:\n";
cout << "Name: " << info.getName() << endl;
cout << "ID: " << info.getId() << endl;
//cout << "Classes: " << info.getClasses() << endl;
info.setName(s2Name);
info.setId(s2Id);
// info.setClasses(s2Classes, NUMCLASSES1);
cout << "\n\nHere is student #2's information:\n";
cout << "Name: " << info.getName() << endl;
cout << "ID: " << info.getId() << endl;
//cout << "Classes: " << info.getClasses() << endl;
return 0;
}
The usual way to pass around variable-length lists in C++ is to use an std::vector. A vector is a single object that you can easily pass to a function, copying (or referencing) its contents. If you are familiar with Java, it's basically an ArrayList. Here is an example:
#include <vector>
#include <string>
using namespace std;
class foo {
private:
vector<string> myStrings;
public:
void setMyStrings(vector<string> vec) {
myStrings = vec;
}
}
//...
foo myObj;
vector<string> list = {"foo","bar","baz"};
myObj.setMyStrings(list);
If don't want to use the standard library though, you can pass an array C-style. This involves passing a pointer to the first element of the array, and the length of the array. Example:
void processStrings(string* arr, int len) {
for (int i = 0; i < len; i++) {
string str = arr[i];
//...
}
}
string array[] = {"foo","bar","baz"};
processStrings(array, 3); // you could also replace 3 with sizeof(array)
Passing raw arrays like this, especially if you wanted to then copy the array into an object, can be painful. Raw arrays in C & C++ are just pointers to the first element of the list. Unlike in languages like Java and JavaScript, they don't keep track of their length, and you can't just assign one array to another. An std::vector encapsulates the concept of a "list of things" and is generally more intuitive to use for that purpose.
Life lesson: use std::vector.
EDIT: See #nathanesau's answer for an example of using constructors to initialize objects more cleanly. (But don't copy-paste, write it up yourself! You'll learn a lot faster that way.)
You can pass array of any_data_type to function like this
void foo(data_type arr[]);
foo(arr); // If you just want to use the value of array
foo(&arr); // If you want to alter the value of array.
Use std::vector. Also, don't add functions you don't need. Here's an example of using std::vector
#include <string>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
class Student // Student class declaration.
{
private:
vector<string> classes;
string name;
int id;
public:
Student (const vector<string> &classesUse, string nameUse, int idUse) :
classes (classesUse),
name (nameUse),
id (idUse)
{
}
void print ()
{
std::cout << "Name: " << name << std::endl;
std::cout << "Id: " << id << std::endl;
std::cout << "Classes: ";
for (int i = 0; i < classes.size (); i++)
{
if (i < classes.size () - 1)
{
std::cout << classes[i] << ", ";
}
else
{
std::cout << classes[i] << std::endl;
}
}
std::cout << std::endl;
}
};
int main()
{
Student John ({"C++","Intro to Theatre","Stagecraft"},
"John",
51090210);
John.print ();
Student Rick ({"Intro to Rocket Science","Intermediate Acting"},
"Rick",
666123420);
Rick.print ();
return 0;
}
Name: John
Id: 51090210
Classes: C++, Intro to Theatre, Stagecraft
Name: Rick
Id: 666123420
Classes: Intro to Rocket Science, Intermediate Acting
In the private variables of class Student, you are storing a string:
String classes;
where as you should be storing an array of strings like:
String classes[MAX_NUM_CLASSES];
then in the set classes function, pass in an array of strings as the first argument, so it should be :
void setClasses(string[] c, int num)
{
classes = c; //not sure if simply setting them equal will work, rather copy entire array using a for loop
arraySize = num;
}
This should point you in the right direction
Also, use std::vector instead of string[], it will be easier.

Text game opens in C++ opens twice

So, I'm making a text game. It opens and works fine then will abruptly close with no apparent trigger and open a new instance of itself. After it does this once it does not do it again. If I'm doing other things stupidly I would also like to know this. Heads up that this is a lot of code because I do not know where the problem lies. I tried to cut out the non important bits though. Would also like to know how to read write files in another directory that isn't the root one the exe is in. I am using Code::Blocks to compile. If the header files are important I can include them.
//#libraries
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "windows.h"
#include "math.h"
#include "time.h"
#include "Dungeon.h"
using namespace std;
//functions
int roomLogic();
void stats();
void options();
void devConsole();
void tutorial();
//Choose Class functions
void chooseClass();
void choosePaladin();
void chooseWarlock();
void chooseRanger();
void chooseWarrior();
void chooseRogue();
//global variables
int textSpeed;
int turns = 0;
bool debug = false;
//Player Stats
//Name and Such
int kills = 0;
int killScore = 0;
int damageDelt = 0;
int damageTaken = 0;
int score = 0;
string name;
string Class;
string race;
//Items
int keys = 0;
int money = 10;
int moneySpent = 0;
//Combat
int att = 10;
int def = 10;
int speed = 10;
int sneak = 10;
int eva = 10;
int perc = 10;
int accuracy = 10;
int health = 100;
int maxHealth = 150;
int mp = 50;
int magicPower = 10;
int main()
{
cout << "WHOLE BUNCH OF INTRO TEXT THAT YOU DO NOT NEED TO READ BECAUSE
THERE IS A LOT" << endl;
Sleep(1000);
//Text Speed Definition
cout << "How do you like your text speed? 'Slow', or 'Fast'?" << endl;
cout << "Type your answer and press ENTER to send" << endl;
string textSpeedInput;
cin >> textSpeedInput;
bool success = false;
//a while statement to get them to say yes no and set a text speed, not
important
cout << "If you have never played the game before and would like a
tutorial, type \n'tutorial' if not type 'no'" << endl;
string tutor;
cin >> tutor;
bool tut = false;
//another while statement thing that I cut out the function literally just
couts
chooseClass();
stats();
roomLogic();
system("PAUSE");
exit(0);
return 0;
}
void chooseClass()
{
cout << "Choice is fun, but randomness can be too, Would you like to
choose your class and race(Yes) or get a random class and race(No)" <<
endl;
string willChoose;
cin >> willChoose;
if(willChoose == "Yes" || willChoose == "yes")
{
cout << "Would you like to be a:\n" << endl;
cout << "Paladin (+3 Defense, -1 Speed)," << endl;
cout << "Warlock (+3 Magic Power, -1 Defense)," << endl;
cout << "Ranger (+2 Accuracy, +1 Perception, +1 Speed, -2 Defense)," <<
endl;
cout << "Warrior (+2 Attack, +1 Defense, -1 Magic Power), or" << endl;
cout << "Rogue (+2 Stealth, +1 Attack, +1 Evasion, +1 Speed, -3
Defense)?" << endl;
bool hasChoosen = false;
cin >> willChoose;
//another cut out while statement
code has broken by this point. I have no clue why. Please help is it an augment I need in my int main or something?
Here is an example read write I use just so you know:
ifstream x_file (".filenamehere.txt"); x_file >> variable to change;
x_file.close();
And to write:
ofstream y_file (".filename.txt"); y_file << thing to write; y_file.close();

How to solve the error: ISO C++ forbids variable-size array

I keep getting an error that says ISO C++ forbids variable-size array.
I am suppose to have an output that displays
Level Score Stars
----------------------------------
1 3840 ***
and so on....
Here is my program
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>
using namespace std;
int buildArrays(int A [], int B [], int C [])
{
int i = 0, num;
ifstream inFile;
inFile.open("candycrush.txt");
if (inFile.fail())
{
cout << "The candycrush.txt input file did not open" << endl;
exit(-1);
}
while (inFile)
{
inFile >> num;
A[i] = num;
inFile >> num;
B[i] = num;
inFile >> num;
C[i] = num;
i++;
}
inFile.close();
return i;
}
void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
cout << endl;
cout << reportTitle << endl;
cout << "Levels\tScores\tStars" << endl;
cout << "---------------------" << endl;
for (int i = 0; i < numberOfLevels; i++)
{
cout << levelsArray[i] << "\t" << scoresArray[i] << "\t";
for (int j = 0; j < starsArray[i]; j++)
{
cout << "*";
}
cout << endl;
}
}
void sortArrays(int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
{
for (int i = 0; i < numberOfLevels; i++)
{
for (int j = 0; j < numberOfLevels; j++)
{
if (levelsArray[i] < levelsArray[j])
{
int temp1 = levelsArray[i];
int temp2 = scoresArray[i];
int temp3 = starsArray[i];
levelsArray[i] = levelsArray[j];
scoresArray[i] = scoresArray[j];
starsArray[i] = starsArray[j];
levelsArray[j] = temp1;
scoresArray[j] = temp2;
starsArray[j] = temp3;
}
}
}
}
int main()
{
int MAX = 400; (This is where I am getting my valid array size error)
int levelsArray[MAX];
int scoresArray[MAX];
int starsArray[MAX];
int numberOfLevels = buildArrays(levelsArray, scoresArray, starsArray);
printArrays("Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);
sortArrays(levelsArray, scoresArray, starsArray, numberOfLevels);
printArrays("Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels);
system("pause");
}
Unless you (or your teacher, if you're doing this as homework) are intent on doing this badly, you should not simply convert MAX to a const.
Instead you should use std::vector instead of using arrays at all.
As long as you're at it:
Create a struct to hold the three parts of a single score.
Use std::sort instead of your own sorting function.
Overload operator>> and operator<< to do I/O on score objects.
Prefer one-step initialization over default-construction followed by the real initialization (e.g., creating, then separately opening a stream as well as creating then separately filling the vector).
Don't ever use while (stream) read_data1. Always test the result of reading the data, and react to that result.
Using those, we end up with code something like this:
struct score {
int level;
int score;
int stars;
bool operator<(score const &other) const {
return level < other.level;
}
friend std::istream &operator>>(std::istream &is, score &s) {
return is >> s.level >> s.score >> s.stars;
}
friend std::ostream &operator<<(std::ostream &os, score const &s) {
return os << s.level << "\t"
<< s.score << "\t"
<< std::string(s.stars, '*');
}
};
int main() {
std::ifstream in("candycrush.txt");
std::vector<score> scores{std::istream_iterator<score>(in),
std::istream_iterator<score>()};
std::cout << "Unsorted:\n";
for (auto const &s : scores)
std::cout << s << "\n";
std::cout << "\n";
std::sort(scores.begin(), scores.end());
std::cout << "Sorted:\n";
for (auto const &s : scores)
std::cout << s << "\n";
}
You should probably also add something to deal with two scores with equal levels (e.g., by comparing scores in that case), but that's probably a subject for another answer/diatribe.
1. ...or while (stream.good()) or while (!stream.eof()).
gcc support variable size arrays, but other compilers do not. Try.
int main()
{
#define MAX 400 /* use macro instead of stack variable */
int levelsArray[MAX];
int scoresArray[MAX];
int starsArray[MAX];
/* rest of your code */
return 0;
}
The array size should be a compile time constant in C++ for most compilers. Try
#define MAX 400;
...
int levelsArray[MAX];
or
const int MAX=400;
...
int levelArray[MAX];
You need to make the MAX variable to const variable.
Try this:
const int MAX=400;
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<fstream>
#define MAX 400 //<- Try this
using namespace std;
I would also recommend using classes when dealing with multiple arrays.
With the use of a class, there will be no need for you to pass multiple arrays to a function and set the parameter of the function with that long list of arrays. Such as this one:
void printArrays(string reportTitle, int levelsArray [], int scoresArray [], int starsArray [], int numberOfLevels)
Follow the vector usage instead (when variable sized array required for your purposes): https://stackoverflow.com/a/49021923/4361073

C++ simple class declaration program

I got a program to create in C++ in our introduction to C++ class in school. I am doing everything as I got in examples, but still getting errors.
w4x.cpp was given and I have to create Molecule.h and Molecule.cpp. I did that, but I am getting errors because my variables were not declared in scope, but I can't understand why.
// w4x.cpp
#include <iostream>
using namespace std;
#include "w4x.h"
#include "Molecule.h"
int main() {
int n = MAX_MOLECULES;
Molecule molecule[MAX_MOLECULES];
cout << "Molecular Information\n";
cout << "=====================" << endl;
for (int i = 0; i < MAX_MOLECULES; i++) {
if (!molecule[i].read()) {
n = i;
i = MAX_MOLECULES;
}
cout << endl;
}
cout << "Structure Name Mass\n";
cout << "==================================================" << endl;
for (int i = 0; i < n; i++)
molecule[i].display();
}
//Molecule.h
const int MAX_STRUCT = 10;
const int MAX_NAME = 20;
class Molecule {
char name[MAX_STRUCT];
char rate[MAX_NAME];
double weight;
public:
Molecule();
void read(const char*, const char*, double);
void display() const;
~Molecule();
};
//Molecule.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Molecule.h"
Molecule::Molecule(){
name[0]= '\0';
rate[0]= '\0';
weight = 0;
}
void::read(const char* n, const char* r, double w) {
weight = w;
strncpy (name, n, MAX_STRUCT);
name[MAX_STRUCT]='\0';
strncpy (rate, r, MAX_NAME);
rate[MAX_NAME]='\0';
cout << "Enter structure : ";
cin.getline (n, MAX_CHARS);
cout << "Enter full name : ";
cin.getline (r, MAX_NAME);
cout << "Enter weight : ";
cin >> w;
}
void::display() const
{
int x;
for ( x=0; x<i; x++)
cout << n << " " << r << " " << w << endl;
}
My first question is, how can I pass char name[MAX_STRUCT]; char rate[MAX_NAME]; double weight; from Molecule.h to Molecule.cpp
The problem with your definitions is here:
void::read(const char* n, const char* r, double w)
and here
void::display() const
What :: says here, is that you are implementing a function within a class. So you need to specify which class and which function! What you are telling it now, is that you are implementing a function inside class void, which is nonexistent.
You should convert them to:
void Molecule::read(const char* n, const char* r, double w)
void Molecule::display() const
Your other question regarding passing class members:
The functions of a class have access to its variables, therefore, you don't need to concern yourself with that. Just use the variables.
Also, if you notice in your w4x.cpp, the function Molecule::read() is called without parameters, so your TAs ask you to implement it without parameters. Indeed, since you have access to Molecule::name, Molecule::rate and Molecule::weight directly, you should read data and write to those variables instead of asking for parameters. Therefore, your read function would look like this:
void Molecule::read()
{
// read into name, rate and weight
}
Furthermore, w4x.cpp expects read to report whether it has been successful or not. This means that you should do error checking in Molecule::read and return 0 if no errors or -1 (for example) in case of errors.