473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

converting the string of a file to a float

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
12 11211
ilikepython
844 Expert 512MB
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
1,764 Expert 1GB
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
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
1,764 Expert 1GB
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
844 Expert 512MB
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
1,764 Expert 1GB
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
3,652 Expert 2GB
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
9,208 Expert Mod 8TB
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
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
3,652 Expert 2GB
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
Okay...thanks for all the help everyone I finally figured it out!
May 2 '07 #12
Savage
1,764 Expert 1GB
Okay...thanks for all the help everyone I finally figured it out!
We are more than happy to help u!
May 2 '07 #13

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

Similar topics

2
by: Ikot | last post by:
Hello, I am stuck with a problem, might be very trivial, to ask here, but dont find a way out. I read a string like 12345.678 from a text file, which will be string for me, but how do I convert it...
15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
3
by: Jim Langston | last post by:
I have a CSkill class which is rather complex as it is recursive. That is: class CSkill { public: CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value ) {}; void Update(...
3
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
7
by: DirtyRasa | last post by:
Here is what my professor told me to to. Write a function that converts a string to a float and returns a float. Test your function by entering f4 and with 4f. Both should say Data entered was...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.