converting the string of a file to a float

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joestevens232
    New Member
    • Oct 2006
    • 43

    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 :
    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 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:
    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
    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**)'
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by joestevens232
    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 :
    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 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:
    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
    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_s tr()). 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().

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by ilikepython
      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

      Comment

      • joestevens232
        New Member
        • Oct 2006
        • 43

        #4
        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**)'

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by joestevens232
          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

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Savage
            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?

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by ilikepython
              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

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Here's a C++ solution:
                  Code:
                  ifstream input("Filetest.txt");
                  string name;
                  float var[20];
                  input >> name >> var[0] >> var[1] >> var[2];
                  cout << name << " " << var[0] << " " << var[1] << " " << var[2] << endl;
                  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

                  Comment

                  • joestevens232
                    New Member
                    • Oct 2006
                    • 43

                    #10
                    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()
                    {
                        float quiz;
                        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 = line.c_str();
                                b.quizzes[i] = quiz;
                            }
                    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

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      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.

                      Comment

                      • joestevens232
                        New Member
                        • Oct 2006
                        • 43

                        #12
                        Okay...thanks for all the help everyone I finally figured it out!

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by joestevens232
                          Okay...thanks for all the help everyone I finally figured it out!
                          We are more than happy to help u!

                          Comment

                          Working...