ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am working on some homework for c++ class. I am trying to pass the data from a file into an array. I am using g++ version 2.96.
When I try to compile it spits error about converting from string. Here is all below.
quote:
--------------------------------------------------------------------------------
void read_in (string &filename, string stud_id[], string fname[], string lname[], int score[], int &n) {
const int max_students = 100;
n=0;
quote:
--------------------------------------------------------------------------------
int numval, testscore;
string inputfilename, lname, sid, fname;
case 'A': case 'a': {
cout << "Please enter file to be read: ";
cin >> inputfilename;
read_in(inputfilename, sid, lname, fname, testscore, numval);
}
--------------------------------------------------------------------------------
Error:
quote:
--------------------------------------------------------------------------------
grades.c: In function `int main ()':
grades.c:164: cannot convert `string' to `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> > *' for
argument `2' to `read_in (string &, basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> > *,
basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> > *, basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> > *, int *,
int &)'
--------------------------------------------------------------------------------
I am obviously passing the strings by reference, as I thought I should. I did also think that string arrays always passed by ref without using &. What is wrong here?
btw. line 164 is this:
read_in(inputfilename, sid, lname, fname, testscore, numval);
The problem is that you have declared your read function to take arrays of strings, but you are passing just strings. Here is your code with the fix:
Code:
#include <stdlib.h>
#include <fstream.h>
#include <string>
void read_in ( const std::string filename,
std::string stud_id[],
std::string fname[],
std::string lname[],
int score[],
int& n)
{
const int max_students = 100;
n=0;
ifstream fin(filename.c_str());
if (!fin)
{
cout << "Bad file.\n";
exit(1);
}
while (fin >> stud_id[n] >> lname[n] >> fname[n] >> score[n])
{
n++;
}
} //end read_in
int main( int argc, char * argv[] )
{
int numval;
int testscore[10];
std::string inputfilename;
std::string lname[10];
std::string sid[10];
std::string fname[10];
cout << "Please enter file to be read: ";
cin >> inputfilename;
read_in(inputfilename, sid, lname, fname, testscore, numval);
for ( int i = 0; i < numval; i++ )
{
cout << "Sid: " << sid << endl;
cout << "lname: " << lname << endl;
cout << "fname: " << fname << endl;
cout << "testscore:" << testscore << endl << endl;
} // end for
} // end main
I don't know how far along you are in the class. but your program would be much better if you passed a vector of student classes or even a vector of structs instead of the sperate arrays. This way would be cleaner and would eliminate the static sized arrays that will cause problems. You should pass the max size of the declared arrys into the read method if you do use the arrays.
You could also return the number of items found rather then pass it as a parameter.
Good Luck
Gary
NOTE: The cout statements in the loop had i indexes but they will not display in this forum. They were being interpreted as start italic.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.