Hi. I'm doing an assignment. And I'm trying to read input from a file word by word using getline and then assigning each word to a variable, using a function called 'void ReadShares()'. Each line in the input file consists of "date time, price, volume, value, condition". Date is in DD/MM/YYYY format and time is in HH:MM:SS PM format. I'm supposed to display the time in 24 hours clock instead of 12 hours too.
It's not recommended to use eof() as the condition for a while loop. There are several reasons, one is that there are other error flags which may be set before reaching eof. Another is that, as in your code above, the check is made before attempting to read from the file. What is actually needed is a check on the file status after the input operation.
Typically a better way is to test the result of the input operation within the loop condition, for example like this:
1 2 3 4 5 6
std::string line;
while (std::getline(inFile, line))
{
std::cout << line << std::endl;
}
I've not tested this, (can't really do that without seeing a sample of the actual file data) but it might work better than your current code:
Is there a reason you want to use getline() instead of just using the extraction operator>>? The extraction operator has the behavior you seem to want. Why are price, volume, and value strings? Wouldn't they be better off being numeric values instead?
I'm supposed to display the time in 24 hours clock instead of 12 hours too.
What have you tried, this is really fairly simple.
Hi, the input file includes:
Date Time, price, volume, value, condition
The file data is something like this:
7/5/2014 12:12:12 PM, 4.3, 2000, 450030, AB CD
7/5/2014 12:11:30 PM, 5.4, 32000, 65930,
7/5/2014 12:09:32 PM, 3.9, 2899, 560030, CD
.
.
.
7/5/2014 10:50:04 AM 6.4, 8750, 123789, F
Is there a reason you want to use getline() instead of just using the extraction operator>>?
Because I want to read the words both separated by commas and white space. Only the date and time is separated by just white space. The other values are separated by commas only.
Why are price, volume, and value strings?
I wanted to use the getline function which, if I'm not wrong, only works on strings.
What have you tried, this is really fairly simple.
I've tried to put this function in another class called timeImp.cpp, which is an implementation file for time.h.