Two Programs that have no errors won't work.

Neither program gives me the the print screen min, max. they do terminate when a negative number is entered.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
using namespace std;
void main()
{
int a, b, c, d, e;
cout << "\n Please enter the value of A: ";
cin >> "%d", &a;
cout << "\n Please enter the value of B: ";
cin >> "%d", &b;
cout << "\n Please enter the value of C: ";
cin >> "%d", &c;
cout << "\n Please enter the value of D: ";
cin >> "%d", &d;
cout << "\n Please enter the value of E: ";
cin >> "%d", &e;

if (a < b && a < c && a < d && a < e)
cout << ("\n %d is Minimum which is value of A", a);
else if (b < a && b < c && b < d && a < e)
cout << ("\n %d is Minimum which is value of B", b);
else if (c < a && c < b && c < d && c < e)
cout << ("\n %d is Minimum which is value of C", c);
else if (d < a && d < b && d < c && d < e)
cout << ("\n %d is Minimum which is value of D", d);
else if (e < a && e < b && e < c && e < d);
cout << ("\n %d is Minimum which is value of E", e);
return

if (a > b && a > c && a > d && a > e)
cout << "\n\n %d is Maximum which is value of A" << a;

else if (b > a && b > c && b > d && a > e)
cout << ("\n\n %d is Maximum which is value of B", b);
else if (c > a && c > b && c > d && c > e)
cout << ("\n\n %d is Minimum which is value of C", c);
else if (d > a && d > b && d > c && d > e)
cout << ("\n\n %d is Minimum which is value of D", d);
else if (e > a && e > b && e > c && e > d)
cout << ("\n\n %d is Minimum which is value of E", e);
getch();
}
[/code]

#include <iostream>
using namespace std;

void main()
{
float value, sum;
float average, minimum, maximum;
int count;
sum = 0.0;
count = 0;
cout << "Enter a value: ";
cin >> value;
minimum = value;
maximum = value;
while (value >= 0.0)
{
// process value
sum += value;
count++;
if (value > maximum)
maximum = value;
else if (value < minimum)
minimum = value;
// get next value
cout << "Enter a value: ";
cin >> value;
}
if (count == 0)
cout << "No data entry" << endl;
else
{
average = sum / count;
cout << "There were " << count << " numbers" << endl;
cout << "Average was " << average << endl;
cout << "Minimum was " << minimum << endl;
cout << "Maximum was " << maximum << endl;
}
}
javascript:PostPreview()
In the first program, it looks like you're trying to mash up printf/scanf stuff with cin/cout. Read over this, especially the standard input/output sections: http://www.cplusplus.com/doc/tutorial/basic_io/
Last edited on
Two Programs that have no errors won't work.

Well, the second program compiles and runs ok for me with MSVC but warns me that main should be returning an int not void (GCC sees this as an error.)

Also, you have to use a negative number to break out of the loop because you're using a float variable (testing floats and doubles for equality is problematic!) Other than that it runs.

Enter a value: 1
Enter a value: 2
Enter a value: 3
Enter a value: 4
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 0
Enter a value: -1
There were 8 numbers
Average was 3.5
Minimum was 0
Maximum was 7



But the first program does not compile for me with either MSVC or GCC, so I don't what compiler you're using!?!

(And having no compiler or linker errors doesn't mean you have no runtime errors!!)

Andy

PS Please use code tags. (Ideally you'd go back and reformat your old code, too.)

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
// little C++ Shell cog wheel button ---->
// (if missing try refreshing your browser)
#include <iostream>
using namespace std;

int main() {
    cout << "Hello code tags!\n";

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