c_str() problem

This is a discussion on c_str() problem within the C++ Programming forums, part of the General Programming Boards category; Hallo, I try to do this: faceList.push_back(atoi( line[i].c_str() )); But I get an error saying that c_str() is not declared, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    c_str() problem

    Hallo,

    I try to do this:
    faceList.push_back(atoi( line[i].c_str() ));

    But I get an error saying that c_str() is not declared, but I have already used it and then there is no error.

    This works:
    (float)atof(vertexString[0].c_str())

    Any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Could you give us the smallest example of a program that doesnt work?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Is line an array or vector of strings? If it is, then make sure you #include <string>. If not, then c_str() won't work on an object that is not a string.

    Of course, a stringstream is often better for conversions than atoi.

    Also, if line is a string, but you only want to convert an individual character to a number, then just subtract the '0' character:
    Code:
    int num = line[i] - '0';
    Last edited by Daved; 05-14-2007 at 01:18 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Sorry for the limited information

    This is the function I am working on now, where c_str() does not work
    Code:
    std::vector<int> loadFaceConnections(std::string line)
    {
         //stff
         std::vector <int> faceList;
         for (int i = 2; i <= line.size(); i++)
         {
             if (line[i] != ' ' && line[i] != '/')
             {
                 faceList.push_back(atoi( line[i].c_str() ));
             }
         }
         
         for (int x = 0; x < faceList.size(); x++)
         {
             std::cout << faceList[x] << std::endl;
         }
         
         return faceList;
    }
    But I find it kind of weird that c_str() is not declared (according to compiler) as it works in this function that is part of the same file/program
    Code:
    Vector3D processVertex(std::string line)
    {
             std::string vertexString[3];
             int x = 0;
             
             for (int i = 2; i <= line.size(); i++)
             {
                 if ( line[i] == ' ')
                 {
                      x++;
                 }
                 else
                 {
                     // add to same cord
                     vertexString[x].push_back(line[i]);
                 }
             }
             
             
             // Convert the string into a number
             Vector3D vertex( (float)atof(vertexString[0].c_str()),
                              (float)atof(vertexString[1].c_str()),
                              (float)atof(vertexString[2].c_str())
                              );
             
             return vertex;
    }
    Thanks

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    When you access line[i] in your first example you access the character at index i in the string object line. that character is of type char, and can thus not have the function c_str() in it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    In loadFaceConnections(), line is a std::string, so line[i] is a char, and a char does not have a c_str() member function. Perhaps you wanted to pass a vector of strings instead?
    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

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    So what are you trying to do there? Obviously, line is a string, and line[i] is a character and atoi only works with a string of characters.

    Are you trying to convert a single digit? If so, see my edited post above.

    Are you just trying to extract a number with possibly more than one digit from a string? If so, you'll need to parse the line a bit more. You can use a stringstream, or maybe find() and substr(). It depends on what the line looks like and how you want to parse it. Give the format of the line and an example if you need further help.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I am trying to read some info from a file I have. The format for the data is:
    ...
    f 1/1/1 2/2/2 3/3/3
    f 2/2/2 4/4/4 3/3/3
    ...

    The numbers can be anything but they are always a int.

    If the line starts with the character f I call the loadFaceConnection() function.

    I know that as the loop is now it will only take single number digits, but I did like this so that I had somewhere to start, but I got stuck.

    Thanks for the help so far

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Do you know how to read that line in and get the integers if you were using cin instead of having the text in a string?

    I would place the line into a stringstream and parse it that way since you know the exact format. The code is exactly the same as you'd use for cin except you'd use your stringstream variable instead. Practice first with a simple program and then when it is working move the code into your bigger application.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Do you know how to read that line in and get the integers if you were using cin instead of having the text in a string?
    Kind of embarrassing, but I dont know how. Reading trough some stuff now

    Edit:

    I have made something that works now, thanks for all help.

    Code:
     std::vector<int> loadFaceConnections(std::string line)
    {
         std::vector <int> faceList;
         std::string tempString;
         int tempNumber = 0;
         
         for (int i = 2; i <= line.size(); i++)
         {
             // If the line is not empty and does not have a '/' sign it contains
             // a number. Add it to the temp string
             if (line[i] != ' ' && line[i] != '/')
             {
                 // Add the number to the tempString
                 tempString.push_back(line[i]);
             }
             else
             {
                 // If the string is not empty we create a integer out of it
                 if (tempString.size() != 0)
                 {
                     // Create a number
                     std::istringstream stringNumber (tempString);
                     stringNumber >> tempNumber;
                     
                     // Add the number to the vector
                     faceList.push_back(tempNumber);
                     
                     // Clear the tempString
                     tempString.clear();
                 }
             }
         }
         
         // Print the thing, for debugging purpose
         for (int x = 0; x < faceList.size(); x++)
         {
             std::cout << faceList[x] << std::endl;
         }
         std::cout << std::endl;
         
         
         return faceList;
    }
    This work fine, except that the last number never gets inputed
    Last edited by h3ro; 05-14-2007 at 03:00 PM.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    This work fine, except that the last number never gets inputed
    This is because when you exit the for loop - tempString can contain data that you never process
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    This is because when you exit the for loop - tempString can contain data that you never process
    I know, I assume that I have to else statement one more time, but not sure how to accomplish this. I have tried to increase the size of line.size() but that did not work

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Code:
          for (int i = 2; i <= line.size(); i++)
         {
             // If the line is not empty and does not have a '/' sign it contains
             // a number. Add it to the temp string
             if (line[i] != ' ' && line[i] != '/')
             {
                 // Add the number to the tempString
                 tempString.push_back(line[i]);
             }
             else
             {
                 // If the string is not empty we create a integer out of it
                 if (tempString.size() != 0)
                 {
                     // Create a number
                     std::istringstream stringNumber (tempString);
                     stringNumber >> tempNumber;
                     
                     // Add the number to the vector
                     faceList.push_back(tempNumber);
                     
                     // Clear the tempString
                     tempString.clear();
                 }
             }
         }
         if (tempString.size() != 0)
        {
            // Create a number
            std::istringstream stringNumber (tempString);
            stringNumber >> tempNumber;
                  
            // Add the number to the vector
            faceList.push_back(tempNumber);
                     
        }
     
    FOR EXAMPLE
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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