This is relatively straight forward. For each line in the file (except the first), put the line into a string. Next, determine the indexes of the first and second ':' characters. Put the substring between those characters into another string. To get the amount as an int, use a stringstream.
int a = 0;
int start = 0;
int number;
int total = 0;
while(a < outputstring.size())
{
if(outputstring[a] == ':'){
if(start != 0)
number = int(outputstring.substr(start,a-start));
//After here do whatever you want with the number. Between here
//I think you wanted to total them?
total += number;
// And here
break;
else
start = a + 1;
}
a+= 1;
}
you may need tweak that a little i didn't compile it, i just did it in the browser.
you will need to do that for each line in the text file if your not farmilure with getline, http://www.cplusplus.com/reference/string/getline/
you will need to include the:
#include <string>
And i'm guessing you already know how to read the file with:
while(a < outputstring.size())
{
if(outputstring[a] == ':'){
if(start != 0)
number = int(outputstring.substr(start,a-start));
//After here do whatever you want with the number. Between here
//I think you wanted to total them?
total += number;
// And here
break;
else
start = a + 1;
}
a+= 1;
}
I can't get the amount.. Maybe I cut wrongly?
I try to do a looping so that after I cut and all the amount I am able to calculate. But how?
1 2 3 4 5 6
for(int i = 0; i > file.size(); i++){
a = newchar[file[i].size()];
a = strdup(file[i].c_str());
token = strtok(a,"/");
}//end loop
I'm trying to do something like this so I can gather all the amount, but fail..
I know I did somewhere wrong in this part. The delimiter I think I set wrongly.
I still dun get it haha..
Sorry for my lack of knowledge in C++..
Hey galik, I tried to do it the getline method. It works, but it can only get a string from a line.
However I need to get the whole list of amount and sum up together.
Which I feels that by using loop and array will be the way to go?
I tried the loop but got error..
I want to be able to add the amount, and the future amount where user can stored in the textfile.
void PCMF::test(){
string temp;
int v ;
ifstream sFile("file.txt");
vector<string> file;
while(getline(sFile,temp))
{
file.push_back(temp);
}//end loop
//major problem from this part onwards
for(int i = 0; i <=file.size() ; i++){
getline(sFile, temp, ':');
getline(sFile, temp, ':') >> v;
cout << v;
}
pause();
}//end test
Your lines 10-13 read the whole file into your vector as strings. I don't think you really want to do that.
Your first problem is to skip the first line. You can do that like this:
1 2
std::string line;
std::getline(sFile, line); // skip first line
Then for every other line, in a loop, you need to do three things:
1 2 3 4 5 6 7 8 9 10 11
// #1 Read up to and including your delimiter ":".
std::getline(sFile, line, ":"); // skip to number column
// #2 Read in just your number
int v;
sFile >> v;
// Use the number v here....
// #3 Finish reading the rest of the line
std::getline(sFile, line);
This way there is no need to read the file into a std::vector<std::string> at all. All you are doing is looking at how to read past and discard the data you don't need in order to get to the data you are looking for - on a line by line basis.