Thread: invalid conversion from `char*' to `char'

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    invalid conversion from `char*' to `char'

    Hello, I am new to C++, I just started yesterday. I am trying to make a fuction where I can return a line from a .txt with linesi(file.txt,line number).

    But I get the following errors, and being new, I have no idea why:

    C:\Documents and Settings\Family\Desktop\Untitled21.cpp In function `int main()':
    17 C:\Documents and Settings\Family\Desktop\Untitled21.cpp invalid conversion from `char*' to `char'
    17 C:\Documents and Settings\Family\Desktop\Untitled21.cpp initializing argument 1 of `int linesi(char, int)'
    C:\Documents and Settings\Family\Desktop\Untitled21.cpp In function `int linesi(char, int)':
    22 C:\Documents and Settings\Family\Desktop\Untitled21.cpp invalid conversion from `char' to `const char*'
    22 C:\Documents and Settings\Family\Desktop\Untitled21.cpp initializing argument 1 of `std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]'
    28 C:\Documents and Settings\Family\Desktop\Untitled21.cpp invalid conversion from `char*' to `int' 24 C:\Documents and Settings\Family\Desktop\Untitled21.cpp [Warning] address of local variable `buffer2' returned


    Just wish I knew what I was doing wrong. Here is my code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int linesi (char b, int c);
     
    int main ()
    {
       char b[256];
       int c;
       char buffer[256];
       cout << "Please enter the filename of a .txt file: ";
       cin >> b;
       cout << "Please enter the line number: ";
       cin >> c;
       ofstream lfo("list2.txt");
       lfo << linesi(b,c);
    }
         
    int linesi (char b, int c)
     {
         ifstream ag(b);
         int x = 1;
         char buffer2[256];
       while (!ag.eof())
       {
        ag.getline(buffer2, 200);
        if (x == c) { return buffer2; }
        x++;
    }
    }
    I've been working on this for hours.. help would be greatly appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something along this line...
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    char *linesi (char *b, int c)
    {
       ifstream ag(b);
       for ( int x = 1; ag.getline(b, 200); ++x )
       {
          if ( x == c )
          {
             return b;
          }
       }
       return 0;
    }
    
    int main ()
    {
       char b[256];
       int c;
       cout << "Please enter the filename of a .txt file: ";
       cin >> b;
       cout << "Please enter the line number: ";
       cin >> c;
       ofstream lfo("list2.txt");
       lfo << linesi(b,c);
    }
    
    /* my output
    Please enter the filename of a .txt file: testpp.cpp
    Please enter the line number: 5
    
    H:\test>type list2.txt
    char *linesi (char *b, int c)
    */
    Last edited by Dave_Sinkula; 06-09-2005 at 08:56 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    Much appreciated!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    In C++, the type of the variable you send to a function must exactly match the type of the variable the function is expecting. You declared your function like this:
    Code:
    int linesi (char b, int c);
    The function is expecting a char type and an int type. You called your function like this:

    Code:
    char b[256];
    int c;
    ..
    ...
    linesi(b,c);
    You are sending your function a char array and an int. A char array is not the same thing as a char type. Here is how you declare both a char array and a char type:

    char b[256];
    char ch = 'x';

    If you sent the variable ch to your function instead of b, you wouldn't get an error. It turns out that for a char array, the brackets and the size are part of the type of the variable. Your variable 'b' is actually of type char[256]. Here is an example:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int linesi (char b[256], int c, char ch);
     
    int main ()
    {
       char b[256];
       int c;
       char ch='x';
       
       cout << "Please enter the filename of a .txt file: ";
       cin >> b;
       cout << "Please enter the line number: ";
       cin >> c;
       
       linesi(b, c, ch);
    
       return 0;
    }
         
    int linesi (char b[256], int c, char ch)
    {
    	
    	cout<<endl<<"Here is your input:"<<endl;
    	cout<<b<<endl;
    	cout<<c<<endl;
    	cout<<ch<<endl;
    	
    	return 100;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid conversion from `char*' to `char'
    By Ron in forum C Programming
    Replies: 11
    Last Post: 06-14-2008, 03:10 PM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM