I having a error with data structures - c++

I am having a error from the compiler telling me that "student" "does not refer to a value" it was to my understanding student.test refers to a value and that value has already be initialized to the variable. I need help
int main() {
ifstream dataIn;
ofstream dataOut;
struct student{
string lastName=" ";
string firstName=" ";
int test1=0;
int test2=0;
int test3=0;
int test4=0;
int test5=0;
};
char grade=' ';
int total=0;
int average =0;
average=averageScore(student.test1, student.test2, student.test3, student.test4,student.test5,student.average, student.total);

The problem is that struct student is a type definition and not a variable declaration.
Therefore, in order to access the fields you should declare a variable of this type, and use the variable and not the class i.e.:
student st;
char grade=' ';
int total=0;
int average =0;
average=averageScore(st.test1, st.test2, st.test3, st.test4,st.test5,st.average, st.total);
(And also, as Mr. Yellow mentioned the average field is not defined for struct student)

Related

Can I reference a non static member to a private string array in a class? C++

I'm trying to have three private data members (string firstName[], string lastName[], and float testScores[]) in a class where the number of elements is an input from a public data member, totalStudents (which is user defined). Further, adding const in front of totalStudents isn't an option because the value changes when the user inputs the value. Here is the header code (C++):
#ifndef __newClassHeader.h__
#define __newClassHeader.h__
#include <iostream>
#include <string>
using namespace std;
class student {
public:
student();
int totalStudents;
int askStud() {
cout << "Enter the number of students: ";
cin >> totalStudents;
}
private:
string FirstName[totalStudents]; // error: a nonstatic member reference must be relative to a specific object
string lastName[totalStudents]; // error: a nonstatic member reference must be relative to a specific object
};
#endif
I tried adding a totalStudentsPtr variable (that points to totalStudents) after the user info, but that spit out the same issue. For example, I tried this
#ifndef __newClassHeader.h__
#define __newClassHeader.h__
#include <iostream>
#include <string>
using namespace std;
class student {
public:
student();
int totalStudents;
int askStud() {
cout << "Enter the number of students: ";
cin >> totalStudents;
}
string firstName[totalStudents]; // error– a nonstatic member reference must be relative to a specific object
string LastName[totalStudents]; // error– a nonstatic member reference must be relative to a specific object
private:
string* FirstName;
string* lastName;
};
#endif
but it gives the same error code. Also, I tried using protected instead of private, but that also didn't work.
When you declare an array field as string firstName[totalStudents]; you are telling the compiler to allocate static (read stack) memory for an array with a number of elements that is not yet known (also dynamic). This is not how the compiler works.
You need to either use a value that is constant at compile time, or use std::vector:
class student {
public:
student();
int askStud() {
cout << "Enter the number of students: ";
cin >> totalStudents;
}
private:
vector<string> FirstName; // Dynamically sized!
vector<string> lastName; // Dynamically sized!
};
P.S. Try to avoid using using namespace std; in header files as it pollutes the global namespace.

Error while declaring and initializing variable of the struct type

I have problem while creating the variable of Employee(struct) user defined type.Error says that member of struct type is not allowed like that.
struct Employee{
char name[100];
char address[100];
}emp[3];
int main(){
char ch;
int i=-1;
fstream in;
in.open("Muhammad.txt",ios::out|ios::binary|ios::ate);
do{
i++;
cout<<"\nEnter name:";cin.getline(emp.name[i],100);//error line
cout<<"\nEnter address:";cin.getline(emp.address[i],100);//error line
in.write( reinterpret_cast<char*>(&emp[i]),sizeof(Employee));
}while(ch=='y'||ch=='Y');
system("PAUSE");
return 0;
}
expected that object of Employee will be created normally
but got following error:
[Error] request for member 'name' in 'emp', which is of non-class type 'Employee [3]'
You declared an array of three elements of the type struct Employee.
This construction
struct Employee{
char name[100];
char address[100];
}emp[3];
is equivalent to
struct Employee{
char name[100];
char address[100];
};
Employee emp[3];
And each element of the array has only one data member name and address.
So you have to write for example
getline(emp[i].name,100)
And the loop shall have no more than three iterations.
Moreover the variable ch was not initialized and is not changed in the loop.

How to initialize a structure variable to default values?

I am learning about structures now and I am trying to create default values for each variable of that structure type. My testing code is as follows:
#include<iostream>
using namespace std;
int main (void){
// DECLARING THE STRUCTURE
struct str_client{
char name[20] = "\0";
int age = 0;
double money = 0.00;
};
// DECLARING A VARIABLE OF THAT STRUCTURE TYPE (IN THIS CASE, AN ARRAY).
str_client client[3];
return (0);}
Is this initialization the correct way to do it?
Use constructor.
Since class and struct are similar.
struct str_client{
string name;
int age;
double money;
str_client()
{
name = "";
age = 0;
money = 0.0;
}
};
Edit
Using Member Initializer list will improve performance
struct str_client{
string name;
int age;
double money;
str_client()
: name(""), age(0), money(0.0)
{
}
};

Adding a class to a map

I am trying to add a class object to a map, this is what I have:
#include<vector>
#include<map>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
class Student{
int PID;
string name;
int academicYear;
public:
Student(int, string, int);
};
Student::Student (int P, string n, int a) {
PID = P;
name = n;
academicYear = a;
}
void createStudent(map<string, Student>);
int main(int argc, char** argv){
map <string, Student> studentList;
createStudent(studentList);
}
void createStudent(map<string, Student> studentList){
int PID;
string name;
int academicYear;
cout << "Add new student-\nName: ";
getline(cin, name);
cout << "PID: ";
cin >> PID;
cout << "Academic year: ";
cin >> academicYear;
Student newstud (PID, name, academicYear);
studentList[name] = newstud; //this line causes the error:
//no matching function for call to
//'Student::Student()'
}
I don't understand why the constructor function is being called there, I thought newstud would already have been constructed from the previous line. Can anyone explain what is happening when I try to add newstud to the map?
Problem 1st
std::map::operator[] will insert new element into container if its not present, using default constructor, which in your case is not present and will probably doesn't make sense even if you provide one.
So use std::map::insert for such scenario
Problem 2nd
Even if you successfully insert using studentList.insert(std::make_pair(name, newstud)); its not going to reflect changes in original map studentList in main ( ) unless you use a reference type, in function definition & declaration of createStudent
So use
void createStudent(map<string, Student>& );
In order to add entries using the function, you should make your parameter studentList pass by reference.
And instead of
studentList[name] = newstud;
use:
studentList.insert(std::make_pair(name, newstud));
Just a suggestion, though.

Error with Converting Struct

I am trying to read in a huge data set of lastnames firstnames and ssn's. When I try to point to my struct it says it is trying to convert my single_info struct from individualf to singleinfo ...
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct single_info {
string firstnames[6000];
string lastnames[6000];
string socialsecurity[6000];
double gpa;
};
void ScanInFile(istream &, struct single_info, int *total);
void Outputfile(ostream &, struct single_info, int *total);
Here is my struct
Here are my functions and for some reason I get error: could not convert ‘& individualf’ from ‘single_info*’ to ‘single_info’
void ScanInFile(istream &inputfile, struct single_info *individualf, int *total)
{
int i=0;
while(!inputfile.eof()){
inputfile >> individualf->socialsecurity[i];
inputfile >> individualf->firstnames[i];
inputfile >> individualf->lastnames[i];
i++
}
*total = i;
}
void Outputfile(ostream &outputfile, struct single_info *individualf, int *total)
{
for(int i=0; i < *total; i++){
outputfile << individualf->socialsecurity[i];
outputfile << individualf->firstnames[i];
outputfile << individual->lastnames[i];
}
outputfile << endl;
}
The problem is that the declarations of ScanInFile and Outputfile say that they take a struct single_info as their parameter type. It looks like you meant for that to be struct single_info* (which would match your definitions). Whatever code is attempting to call your functions can only see the declarations and is trying to pass a single_info* even though it asks for just a single_info.
Note that putting struct before a struct identifier is unnecessary in C++.
Yes it's the prototype. It needs to be defined exactly like the function itself.