By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

converting the string of a file to a float

P: 43
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I think if you see my code you'll get what im trying to do :
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7. using std::ifstream;
  8. using std::ofstream;
  9. //Structure for holding all the grades                                        
  10. struct Grades
  11. {
  12.     string student;
  13.     float quizzes[7];
  14.     float projects[6];
  15.     float exams[2];
  16.     float labs[14];
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.     Grades a, b, c, d, e;
  23.     string line;
  24.     ifstream myfile("grades.txt");
  25.     if (myfile.is_open())
  26.     {
  27.         if (! myfile.eof() )
  28.         {
  29.             getline (myfile,line, ' ');
  30.             a.student = line;
  31.         }
  32.         for (int i = 0; i <= 6; i++)
  33.         {
  34.             float quiz;
  35.             getline (myfile, line, ' ');
  36.             quiz = strtof(line);
  37.             b.quizzes[i] = quiz;
  38.         }
  39.  
  40.         myfile.close();
  41.     }
  42.     else cout << "Unable to open file";
  43.  
  44. }
  45.  
the problem is in the strtof(line) which is the string to float converter...I have no idea what Im doing wrong.

The file im reading in from is grades.txt:
Expand|Select|Wrap|Line Numbers
  1. Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 
  2. 96 100 98 92 88 96 92 86 92 94 100 96 
  3.  
Im trying to assign the numbers from the input now into each of the arrays...I got the string student to be Smith and now Im working on gettin the next 7 grades into the quizzes array then the next 6 grades into projects and so on...Any help will be greatly appreciated. I just need to figure out why the converter isnt working...heres what the compiler says:

grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `float strtof(const char*, char**)'
Apr 30 '07 #1
Share this Question
Share on Google+
12 Replies


ilikepython
Expert 100+
P: 844
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I think if you see my code you'll get what im trying to do :
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7. using std::ifstream;
  8. using std::ofstream;
  9. //Structure for holding all the grades                                        
  10. struct Grades
  11. {
  12.     string student;
  13.     float quizzes[7];
  14.     float projects[6];
  15.     float exams[2];
  16.     float labs[14];
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.     Grades a, b, c, d, e;
  23.     string line;
  24.     ifstream myfile("grades.txt");
  25.     if (myfile.is_open())
  26.     {
  27.         if (! myfile.eof() )
  28.         {
  29.             getline (myfile,line, ' ');
  30.             a.student = line;
  31.         }
  32.         for (int i = 0; i <= 6; i++)
  33.         {
  34.             float quiz;
  35.             getline (myfile, line, ' ');
  36.             quiz = strtof(line);
  37.             b.quizzes[i] = quiz;
  38.         }
  39.  
  40.         myfile.close();
  41.     }
  42.     else cout << "Unable to open file";
  43.  
  44. }
  45.  
the problem is in the strtof(line) which is the string to float converter...I have no idea what Im doing wrong.

The file im reading in from is grades.txt:
Expand|Select|Wrap|Line Numbers
  1. Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 
  2. 96 100 98 92 88 96 92 86 92 94 100 96 
  3.  
Im trying to assign the numbers from the input now into each of the arrays...I got the string student to be Smith and now Im working on gettin the next 7 grades into the quizzes array then the next 6 grades into projects and so on...Any help will be greatly appreciated. I just need to figure out why the converter isnt working...heres what the compiler says:

grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `float strtof(const char*, char**)'
Try instead of strtof(line) put strtof(line.c_str()). Some functions require a \0 charactor at the end.
Also, I don't know about you but I couldn't find much about strtof() on Google. Have you tried using atof().
Apr 30 '07 #2

Savage
Expert 100+
P: 1,764
Also, I don't know about you but I couldn't find much about strtof() on Google.
Thats because it don't exist at all.Only simmilar function is strtod() which converts string to double.

Savage
Apr 30 '07 #3

