Urgent Subtracting two decimal numbers in a file

hey all

I am a c++ beginner and I am having a little trouble figuring this problem out.. hope u guys can help me out and guide me through it .... I am trying to subtract two decimal numbers declared as double in a trace file and store the result of each subtraction for summing them up later..

here's a snap shot of my trace file
s 18.112906208 _1_ MAC --- 1047 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [64 0] 0 0.
r 18.121850295 _9_ MAC --- 1047 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [64 0] 1 0.
D 18.123812660 _9_ MAC COL 0 RTS 44 [256e 9 1 0] .
D 18.130992027 _9_ MAC COL 0 RTS 44 [256e 9 1 0] .
s 18.205094269 _1_ MAC --- 1048 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [65 0] 0 0.
r 18.214038356 _9_ MAC --- 1048 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [65 0] 1 0.
s 18.229139602 _1_ MAC --- 1049 tcp 1118 [13a 9 1 800] ------- [1:1 9:0 32 9] [66 0] 0 0.
r 18.238083689 _9_ MAC --- 1049 tcp 1060 [13a 9 1 800] ------- [1:1 9:0 32 9] [66 0] 1 0.

I am always subtracting the second column in the 's' line from the 'r' line. For example
T1 = 18.121850295 - 18.112906208 =
T2= 18.214038356 - 18.205094269
time = T1 + T2 +...
I did try writing the code but I keep getting the wrong subtraction result... help would be highly appreciated ...thanx in advance


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
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main() {
    char c;
    string line;
    int numlines = 0, sum = 0, h;
    double b, time = 0.0;
    string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
    
ifstream myfile ("Spr.tr");
if(myfile.is_open()) {
while (getline(myfile,line)) {
	numlines++;
	istringstream input(line);
    input >> c >> b >> m >> d >> e >> f >> g >> h >> q >> i >> o >> j>> k;
	
   
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(9);
	cout << "b=" << b << ".\n";
        time = b- time ;
        cout << time <<  ".\n";
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}
Last edited on
Topic archived. No new replies allowed.