Re Program 19

Hello,

there was a bug in the original program so I fixed it so that it compiles.

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
 #include <stdint.h> 
#include <limits>
#include<iostream>



int main()

{


std::cout << "Please enter some floating-point values | to quit";

	double num, sum, min, max;
	sum = 0;
	min =  std::numeric_limits<int>::max();
	max =  std::numeric_limits<int>::min();

	while (std::cin >> num){
		sum += num;
		if (num > max)
			max = num;
		if (num < min)
			min = num;
	}
 
      char ' ' = 0;
      while (std::cin >> '|')
	
	std::cout << "Sum = " << sum
		<< "\nMax = " << max
		<< "\nMin = " << min << std::endl;

	return 0;
}


Now if I compile it it does not work like expected, if I enter '|' I just face the end of the program, without any sums.
Last edited on
closed account (SECMoG1T)
@pacman line 27 is an error you can't assign to a literal char ' ' = 0;
line 28 too while (std::cin >> '|') standard input cant write
to a literal.
replace that with an identifier.

edit: you dont need them remove them it would compile fine.
Last edited on
I`m still guessing what is the purpose of the program or what it should do because I can`t see any.
closed account (SECMoG1T)
well are you serious? :D, i thought you wrote it ?
i think what the program does is simply request values(double) from the user from std input
with a while loop and then finds the highest, lowest and the sum from the entries, and exists if the user enters a non integral type or a '|' , display result, that's all.
I wrote it partially. But if I enter some values and exit the program I can`t see any highest, lowest and the sum from the entries!!
closed account (SECMoG1T)
but this works
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
 #include <stdint.h>
 #include <limits>
 #include<iostream>



int main()

{


std::cout << "Please enter some floating-point values | to quit";

	double num, sum, min, max;
	sum = 0;
	min =  std::numeric_limits<int>::max();
	max =  std::numeric_limits<int>::min();

	while (std::cin >> num){
		sum += num;
		if (num > max)
			max = num;
		if (num < min)
			min = num;
	}

      ///char ' ' = 0;             this is illegal , plus you dont need it.
      ///while (std::cin >> '|')   this is illegal , 

	std::cout << "Sum = " << sum
		<< "\nMax = " << max
		<< "\nMin = " << min << std::endl;

	return 0;
}


/// you must enter arthmetic types that can be converted to double

to prevent exit when you enter a character you might consider either using
while(std::cin) or while(!std::cin.fail()) or while(std::cin.good() to check the state of you stream
and then add std::cin>>num above sum += num;

here
1
2
3
4
5
6
7
8
 while (std::cin >> num)
       {
		sum += num;
		if (num > max)
			max = num;
		if (num < min)
			min = num;
	}
Last edited on
I know it`s a known problem. I would fix it like this:

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 <stdint.h> 
#include <limits>
#include<iostream>



int main()

{


       std::cout << "Please enter some floating-point values | to quit ";

	double num, sum, min, max;
	sum = 0;
	min =  std::numeric_limits<double>::max();
	max =  std::numeric_limits<double>::min();

	while (std::cin >> num){
		sum += num;
		if (num > max)
			max = num;
		if (num < min)
			min = num;
	}

        if (!std::cin) {  // we get here only if an input operation failed
 
        if (std::cin.fail()) {   // stream encountered something unexpected
              std::cin.clear();  // make ready for more input

     
      
      
	
	std::cout << "Sum = " << sum
		<< "\nMax = " << max
		<< "\nMin = " << min << std::endl; 

         return 0;

     }

   }

}	
  




Now it btw works.
Topic archived. No new replies allowed.