how to I get the data inside the for loop so that I could add it outside? Please rep ASAP :) Thanks!! (see code inside)

#include <iostream>
#include <cmath>
using namespace std;

typedef string ActType[50];


int main () {

ActType act;
int numOfact;
int ET;

cout<<"Enter the number of activity to be read: ";
cin>>numOfact;

for (int a = 0; a < numOfact; a++){
cout<<"Enter Activity: ";
cin>>a[act];
}
for (int ip = 0; ip < numOfact; ip++){
cout<<"Enter IP of "<<act[ip]<<": ";
cin>>ip[act];
}
for (int ot = 0; ot < numOfact; ot++){
cout<<"Enter OT: ";
cin>>ot;

}
for (int mt = 0; mt < numOfact; mt++){
cout<<"Enter MT: ";
cin>>mt;

}
for (int pt = 0; pt < numOfact; pt++){
cout<<"Enter PT: ";
cin>>pt;
}

ET = (ot + 4 * mt + pt) / 6; // <------------ here is the only error :|
cout<<ET;

return 0;
}



Last edited on
Your variables OT, MT & PT exist only inside the for loops. They are declared in the declaration portion and will only be valid between those curly braces.

Try assigning them whilst inside the for loop to a variable declared outside the loop, i.e.

1
2
3
4
5
6
7
int pt_real = 0;
for (int pt = 0; pt < numOfact; pt++)
{
cout << "Enter PT:";
cin >> pt;
pt_real = pt; // assign pt_real the value of pt since pt will only exist inside this loop
}


just a suggestion, i don't know if it is a valid solution but its worth a shot.
Last edited on
have you tried to compile my code? did you see the meaning of my error?
I did and the suggestion I made will allow your program to run.

Here is the code I compiled. The variables you had originally used in your ET formula were variables that were only available to the for loops in which they were declared.

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
#include <iostream>
#include <cmath>
using namespace std;

typedef string ActType[50];


int main () {

ActType act;
int numOfact;
int ET;
int ot_r = 0;
int mt_r = 0;
int pt_r = 0;

cout<<"Enter the number of activity to be read: ";
cin>>numOfact;

for (int a = 0; a < numOfact; a++){
cout<<"Enter Activity: ";
cin>>a[act];
}
for (int ip = 0; ip < numOfact; ip++){
cout<<"Enter IP of "<<act[ip]<<": ";
cin>>ip[act];
}
for (int ot = 0; ot < numOfact; ot++){
cout<<"Enter OT: ";
cin>>ot;
ot_r = ot;

}
for (int mt = 0; mt < numOfact; mt++){
cout<<"Enter MT: ";
cin>>mt;
mt_r = mt;
}
for (int pt = 0; pt < numOfact; pt++){
cout<<"Enter PT: ";
cin>>pt;
pt_r = pt;
}

ET = (ot_r + 4 * mt_r + pt_r) / 6; // <------------ here is the only error :|
cout<<ET;

return 0;
}
yeah it ran but it doesn't get the answer :| ot mt pt are in one column but et on the other hand cannot get the answer of the 1st column.
when I entered 13 in the activity, OT became infinite loop and I cannot input on mt and pt. when I input 5 on the activity OT MT and PT only have two arrays. do get what I mean? o_0
Last edited on
This program is some kind of a table. "Activity IP OT MT PT ET " <<---- one column so I need to have solution of OT MT and PT so that I could output the answer to the ET.
Wait if you are trying to make a table why are you not using arrays? Wouldn't that make more sense? An array will allow you to establish rows & columns.

EDIT: http://www.cplusplus.com/doc/tutorial/arrays/ to read up more on arrays.

i.e.
1
2
3
4
5
6
7
8
9
int pt_array[size]

for (int pt = 0; pt < numOfact; pt++)
{
cout<<"Enter PT: ";
cin>>pt_array[pt];
pt_r += pt_array[pt]; // add the value of the current index to the total of pt_r
}
Last edited on
can you give me a sample? I really don't know. :|
This is the table I was referring because this is a PERT.cpp program actually
ACTIVITY IP OT MT PT ET ES EF LS LF Slack
A none 2 3 4 3 0 3 0 3 0
B
C
D
E
F
G
H
I
J
K
L
M

the code is the one I was doing and the output on the compiler must be this..

Enter the number of activity to be read: 13
Enter Activity: A
|
|
|
|
|
|
|
|
|
Enter Activity: M <--- 13th time
Enter IP of A: none
|
|
|
|
|
|
|
|
Enter IP of M: l <--- 13th time
Enter OT: 2
|
|
|
|
|
|
|
|
|
Enter OT: 2 <--- 13th time
Enter MT: 3
|
|
|
|
|
|
|
Enter MT: 2 <--- 13th time
Enter PT: 4
|
|
|
Enter PT: 2 <--- 13th time
ET of A is 3
ET of B is 12
|
|
|
|
|
ET of M is 2 <--- 13th time

to come up of that answer in the 1st column ET = (OT + 4 * MT + PT) / 6, just ignore the rest.
I really don't get this :|
Last edited on
sorry the spacing won't work. In my table ET has the answer of 3 from the solution I gave.
I'm sorry I don't really understand what you're problem is, maybe a more informed member or someone who comprehends your issue can offer you help. I tried, remain patient, more people will respond.

Good luck!
I thank you for the help. some of it worked :) Thanks.
Here:
keep in mind that you cannot use loop counters for it.

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
#include <iostream>
#include <cmath>
using namespace std;

typedef string ActType[50];


int main () {

ActType act;
int numOfact;
int eT;

int ot1, mt1, pt1;

cout<<"Enter the number of activity to be read: ";
cin>>numOfact;

for (int a = 0; a < numOfact; a++){
cout<<"Enter Activity: ";
cin>>a[act];
}
for (int ip = 0; ip < numOfact; ip++){
cout<<"Enter IP of "<<act[ip]<<": ";
cin>>ip[act];
}
for (int ot = 0; ot < numOfact; ot++){
cout<<"Enter OT: ";
cin>>ot1;

}
for (int mt = 0; mt < numOfact; mt++){
cout<<"Enter MT: ";
cin>>mt1;

}
for (int pt = 0; pt < numOfact; pt++){
cout<<"Enter PT: ";
cin>>pt1;
}

eT = (ot1 + 4 * mt1 + pt1) / 6; // <------------ here is the only error :|
cout<<eT;

system("PAUSE");


return 0;
}
Topic archived. No new replies allowed.