loops

hey all
I have written a code where I read different ranges from a file and then perform a specific operation within each range I read. My code is really long as I am required to read 15 intervals I know in such cases that I need to use a for loop because the process is being repeated but somehow I am stuck plz guide me through it

here's my code... 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <iomanip>

using namespace std;

int main() {
    char c;
    string line;
    int numlines = 0, sum1 = 0,sum2 =0, sum3 =0 ,num, h,ze;
    double b, S, T,time = 0.0;
    string m,d,e,f,g,q,i,o,j,k,l,y,n,p,r,z,x,w;
    double Tp = 0.0, Thput = 0.0;
    double time1 = 0.0, time2 = 0.0, time3 =0.0; 
    double Throughput = 0.0;  
    double Te[2];
   
ifstream myfile ("RN1-9.tr");
ofstream MYFILE("Tjr.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;

if(b > 10.0 && b <= 20.0) {
	
	if (c== 'r')
		sum1 = sum1 + h;

	
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time1 = time1 + S;
		break;	
	}	

}

if(b > 20.0 && b <= 30.0) {
	
	if (c== 'r')
		sum2 = sum2 + h;
	
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time2 = time2 + S;
		break;	
	}	
}

if (b > 30.0 && b <= 40.0) {
		
	if (c== 'r')
		sum3 = sum3 + h;
		
	switch (c)
	{
		case 's':
		T = b;
		break;
		
		case 'r' :
		S = b- T;
		time3 = time3 + S;
		break;	
	}	

}

}
	
Tp = sum1 * 8;
double tt;
tt = Tp /(time1); 
Te[0] = tt/1000000;
MYFILE << fixed <<  setprecision(9); 
MYFILE << "10 " << " " << Te[0]<<".\n";

Tp = sum2 * 8;
double TTT;
TTT = Tp /(time2); 
Te[1] = TTT/1000000;
MYFILE << fixed <<  setprecision(9); 
MYFILE << "20 " << " " << Te[1] <<".\n";

Tp = sum3 * 8;
double Ttt;
Ttt = Tp/(time3); 
Te[2] = Ttt/1000000;
MYFILE<< fixed <<  setprecision(9);      
MYFILE << "30 " << " " << Te[2] << ".\n";

for (num =0; num<=2; num++){
	 Throughput+= Te[num];
 }
 
 
double average =0.0;
	cout << "num" << num << ".\n";
	average = Throughput /num ; 
	
cout << fixed << setprecision(9);
cout <<  "Average Throughput" <<  average << ".\n"; 
	
myfile.close();
}

else cout << "Unable to open file";


return 0;
}

  
Topic archived. No new replies allowed.