Click to See Complete Forum and Search --> : MS Visual C++ 6.0 getline function


f15e
09-16-2002, 09:22 PM
I get this error each time I use the getline function. I am using MS Visual C++ 6.0.

error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int,char)' : cann
ot convert parameter 1 from 'char' to 'char *'

Here is a bit of my code. Can you tell me why I am getting these errors? Thanks for the help!!

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

using namespace std;

struct BookItems
{
char bk_Isbn;
char bk_Title;
char bk_Author;
char bk_Publisher;
int bk_Pub_Yr;
double bk_Price;
int bk_Num_In_Stock;
};

const int NUM_BOOKS = 50;
const int MAX_ISBN_NUMS = 15;
const int MAX_TITLE_CHARS = 20;
const int MAX_AUTHOR_CHARS = 15;
const int MAX_PUBLISHER_CHARS = 20;

int main()
{
ifstream inFile;
int counter = 0;
char isbn[MAX_ISBN_NUMS];
char title[MAX_TITLE_CHARS];
char author[MAX_AUTHOR_CHARS];
char publisher[MAX_PUBLISHER_CHARS];
BookItems bookInfo[NUM_BOOKS] = {' '};

inFile.open("books.txt");

inFile.getline(bookInfo[0].bk_Isbn, MAX_ISBN_NUMS, ',');
for(int i = 0; i < 50 && inFile; i++)
{
inFile.getline(bookInfo[i].bk_Title, MAX_TITLE_CHARS, ',');
inFile.getline(bookInfo[i].bk_Author, MAX_AUTHOR_CHARS, ',');
inFile.getline(bookInfo[i].bk_Publisher, MAX_PUBLISHER_CHARS, ',');
inFile >> bookInfo[i].bk_Pub_Yr >> bookInfo[i].bk_Price
>> bookInfo[i].bk_Num_In_Stock;
inFile.getline(bookInfo[i+1].bk_Isbn, MAX_ISBN_NUMS, ',');

cout << bookInfo[i].bk_Title << bookInfo[i].bk_Author << bookInfo[i].bk_Publisher
<< bookInfo[i].bk_Pub_Yr << bookInfo[i].bk_Price << bookInfo[i].bk_Num_In_Stock
<< bookInfo[i+1].bk_Isbn;

counter++;
}

inFile.close();

return 0;
}

dighn
09-17-2002, 12:57 AM
the bk_*** in your struct are characters, not strings. you need to make them char arrays like you did for the local variables in your main()

btw you need to make the array 1 larger than the maximum # of characters to compenstate for the null terminator.

f15e
09-17-2002, 10:45 AM
So should I do this instead?

struct BookItems
{
char bk_Isbn[15];
char bk_Title[20];
etc....
};

and if I do this do I need the constants:

const int MAX_ISBN_NUMS = 15;
const int MAX_TITLE_CHARS = 20;
const int MAX_AUTHOR_CHARS = 15;
const int MAX_PUBLISHER_CHARS = 20;
b/c there in the struct.

and if I don't need them, what do I put for example:

inFile.getline(bookInfo[i].bk_Title, MAX_TITLE_CHARS, ',');

in place of MAX_TITLE_CHARS or can it be left out. I am not familiar with structs and the getline function to much. Dabbled with it once or twice, but not in this sense.
Thanks for your help!!

dighn
09-17-2002, 09:06 PM
that should work.

the const stuff is nto required, but is preferred so if you want to change your code, changes can be made easily. and it also makes the code more readable. but no you don't need them for anything