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 :
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:
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**)'
Code:
#include <cstdlib> #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; using std::ifstream; using std::ofstream; //Structure for holding all the grades struct Grades { string student; float quizzes[7]; float projects[6]; float exams[2]; float labs[14]; }; int main() { Grades a, b, c, d, e; string line; ifstream myfile("grades.txt"); if (myfile.is_open()) { if (! myfile.eof() ) { getline (myfile,line, ' '); a.student = line; } for (int i = 0; i <= 6; i++) { float quiz; getline (myfile, line, ' '); quiz = strtof(line); b.quizzes[i] = quiz; } myfile.close(); } else cout << "Unable to open file"; }
The file im reading in from is grades.txt:
Code:
Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88 96 92 86 92 94 100 96
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**)'
Comment