P: 43
okay i tried both of your suggestions and neither worked....i think it's because of the witespace that its not working. Can you please look at it again I've tried everything and i keep getting error mesages...here's the 2 i got when i tried your suggestions:
-bash-3.00$ g++ grader.cpp
grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `double atof(const char*)'
-bash-3.00$ g++ grader.cpp
grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `double strtod(const char*, char**)'
Apr 30 '07 #4

Savage
Expert 100+
P: 1,764
okay i tried both of your suggestions and neither worked....i think it's because of the witespace that its not working. Can you please look at it again I've tried everything and i keep getting error mesages...here's the 2 i got when i tried your suggestions:
-bash-3.00$ g++ grader.cpp
grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `double atof(const char*)'
-bash-3.00$ g++ grader.cpp
grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `std::string' to `const char*' for argument `1' to `double strtod(const char*, char**)'
Try,instead of using string to use a pointer to a char(char *line)

Savage
Apr 30 '07 #5

ilikepython
Expert 100+
P: 844
Try,instead of using string to use a pointer to a char(char *line)

Savage
Also, isn't the only difference the terminating nul charactor, so you could just do line.c_str(). I always thought that that was the difference between char* and string but I'm not sure so could you please correct me?
May 1 '07 #6

Savage
Expert 100+
P: 1,764
Also, isn't the only difference the terminating nul charactor, so you could just do line.c_str(). I always thought that that was the difference between char* and string but I'm not sure so could you please correct me?
Yes,thats the only differance and c_str() should work,but OP said that he tryed our suggestions and it didn't worked so that's way I suggested just using char*.


Savage
May 1 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
Using line.c_str() may give you the char[] representation of the string, but any changes made to this char[] are not echoed in the string object. See here for more information.

You may have to parse the string yourself or, as suggested, use a char* throughout rather than a string object.
May 1 '07 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Here's a C++ solution:
Expand|Select|Wrap|Line Numbers
  1. ifstream input("Filetest.txt");
  2. string name;
  3. float var[20];
  4. input >> name >> var[0] >> var[1] >> var[2];
  5. cout << name << " " << var[0] << " " << var[1] << " " << var[2] << endl;
  6.  
  7.  
You simply haul the line from the text file into the appropriate variables. The >> operator skips all whitespace (unless you specify noskipws). If you know the format of the file then this works. If the number of numeroic values varies, then I would put a count after the name and before the variables so the program can receive the count and therby know how many values follow:

count---V

Smith 3 1.5 2.5 3
Jones 4 3.0 26 17.5 20
May 1 '07 #9

P: 43
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7. using std::ifstream;
  8. using std::ofstream;
  9. //Structure for holding all the grades                                                 
  10. struct Grades
  11. {
  12.     string student;
  13.     float quizzes[7];
  14.     float projects[6];
  15.     float exams[2];
  16.     float labs[14];
  17. };
  18. int main()
  19. {
  20.     float quiz;
  21.     Grades a, b, c, d, e;
  22.     string line;
  23.     ifstream myfile("grades.txt");
  24.     if (myfile.is_open())
  25.     {
  26.         if (! myfile.eof() )
  27.         {
  28.             getline (myfile,line, ' ');
  29.             a.student = line;
  30.         }
  31.         for (int i = 0; i <= 6; i++)
  32.         {
  33.             float quiz;
  34.             getline (myfile, line, ' ');
  35.             quiz = line.c_str();
  36.             b.quizzes[i] = quiz;
  37.         }
  38.  
is that what u mean cuz i tried it and im still getting an error but its different:
grader.cpp: In function `int main()':
grader.cpp:41: error: cannot convert `const char*' to `float' in assignment
May 1 '07 #10

Ganon11
Expert 2.5K+
P: 3,652
No. You declare quiz as a float, and then try to assign a CString to it. You need to make use of the atof function and a double.
May 1 '07 #11

P: 43
Okay...thanks for all the help everyone I finally figured it out!
May 2 '07 #12

Savage
Expert 100+
P: 1,764
Okay...thanks for all the help everyone I finally figured it out!
We are more than happy to help u!
May 2 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.