Results 1 to 7 of 7

Thread: C++ strings error

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    C++ strings error

    Im doing some string searching and I keep getting this error can some one help.

    ################################################## #################


    #include <fstream.h> //for IO
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <stdlib.h>

    int main()
    {
    //Declare our input file name
    char filename[] = "fingers.txt";
    char ch_check; //checks for eof
    string line;
    int index; //index of where "Name: " is found
    string INFO[50]; //our array for stroring the info
    string stringA = "Name: ";
    int i = 0; //index for number of entries in to our array

    ifstream INFILE;

    INFILE.open(filename);

    if(INFILE.fail())
    {
    cout << "Couldn't open the input file: " << filename <<endl;
    cout << "Please make sure that it exists and you have permissions to read it." <<endl;
    exit(1);

    }else{
    cout << "The file " << filename << " was opened successfully" <<endl;
    }

    /*
    while((ch_check = INFILE.peek()) != EOF)
    {
    INFILE.get(character); //input from our file stream
    cout << character;

    }
    */

    //We will get the name of each person. We look for the word "Login:", this line contains the user information that we want.

    int MAXCHARACTERS = 50;//the maximum number of characters to search in a line


    while((ch_check == INFILE.peek()) != EOF)
    {
    //Search for the word "Login"
    INFILE.getline(line,MAXCHARACTERS,'\n'); //search each line until you reach MAXCHARACTERS or reach a newline.

    index = line.find(stringA); //look for stringA in 'line'

    if(index != ios::npos) //if we find the string
    {
    INFO[i] = line.substr(index + 6, line.find("\n&quot);
    i += 1;
    }

    }


    for(int j = 0; j < 50; j++)
    {
    cout << INFO[j];
    }
    INFILE.close();

    return 0;

    }
    ################################################

    The error says

    no matching function for calll to 'ifstream::getline (string &,int &,ch &amp'

    Thanx,

  2. #2

    Re: C++ strings error

    INFILE.getline(line,MAXCHARACTERS,'\n'); //search each line until you reach MAXCHARACTERS or reach a newline.

    It seems to not like that line. I would check documentation on the getline function, and see exactly what params it needs.

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    Re: C++ strings error

    My book says this is the form.

    (requires fstream.h header)

    getline(string var, int n, '\n') - Extract characters from the input stream until either n-1 characters are read or a newline is encountered (terminates input w/ a '\0')


    I declared line

    string line;

    ive also tried

    char line[50]; //just to be sure

    still no go .

    I think Im decaring it wrong some how??? What's another way???

    thanx

  4. #4

    Re: C++ strings error

    Using char[50] worked for me.

    BTW: Why are you including both <string.h> and <string>? Is this program C or C++?
    If it's C++ you should include the new style headers without the .h and use std:: or 'using namespace std' for short examples.

    T.

    (which book by the way?)

  5. #5
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    Re: C++ strings error

    Hmm.
    Im using anjuta to compile and build. could that make a differenence?

    Yea I added that after getting desperate.

    let me try this again....

  6. #6

    Re: C++ strings error


    My book says this is the form.

    (requires fstream.h header)

    getline(string var, int n, '\n') - Extract characters from the input stream until either n-1 characters are read or a newline is encountered (terminates input w/ a '\0')


    I declared line

    string line;

    ive also tried

    char line[50]; //just to be sure

    still no go .

    I think Im decaring it wrong some how??? What's another way???

    thanx
    Book doesn't always match the actual headers. Especially if you are reading a C++ book geared towards Win32.

  7. #7

    Re: C++ strings error

    istream::getline takes a char string, not a C++ string!!!!

    If you want to do a getline on a C++ string, there's a different getline:
    Code:
    istream & getline(istream & in, string & s, char delimiter='\n'); //prototype
    
    //usage
    getline(INFILE, line);

Similar Threads

  1. Perl and Strings
    By paradox in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 08-04-2004, 08:28 PM
  2. strings command
    By Emooz in forum Linux - General Topics
    Replies: 1
    Last Post: 07-13-2004, 10:30 AM
  3. strings for window?
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 05-08-2004, 01:42 AM
  4. I need help with strings in C
    By in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 12-25-2001, 12:08 AM
  5. Intel HaM winmodem init strings etc
    By fearby in forum Linux - Hardware, Networking & Security
    Replies: 12
    Last Post: 12-22-2001, 10:03 AM

Bookmarks

Posting Permissions

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