views:

151

answers:

5
+1  Q: 

C++ compile error

#include<string>
using namespace std;

int main(){
    const int SIZE=50;
    int count=0;
    ifstream fin("phoneData.txt");
    ofstream fout("phoneList.txt");
    string firstName, lastName, phoneNumber;
    if (!fin){
     cout<<"Error opening file. program ending."<<endl;
     return 0;
    }
    while (count<SIZE && fin>>phoneNumber[count]){
     fin.ignore();
     getline (fin, firstName[count], '\n');
     fin>>lastName[count];
     count++;
    }
    return 0;

Here is my code so far. in my while loop, something is wrong with the getline, i keep getting an error message like this:

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1>        c:\program files\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'

please help!!! i can't figure it out!

+1  A: 
getline (fin, firstName[count], '\n');

should be:

getline(fin, firstName);

There are more problems, too. Here's one possible clean-up that makes a few assumptions about your input data that I couldn't tell from your code:

#include <iostream>
#include <fstream>
#include <string>

int main(){
  using namespace std;
  ifstream fin("phoneData.txt");
  ofstream fout("phoneList.txt");
  if (!(fin && fout)){
    clog << "Error opening file. program ending.\n";
    return 1;
  }
  const int SIZE=50;
  string firstName, lastName, phoneNumber;
  for (int count = 0; count < SIZE; ++count) {
    getline(fin, phoneNumber, ' ');
    getline(fin, firstName, ' ');
    getline(fin, lastName);
    if (!fin) {
      break;
    }
    fout << lastName << ", " << firstName << " -- " << phoneNumber << '\n';
  }
  return 0;
}

Input sample:

123 Marcy Darcy
555-0701 Daneal S.

Output sample:

Darcy, Marcy -- 123
S., Daneal -- 555-0701
Roger Pate
A: 

http://www.cplusplus.com/reference/string/getline/

Here is the signature of getline istream& getline ( istream& is, string& str, char delim );

Just do getline (fin, firstName[count], '\n');

Note that the '\n' isn't mandatory. By default it gets the whole line.

Maybe you wanted to declare firstName & co as vectors ? std::vector firstName(SIZE);

On a string, the operator[] gets a char http://www.cplusplus.com/reference/string/string/operator%5B%5D/

So fin>>lastName[count] would just read one char into lastname.

Tristram Gräbener
You still have `[count]`, which was the main problem.
Roger Pate
doh... indeed... thanks
Tristram Gräbener
A: 

how about some *stream includes - just for hygiene even if string includes them for you

getline (fin, firstName); // should work

pm100
A: 

Neither firstName nor lastName are arrays, but you incorrectly use them both as array types.

Rob Pelletier
A: 

I think what you are looking for is

char firstName[1024]
fin.getline (firstName, 1024, '\n')
T.E.D.
No, istream::getline does not take a std::string.
Roger Pate
Hmmm. That's a good point. I'll change the code.
T.E.D.