Hello guys,
Im currently doing a project for school.
Its basically a Cash register that gains data from an input file.
In the input file, the first data is the beginning balance (which i got working).
After the beginning balance, it is followed by transaction number, then transaction amount. I can not figure out how to grab the data from the file (in the getData function). Any help would be appreciated.
EDIT: Just found out I do not need two dimensional arrays for the assignment, its just how I thought it would be done.
A little less than halfway down this page you will see how to read data from a file. It uses the getline() function. This will read the file line-by-line and save it in a specified variable. If you have multiple bits of data on one line, you'll have to split it up using string member functions found in the documentation on this website.
@Tresky, thank you for your reply.
I understand how to read data from a file as Ive done it for the beginning balance, where I have an issue is when I have to read data from a file and input it into a 2dimmensional array. So the file looks like "1 1000.1 3 2819 1 2932". The transaction number is the first followed by the balance.
Is there a specific reason why you want this data inside of a two-dimensional array? It seems like overkill. :P
However, to split the line up into data points.... I don't want to type this all again cause I typed it all yesterday, so here is the link to the post. ;D
You're searching for the space before you even have data in the string. You have to get the first line of data and then find the space.
-Get line of data.
-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.
-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.
Keep repeating this until you have finished the whole line. It's a repetitive process because you have 4 or 5 pieces of data on the same line so you have to do it 4 or 5 times to split all of the data up.
I understand the concept, but putting it into code is where I am completely lost.
Especially since I need to do this part in a function:
-Get line of data.
-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.
and the rest in the main im guessing. I believe I should use a for loop but do not know where to set the end.
To convert from a string to an integer you use a command called atoi(). There isn't one for double, but there is one for float (atof) which you can then cast to a double. You'll have to include a new library for both of these. I believe it is cstdlib if I remember correctly.
1 2 3 4 5 6
#include <cstdlib>
...
string value = "32";
int integer = atoi(value);
1. Make sure there is data in the input file.
2. Make sure you are accessing the correct input file from your program.
3. Make sure that in the line of data you are reading that there is actually a space to be found. If there is no space, then space_position will be set to 0 and your substr() will grab everything from position 0 to position 0... which is nothing. Haha.