How to read String, Int, and Floats from the same line in a File?
Feb 21, 2017 at 1:47am UTC
I'm trying to read and display variables that I have stored in a file. Here is what my textfile looks like on my computer.
ABC 23 18.50 23.25 1.50 2.00
XYZ 5 226.20 175.00 3.50 4.00
I need to be able to read the 3 Letters as a string, the next number as an integer, and the rest of the values as floats.
I included my code. When I run the program, I get into an infinite loop with very large and strange numbers. Could anyone show me the error of my ways?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int ReadFile (ifstream&, string);
int main ()
{
ifstream inFile;
string filename = "Shares.txt" ;
bool IsItOpen;
string StockName;
int NUMofShares;
float PurchasePrice, SalePrice, PurchaseCommission, SaleCommission ;
cout << "Please open the file associated with the Multiple Stock Sales.\n" << endl;
inFile.open(filename);
IsItOpen = ReadFile(inFile, filename);
cout << " Stock, Shares, P Price, S Price, P Commission, S Commission" << endl;
while (!inFile.eof())
{
getline (inFile, StockName);
inFile >> NUMofShares;
inFile >> PurchasePrice;
inFile >> SalePrice;
inFile >> PurchaseCommission;
inFile >> SaleCommission;
cout << StockName << NUMofShares << PurchasePrice << SalePrice << PurchaseCommission << SaleCommission << endl;
}
inFile.close();
system("pause" );
return 0;
}
int ReadFile (ifstream& myFile, string NameOfFile)
{
cout << "Type the name of the file." << endl;
cout << "Example Format: StockPrices.txt" << endl;
cout << "\nPlease Enter Here: " ;
cin >> NameOfFile; //prompting user to enter the filename
if (myFile.is_open() && NameOfFile == "Shares.txt" )//validating the filename that was entered
{
cout << NameOfFile << " has opened successfully.\n" << endl;
return true ;
}
else //if an error occurred opening the file, the program will close
{
cout << NameOfFile << " couldn't be opened...\n" << endl;
cout << "Closing down the program due to file error..." << endl;
return -1;
}
}
Feb 21, 2017 at 1:56am UTC
Why not just
inFile >> StockName;
?
std::getline reads a whole line not just three letters as a string.
I would typically do it like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while (!inFile.eof())
{
inFile >> StockName;
inFile >> NUMofShares;
inFile >> PurchasePrice;
inFile >> SalePrice;
inFile >> PurchaseCommission;
inFile >> SaleCommission;
if (!inFile) break ; // Checks if file operation is a success
cout << StockName << ' ' ;
cout << NUMofShares << ' ' ;
cout << PurchasePrice << ' ' ;
cout << SalePrice << ' ' ;
cout << PurchaseCommission << ' ' ;
cout << SaleCommission << endl;
}
Last edited on Feb 21, 2017 at 1:57am UTC
Feb 21, 2017 at 2:04am UTC
Mantorr22,
I tried something kind of similar to that earlier and it didn't work.
I do agree that using getline is probably a bad idea after all.
I just tried what you provided and it works great. I appreciate it!
Topic archived. No new replies allowed.