LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2001, 11:04 PM   #1
shaggy112
LQ Newbie
 
Registered: May 2001
Posts: 6

Rep: Reputation: 0

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;

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


--------------------------------------------------------------------------------

Excerpt from main:


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);

Thanks a lot!

 
Old 05-24-2001, 10:41 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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.

[Edited by crabboy on 05-24-2001 at 11:45 PM]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 05:28 AM
sending pointer array to function marek Programming 4 04-15-2004 04:46 PM
function won ' t return character array word Linh Programming 1 07-31-2003 06:11 PM
C Function List && Fortran OutToSea Programming 0 06-18-2003 10:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration