Extract data from textfile to calculate amount.

Jul 15, 2011 at 4:47am
Hey guys I got a textfile, which contains both positve and negative input of the data.
Example:

Description:Amount:Transacted Date
Salary:1000:1/1/2011
Transport:-100:1/2022
Laptop Sold:2000:5/2/2010
Food:-30:6/2/2011


The - is the negative amount.

The ":" is a delimiter.
But I would only want to extract the negative number out from the text file to total it up.

Let's say (-100)+(-30)

Lets say I use a text file call file.txt.

Anyone know how to solve it? I only need a little help this then I will be able to minus the positve and the negative output.
Thx!!
Last edited on Jul 15, 2011 at 4:49am
Jul 16, 2011 at 2:34am
Hello.. Anyone knows?
Jul 16, 2011 at 4:57am
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.
Jul 16, 2011 at 6:30am
use getline(streamhere, outputstring);

then iterate through the output string

so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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:

#include <fstream>
Last edited on Jul 16, 2011 at 6:37am
Jul 16, 2011 at 12:57pm
Yea man thx I go try it out!!
Jul 16, 2011 at 5:21pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}

This is overly complicated. std::string::find_first_of will do the job for you just fine.
http://www.cplusplus.com/reference/string/string/find_first_of/
Last edited on Jul 16, 2011 at 6:39pm
Jul 16, 2011 at 5:40pm
Some useful pieces:
1
2
	std::string line;
	std::getline(ifs, line); // skip first line 

You can also use std::getline() to skip upto the ':'
 
std::getline(ifs, line, ':')

So after you skip your first line you can read your int value in like this:
1
2
int v;
std::getline(ifs, line, ':') >> v; // skip up to first ':' then read in an integer 

After reading in your int you can skip the rest of the line like this:
 
std::getline(ifs, line); // skip rest of line 

Hope that helps. std::getline() is very useful:
http://cplusplus.com/reference/string/getline/
Jul 17, 2011 at 11:03am
Thx for the info guys. But I failed with it. =( But I found another way which use string tokenizer which I'm not good at it..

My text file looks like this after some minor changes.

I-1:Salary:2000:2/10/2011:Income
I-2:Sales:10000:9/6/2011:Income
I-3:Test:300:9/11/2011:Income


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
void PCMF::test(){
    
vector<string> file;
    string temp;
    char * token;
    char * a;
    
    summary();
    
    ifstream sFile("file.txt");

    while(getline(sFile,temp))
    {
        file.push_back(temp);
    }//end loop
  
    //for(int i = 0; i > file.size(); i++){
    
     a = new char[file[0].size()];     
     a = strdup(file[0].c_str());
     token = strtok(a,"/");
     //}//end loop
     
    string value[3];
    int tokencount=0;
    while(token!=NULL){
        
    value[tokencount]=token;
        tokencount++;
        token=strtok(NULL,":");
        
    }//end loop
    cout << value[0]<<endl;
    cout << value[1]<<endl;
    cout << value[2]<<endl;

    pause();
    
}



I-1:Salary:2000:2
10/2011
Income


Output after cutting

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 = new char[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++..
Last edited on Jul 17, 2011 at 11:07am
Jul 17, 2011 at 2:49pm
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.

I-1:Salary:2000:2/10/2011:Income
I-2:Sales:10000:9/6/2011:Income
I-3:Test:300:9/11/2011:Income


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
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 
Last edited on Jul 17, 2011 at 2:51pm
Jul 17, 2011 at 4:18pm
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.
Jul 18, 2011 at 12:22pm
Solved thanks!!
One last question, how do I reached the last delimiter?

Example

Description:Amount:Transacted Date:Type
Salary:1000:1/1/2011:Personal
Transport:-100:1/2022:Personal
Laptop Sold:2000:5/2/2010:Business
Food:-30:6/2/2011:Personal
Last edited on Jul 18, 2011 at 3:06pm
Topic archived. No new replies allowed.