MS Visual C++ 6.0 getline function

Sharky Forums


Results 1 to 4 of 4

Thread: MS Visual C++ 6.0 getline function

  1. #1
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    MS Visual C++ 6.0 getline function

    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;
    }
    Last edited by f15e; 09-16-2002 at 08:26 PM.

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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.
    Last edited by dighn; 09-16-2002 at 11:58 PM.
    .

  3. #3
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114
    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!!
    Last edited by f15e; 09-17-2002 at 09:46 AM.

  4. #4
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    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
    .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •