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?
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?
Could you give us the smallest example of a program that doesnt work?
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
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.
Sorry for the limited information
This is the function I am working on now, where c_str() does not work
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/programCode: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; }
ThanksCode: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; }
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.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
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.
Kind of embarrassing, but I dont know how. Reading trough some stuff nowDo 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?
Edit:
I have made something that works now, thanks for all help.
This work fine, except that the last number never gets inputedCode: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; }
Last edited by h3ro; 05-14-2007 at 03:00 PM.
This is because when you exit the for loop - tempString can contain data that you never processThis work fine, except that the last number never gets inputed
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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 workThis is because when you exit the for loop - tempString can contain data that you never process
FOR EXAMPLECode: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); }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler