// Program Looping uses a count-controlled loop to read and
02
// sum 10 integer values and an event-controlled loop to
03
// read and sum values until a negative value is found.
04
// The data is on file Looping.dat.
05
06
#include <iostream>
07
#include <fstream>
08
usingnamespace std;
09
10
int main ()
11
// The data is on file Looping.dat.
12
13
#include <iostream>
14
#include <fstream>
15
usingnamespace std;
16
17
int main ()
18
{
19
ifstream inData;
20
int value;
21
int counter;
22
int sum;
23
inData.open("looping.dat");
24
25
counter = 1;
26
sum = 0;
27
dowhile (counter <= 10)
28
{ // Ten values read and summed
29
inData >> value;
30
sum = sum + value;
31
counter++;
32
}
33
cout << "The first sum is " << sum << endl;
34
35
inData >> value;
36
sum = 0;
37
while (value >= 0)
38
{ // Values are read and summed until a negative is read
39
sum = sum + value;
40
inData >> value;
41
}
42
cout << "The second sum is " << sum << endl;
43
return 0;
44
}
// Program Looping uses a count-controlled loop to read and
02
// sum 10 integer values and an event-controlled loop to
03
// read and sum values until a negative value is found.
04
// The data is on file Looping.dat.
05
06
#include <iostream>
07
#include <fstream>
08
usingnamespace std;
09
10
int main ()
11
// The data is on file Looping.dat.
12
13
#include <iostream>
14
#include <fstream>
15
usingnamespace std;
16
17
int main ()
18
{
19
ifstream inData;
20
int value;
21
int counter;
22
int sum;
23
inData.open("looping.dat");
24
25
counter = 1;
26
sum = 0;
27
do{
while (counter <= 10)
28
{ // Ten values read and summed
29
inData >> value;
30
sum = sum + value;
31
counter++;
32
}
33
cout << "The first sum is " << sum << endl;
34
35
inData >> value;
36
sum = 0;
37
while (value >= 0)
38
{ // Values are read and summed until a negative is read
39
sum = sum + value;
40
inData >> value;
41
}
42
cout << "The second sum is " << sum << endl;
43
return 0;
44
}
is this correct?
my ques. is that the instructor asks me to change while statements to do-while statements and for statements as well. also, for statements implement the counter control ?
// Program Looping uses a count-controlled loop to read and
// sum 10 integer values and an event-controlled loop to
// read and sum values until a negative value is found.
// The data is on file Looping.dat.
#include <iostream>
#include <fstream>
int main()
{
int value;
int counter = 1;
int sum = 0;
std::ifstream inData;
inData.open("looping.dat");
while (counter <= 10)
{ // Ten values read and summed
inData >> value;
sum = sum + value;
counter++;
}
std::cout << "The first sum is " << sum << std::endl;
inData >> value;
while (value >= 0)
{ // Values are read and summed until a negative is read
sum = sum + value;
inData >> value;
}
inData.close();
std::cout << "The second sum is " << sum << std::endl;
return 0;
}