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

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    invalid conversion from 'char' to 'const char*'

    I am writing a function, and I'm getting the error "invalid conversion from 'char' to 'const char*'" when I try to compile. Any help would be appreciated.

    Code:
    char aline[125];
    char sline[125];
    char lbracket[2] = "[";
    char rbracket[2] = "]";
    
         . . . .
    
    if (strcmp(aline[0],lbracket) == 0)
    
         . . . .
    I'm reading into aline with getline(), and then attempting to analyze the first character to see if it is "[". I tried using strcmp(aline[0],"["), but got the same error.
    --LiKWiD
    Becoming un-noobified one day at a time.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    aline[0] is the first char which is of type char.
    aline would be a pointer to a string.
    "[" would be a pointer to a string.
    '[' is a single char so:
    Code:
    if (aline[0]=='[')
    strcmp wouldn't work for what you want because it compares the whole string.
    Last edited by Quantum1024; 03-17-2005 at 04:39 AM.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Yes Quantum is correct.


    You are comparing the entire string I think. Perhaps what you need is something like the following:

    Code:
    #include <iostream.h>
    
    int main()
    
    {
        char aline[125];
        char sline[125];
        char lbracket[2] = {"["};
        char rbracket[2] = {"]"};
    
         cout<<"Type in aline"<<endl;
         cin>>aline;
         
         if (aline[0]==lbracket[0])
         {
             cout<<"The first character was indeed a: [ "<<endl;
         }
         else
         {
             cout<<"The first character was not a: [ "<<endl;
         }    
         
             int stop;
             cin>>stop;    
         
     }

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    std::string anyone?

    P.S
    treenef its <iostream> not <iostream.h> and we also like to put things into the std namespace
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Sweet, works now. Thanks. I guess I don't understand how they work too terribly well. I'm also having trouble appending characters to the end of a string. This is what I have:

    Code:
    strcat(div_check,aline[z])
    That is, of course, in a loop where I want to keep adding the characters from aline to a certain point. I get the same "invalid conversion" error. Thanks again.

    EDIT: I tried changing it to

    Code:
    div_check[z] = aline[z]
    using that same logic from above. Compiles right, so I guess we'll see what happens.
    Last edited by LiKWiD; 03-17-2005 at 03:55 PM. Reason: Correction
    --LiKWiD
    Becoming un-noobified one day at a time.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Well, my program compiles right, but crashes when I try to run it.

    *Long Code Ahead*
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int writeini (char filename[20], char division[20], char section[30], char value[90]) {
        char aline[125];
        char sline[125];
        int before_val;
        
        ofstream ini_write ((strcat("temp",filename)), ios::out);
        
        //validity checker--checks to see if it should replace or add the new information
        ifstream ini_check (filename, ios::in);
        if ( !ini_check.is_open() ) {
             return 1; //returns error "1" if file could NOT be opened
             }
        else {
             int ch;
             char div_check[30];
             
             for (int x = 0, t = 0; (ch = cin.get()) != EOF; x++) {
                 cin.getline(aline,'\n');
                 if (t == 0) {               //check if division and section exist; else set them to be created
                    if (aline[0] == '[') {
                       int lastchar = 0;
                       strcpy(div_check,"");
                    
                       for (int y = 1; aline[y] != ']'; y++) {
                           if (aline[y] == ']') lastchar = --y; //assign 'lastchar' to the last character in the division name
                           }
                    
                       if (lastchar > 1) {
                          int y = 1;
                          while (y <= lastchar) {
                             //div_check += aline[y]; //assembles the found division
                             y++;
                             }
                          }
                       if (strcmp(div_check,division) == 0) { //compares the found division with input division, if same moves on to section
                          t = 1;
                          }
                       strcpy(sline,aline);
                       }
                    }
                 else if (t == 1) {                      //checks division for matching section
                      strcpy(div_check,"");
                      
                      if (aline[0] == '[') {
                         for (int z = 0; aline[z] != '='; z++) {   //assembles all characters before '=' into div_check
                             div_check[z] = aline[z];
                             }
                         }
                      
                      if (strcmp(div_check,section) == 0) {    //if the user input 'section' = the assembled div_check, it readies sline to write to file.
                         strcpy(sline,section);
                         strcat(sline,"=");
                         strcat(sline,value);
                         t = 4;
                         }
                      else if (aline[0] == '[') {      //if the division is ending and a new starting, it writes sline before going on to copy the rest of the file.
                           strcpy(sline,section);
                           strcat(sline,"=");
                           strcat(sline,value);
                           ini_write << sline << "\n";
                           strcpy(sline,aline);
                           t = 4;
                           }
                      }
                 else if (t == 4) {              //writes the remaining portion of the file after edit
                      strcpy(sline,aline);
                      }
                      
                 
                 ini_write << sline << "\n";
                 strcpy(sline,"");
                 }
             }
        ini_write.close();
        ini_check.close();
        }
    
    int main (int argc, char *argv[])
    {
        char hi[30], hey[30], ho[30], hehe[30];
        
        cout << "Hello there. 1st: ";
        cin >> hi;
        cout << "2nd: ";
        cin >> hey;
        cout << "3rd: ";
        cin >> ho;
        cout << "4th: ";
        cin >> hehe;
        
        writeini(hi,hey,ho,hehe);
        
        cout << "writeini(" << hi << "," << hey << "," << ho << "," << hehe << ");";
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    EDIT: Oh, the program goes through and asks for 1st, 2nd, 3rd, and 4th, then crashes. (Apparently while running the function)
    Last edited by LiKWiD; 03-17-2005 at 04:07 PM. Reason: Time of Error
    --LiKWiD
    Becoming un-noobified one day at a time.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I didn't read everything but here are some things I noticed.
    Code:
    ofstream ini_write ((strcat("temp",filename)), ios::out);
    This would add filename to "temp" but temp is only 5 chars (4+NULL charactor) so something is going to be overwritten.
    Code:
    for (int z = 0; aline[z] != '='; z++) {   //assembles all characters before '=' into div_check
                             div_check[z] = aline[z];
    Here you only stop if you encounter a '=' but if there is no = in the imput the loop will keep going possibly crashing.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    I changed

    Code:
    ofstream ini_write ((strcat("temp",filename)), ios::out);
    to

    Code:
    ofstream ini_write ("temp.ini", ios::trunc);
    as I realized, thanks to Quantum, that it was causing the program to crash. It runs now, however, when the function executes, it gets to the first 'if' statement and seems to be caught in an infinite loop or something. I'm not really sure. This:

    Code:
        if ( !ini_check.is_open() ) {
             return 1; //returns error "1" if file could NOT be opened
             }
        else {
             int ch;
             char div_check[30];
             
             for (int x = 0, t = 0; x < 5; x++) {
    .   .   .
    executes, but doesn't seem to ever get to/past the 'for'. I put cout << "HELLO THERE"; before the for, ran it and it printed; then I put it directly after the for and it never shows. Any clues?


    EDIT: Ok, well, I'm dumb. I just noticed that I had cin.getline() instead of file.getline(), so that fixed my problem. I'll post once I run into another error. Thanks.
    Last edited by LiKWiD; 03-18-2005 at 01:19 AM.
    --LiKWiD
    Becoming un-noobified one day at a time.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Well, that didn't take long. I am not sure how to add a character to my string. I'm trying to add aline[y] to string div_check.

    Code:
    char div_check[30];
    .   .   .
    div_check += aline[y];  //also tried ...
    //strcat(div_check,aline[y]); but that's the same problem I was dealing with earlier.
    --LiKWiD
    Becoming un-noobified one day at a time.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    There are several ways to add a single charactor to a string. If you want to only add one char then something like
    Code:
    div_check[strlen(div_check)]=aline[y]
    to add more then one you could use strcat or better yet strncat.
    Code:
    strncat[div_check, &aline[y], MaxCharsToCoppy);

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Thanks for all the help. I learned a lot messing with arrays, but in the end I looked at my code and thought I should rewrite it with STL strings. It is much easier to deal with (IMO) and so far it seems to run exactly as I want it. Thanks again for all the help. I'll post the code I have for possible future reference and to compare the two. Any suggestions on improving it are welcome.


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int writeini (string filename, string division, string section, string value) {
        /*
        ERROR REFERENCE:
              0 = Function executed successfully
              1 = Input file, 'filename', does not exist or could not be opened
              2 = Output file "temp.ini" could not be created/opened
              3 = Unexpected error handling 'filename' (ifstream ini_in)
              4 = Incorrect format in 'filename'; division doesn't match required format ( [division] )
        */
        
        char buff = ' ';
        string linein, lineout, sub;
        int fhandle = 1;
        unsigned int loc;
        
        ifstream ini_in (filename.c_str(), ios::in);
        ofstream ini_out ("temp.ini", ios::trunc);
        
        if ( !ini_in.is_open() ) return 1; // Input could not be opened
        if ( !ini_out.is_open() ) return 2; // Output could not be opened
        
        ini_in.get(buff); // Check input for EOF
        
        while ( !ini_in.eof() ) {
              getline(ini_in, linein, '\n'); // istream from 'filename' to string 'linein'
              if (buff != '\n') linein = buff + linein; // Adds get(buff) to the beginning of string 'linein'
              
              switch (fhandle) {
                     case 1: // Handles division matching
                          if (linein[0] != '[') {
                             lineout = linein;
                             break; // Continue looping through istream until division beginning is found
                             }
                          
                          loc = linein.find("]",0); // Find end of division name
                          if (loc == string::npos) return 4; // Incorrect .ini format
                          else --loc;
                          
                          sub = linein.substr(1, loc);
                          
                          if (sub == division) {
                                  fhandle = 2; // Moves on to examine section
                                  }
                          
                          lineout = linein;
                          break;
                     case 2: // Handles section matching
                          if (linein[0] == '[') { // If a new division has started, add new data and copy remainder of file
                             ini_out << section << "=" << value << "\n";
                             fhandle = 3;
                             lineout = linein;
                             break;
                             }
                          
                          loc = linein.find("=",0); // Find end of section
                          if (loc == string::npos) {
                                  if (linein == section) {
                                     lineout = section + "=" + value;
                                     fhandle = 3;
                                     break;
                                     }
                                  else {
                                       lineout = linein + "=";
                                       break;
                                       }
                                  }
                          
                          sub = linein.substr(0, loc);
                          
                          if (sub == section) { // If section matches input, change to new data
                                  lineout = section + "=" + value;
                                  fhandle = 3;
                                  }
                          else {
                               lineout = linein;
                               }
                          break;
                     case 3: // Copies the remainder of the file once necessary changes have been made
                          lineout = linein;
                          break;
                     default:
                          return 3; // Unexpected Error
                          break;
                     }
              
              ini_out << lineout << "\n";
              ini_in.get(buff); // Check input for EOF
              }
        
        if (fhandle == 1) {
           ini_out << "[" << division << "]\n";
           ini_out << section << "=" << value << "\n";
           }
    
        ini_in.close();
        ini_out.close();
        return 0;
    }
    
    int main (int argc, char *argv[])
    {
        string hi, hey, ho, hehe;
        unsigned int ini_return;
        
        cout << "1st: "; getline(cin, hi, '\n');
        cout << "2nd: "; getline(cin, hey, '\n');
        cout << "3rd: "; getline(cin, ho, '\n');
        cout << "4th: "; getline(cin, hehe, '\n');
        
        ini_return = writeini(hi, hey, ho, hehe);
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Only thing I lack is deleting the original file and renaming "temp.ini" to the original filename. Still working on that.
    Last edited by LiKWiD; 03-20-2005 at 04:31 AM.
    --LiKWiD
    Becoming un-noobified one day at a time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 12:58 PM
  3. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM