Error with Converting Struct - c++

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.

Related

I am getting a " conversion from ‘std::string (*)[50]’ to non-scalar type ‘std::string’ requested" error and cant seem to fix it

Here is the beginning of a program I am writing. As you can see it is incomplete, but I was checking my program for bugs and I got this error. I looked it up and found solutions such as "do not include brackets while calling a multidim array" I corrected and got this error. Any advice on how to solve it?
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<fstream>
using namespace std;
void readEmployees();
void readPasswords();
void mixPasswords(string(*)[50], string[], string(*)[50]);
string employee[50][50];
string passwords[50];
string passwordsAssigned[50][50];
int main()
{
readEmployees();
readPasswords();
mixPasswords(employee, passwords, passwordsAssigned);
return 0;
}
void readEmployees()
{
int y;
ifstream fileInput;
fileInput.open("employees.txt");
for(int x = 0; x < 50; x++)
{
fileInput >> employee[x][y] >> employee[y][x];
y++;
}
}
void readPasswords()
{
ifstream fileInput;
fileInput.open("passwords.txt");
for(int x = 0; x < 50; x++)
{
fileInput >> passwords[x];
}
}
void mixPasswords(string employee(*)[50], string passwords[], string completed(*)[50])
{
}
Your declaration void mixPasswords(string, string, string); does not match the types of the parameters you are passing to it. You need to change your declaration to something like
void mixPasswords(string[][50], string[], string[][50]);
Additionally, the definition you have for mixPasswords does not define the previously declared function since its parameter list doesn't match the declaration. Instead it declares and defines a new, unused, overload of mixPasswords that takes a different set of parameters. You need to make your declaration and definition match.

error: '' was not declared in this scope

I was trying to do a simple program to learn a bit more about reference passage, pointers and how to use this 2 on structs in C++ and I got a few questions.
I got an error on the code below "error: 'totalStudent' was not declared in this scope" and my question is, how should I declare the "totalStudent".
#include <iostream>
using namespace std;
struct test{
char name[30];
int age;
};
void addStudent(struct test *ptrTest,int *totalStudent){
for(int i=0;i<2;i++){
cout<<"\nInsert the name: ";
cin.sync();
cin.getline(ptrTest->name,sizeof(ptrTest->name));
cout<<"\nInsert the age: ";
cin.sync();
cin>>ptrTest->age;
*totalStudent+=1;
}
}
void showStudent(struct test *ptrTest,int totalStudent){
for(int i=0;i<totalStudent;i++){
cout<<"\nName: "<<ptrTest->name;
cout<<"\nAge: "<<ptrTest->age;
}
};
int main()
{
struct test t;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
I can't use pointers and reference passages with structures very well. I can only use them when I'm not using structs.
You forgot to declare this variable in the scope of main:
int main()
{
struct test t;
// LIKE THIS
int totalStudent;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}

I having a error with data structures

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)

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.

Why my template function is not a valid match in this call?

i made the following code to convert number to string and reverse
in the commented part of code i want to make the function type a template
thinking that it will acquire the type according to context "e.g if i assign
it to int variable the function will be of type int " but this not occur and compiler
give error message
D:\computer science\project\stringToint.cpp In function 'int main()':
49 25 D:\computer science\project\stringToint.cpp [Error] no matching function for call to 'intstr(const char [10])'
49 25 D:\computer science\project\stringToint.cpp [Error] candidate is:
17 21 D:\computer science\project\stringToint.cpp template<class T> T intstr(std::string)
i think that their was error in using stringstream object
but i was successful in achieving the function to work if
i specify the type of function but this will make me write
different function for every type
is i miss understand some thing please help
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<typename T>
string strint (T oty)
{
string ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
/*
template<typename T>
T intstr (string oty)
{
T ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
*/
int intstr (string oty)
{
int ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
signed char charstr (string oty)
{
signed char ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
int main()
{
int i;
signed char c;
string s;
s=strint(123);
cout<<s<<endl;
i=intstr("123456789");
cout<<i<<endl;
c=charstr("2");
cout<<c;
return 0;
}
You should explicitly specify template parameter for function, since compiler can't deduce T, because there are no parameters of type T in function args. Like
intstr<int>("123456789");
thanks #ForEveR i now reduced my code to look like this and it is working well
i hope it is the best solution of converting number to string and vise verse
using stringstream
thanks alot
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<typename T1,typename T2>
T2 strint (T1 oty)
{
T2 ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
int main()
{
cout<< strint <string,int>("1234") <<endl;
cout<< strint <int,string>(456) <<endl;
cout<< strint <string,float>("3.14") <<endl;
cout<< strint <string,char>("3") <<endl;
return 0;
}