Like Tree1Likes
  • 1 Post By laserlight

invalid conversion from ‘char’ to ‘const char*' problem

This is a discussion on invalid conversion from ‘char’ to ‘const char*' problem within the C++ Programming forums, part of the General Programming Boards category; I am having a problem with my code. I am writing a program to read an input from a file(just ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    invalid conversion from ‘char’ to ‘const char*' problem

    I am having a problem with my code. I am writing a program to read an input from a file(just 50 lines of three digits numbers each) and then put it into a 2 dim array where a row is going to be a whole number where colums are going to be the numbers in 1st, 2nd and 3rd position (it is for Project Euler)

    I succeed in reading the inputs from a file. But then when i try to convert the numbers in a string in doesnt work because it gives me the invalid conversion error.

    The code is:

    Code:
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <stdlib.h>
        
        using namespace std;
        
        
        	    fstream myfile ("keylog.txt");
        	    string tries[60];
        	    string line;
        	    char codes[50][3];
        	    int i = 0;
        	    int j = 0;
        	    int code[50][3];
        
        	    while(myfile.good()){
        		    getline(myfile, line);
        		    tries[i] = line;
        		    i++;}
        
        	    for(i = 0; i < 50; i++){
        		    for(j = 0; j < 3; j++){
       			    code[i][j] = atoi( tries[i][j] );
        		    }
        	    }
               return 0;
        }
    It compiles with the following error

    keylog.cpp: In function ‘int main()’:
    keylog.cpp:24:35: error: invalid conversion from ‘char’ to ‘const char*’
    keylog.cpp:24:35: error: initializing argument 1 of ‘int atoi(const char*)’

    I have been playing with this couple of hours but I could not figure out what it is.
    Any help would be appreciated.

    Best regards
    Mike

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    your call to getline may as well just be tries[i]; the line variable is uneeded.
    Last edited by rogster001; 08-08-2011 at 09:11 AM. Reason: complete b***s of a post error ridden
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by rogster001 View Post
    Your tries array is declared as one dimension but then you try and use it like it has 2 dimensions
    tries is an array of std::strings; therefore, tries[ i ] is a string, and tries[ i ][ j ] is a char.

    To the OP: The function atoi expects a const char*, and you are giving it a char. To compile this, you need to give it the address of (&) tries[ i ][ j ].

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    duh yes, not seeing that, apologies, but he should still test the length
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by kuci
    I am writing a program to read an input from a file(just 50 lines of three digits numbers each) and then put it into a 2 dim array where a row is going to be a whole number where colums are going to be the numbers in 1st, 2nd and 3rd position (it is for Project Euler)
    It sounds like you just want to subtract '0', not use atoi.

    Quote Originally Posted by Ushakal
    To compile this, you need to give it the address of (&) tries[ i ][ j ].
    Note that just because it compiles does not mean that it is correct.
    Last edited by laserlight; 08-08-2011 at 09:23 AM.
    Ushakal likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by Ushakal View Post
    To the OP: The function atoi expects a const char*, and you are giving it a char. To compile this, you need to give it the address of (&) tries[ i ][ j ].
    This will cause undefined behavior. atoi is for C-style strings only, and not individual characters.
    Never try to pass individual characters to atoi in any way or form.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Elysia View Post
    This will cause undefined behavior. atoi is for C-style strings only, and not individual characters.
    Never try to pass individual characters to atoi in any way or form.
    Is that because atoi searches the string for the first numerical character (that is, '0' <= c && c <= '9'), then continues to read the string until it encounters a non-numerical character or the null terminating character, which may occur at any point in the following bytes of memory after the location of the single char (thus atoi may return an unexpected value)?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ushakal
    Is that because atoi searches the string for the first numerical character (that is, '0' <= c && c <= '9'), then continues to read the string until it encounters a non-numerical character or the null terminating character, which may occur at any point in the following bytes of memory after the location of the single char (thus atoi may return an unexpected value)?
    Yes, but the undefined behaviour is not because "atoi may return an unexpected value", but because atoi may end up reading beyond the bounds of the array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by laserlight View Post
    Yes, but the undefined behaviour is not because "atoi may return an unexpected value", but because atoi may end up reading beyond the bounds of the array.
    I see. Alright, thanks.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by Ushakal View Post
    Is that because atoi searches the string for the first numerical character (that is, '0' <= c && c <= '9'), then continues to read the string until it encounters a non-numerical character or the null terminating character, which may occur at any point in the following bytes of memory after the location of the single char (thus atoi may return an unexpected value)?
    Yes, hence the why it requires a C-style string, not a char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. "invalid conversion from 'char' to 'const char' "
    By gurharman in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2010, 09:43 AM
  3. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 11:58 AM
  4. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  5. invalid conversion from 'char' to 'const char*'
    By LiKWiD in forum C++ Programming
    Replies: 10
    Last Post: 03-20-2005, 02:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21