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 : -
#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: -
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**)'
12 11211
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 : -
#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: -
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_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().
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
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**)'
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
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?
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
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.
Here's a C++ solution: -
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
-
#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
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.
Okay...thanks for all the help everyone I finally figured it out!
Okay...thanks for all the help everyone I finally figured it out!
We are more than happy to help u!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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(...
|
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 ).
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
| |