C++ I need help when I grab data from a text file, and have the float number whi
ID: 3685119 • Letter: C
Question
C++
I need help when I grab data from a text file, and have the float number which is Price (the last getline) return as a float (Only one line of the file is a float Ex: 45.99). Currently with the code I have, its returned the data as an int. Here is a section of my code:
void ReadInventory(book books[], int& size, int MAX_Size)
{
ifstream dataFile;
dataFile.open("inventory.text");
string str;
if (!dataFile.fail())
{
while (dataFile && size < MAX_Size)
{
getline(dataFile, str);
books[size].ISBN = atoi(str.c_str());
getline(dataFile, books[size].Title);
getline(dataFile, books[size].Author);
getline(dataFile, books[size].Publisher);
getline(dataFile, str);
books[size].Quantity = atoi(str.c_str());
getline(dataFile, str);
books[size].Price = atoi(str.c_str());
size++;
}
dataFile.close();
}
Or is there somewhere else in this code that I need only one line from the file as a float?!
Explanation / Answer
books[size].Price = atof(str.c_str());
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.