error in presenting the highest and lowest value in c ++

I believe the problem lies in the if ties.

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

void valor_maior(void)
{
int a, b, c, d, e, valor_maior, valor_menor;
cout << "Digite o [A]: ";
cin >> a;
cout << "Digite o [B]: ";
cin >> b;
cout << "Digite o [C]: ";
cin >> c;
cout << "Digite o [D]: ";
cin >> d;
cout << "Digite o [E]: ";
cin >> e;
valor_maior == a;
if (valor_maior < b)
valor_maior = b;
else
if (valor_maior < c)
valor_maior = c;
else
if (valor_maior < d)
valor_maior = d;
else
if (valor_maior < e)
valor_maior = e;
cout << "Valor maior: " << valor_maior << endl;
cin.get();
return;
}

void valor_menor(void)
{
int a, b, c, d, e, valor_maior, valor_menor;
cout << "Digite o [A]: ";
cin >> a;
cout << "Digite o [B]: ";
cin >> b;
cout << "Digite o [C]: ";
cin >> c;
cout << "Digite o [D]: ";
cin >> d;
cout << "Digite o [E]: ";
cin >> e;

valor_menor = b;

if (valor_menor > b)
valor_menor = b;
else
if (valor_menor > c)
valor_menor = c;
else
if (valor_menor > d)
valor_menor = d;
else
if (valor_menor > e)
valor_menor = e;
cout << "Valor menor: " << valor_menor << endl;
cin.get();
return;
}

void pausa(void)
{
cout << endl;
cout << "Tecle <Enter> para encerrar...";
cin.get();
return;
}

int main(void)
{
valor_maior();
valor_menor();

return 0;
}

A is 11
b is 12
c is 23

valor_maior == a; // vm = 11
if (valor_maior < b) //true
valor_maior = b; //vm = 12
else //else?!
if (valor_maior < c) //skipped due to else
valor_maior = c; //skipped due to if skipped by else
Last edited on
- compile with warnings
- read the warnings
- ¿?
- profit

in valor_maior() you have
valor_maior == a;
== is comparison, not assignment

in valor_menor() you never check `a'


limit your functions to do one thing, right now you are processing user input (duplicate code), finding the max/min and printing the result all in one function.
leave i/o to main() and learn to use the parameters and return value of the functions.
easy to miss; I even copied the == and overlooked it. Let the compiler tell you these things for sure.
This is easier using std::minmax_element().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <iostream>

int main()
{
	int vals[5] = {0};

	std::cout << "Digite o [A]: ";
	std::cin >> vals[0];
	std::cout << "Digite o [B]: ";
	std::cin >> vals[1];
	std::cout << "Digite o [C]: ";
	std::cin >> vals[2];
	std::cout << "Digite o [D]: ";
	std::cin >> vals[3];
	std::cout << "Digite o [E]: ";
	std::cin >> vals[4];

	const auto nx = std::minmax_element(vals, vals + std::size(vals));

	std::cout << "Valor maior: " << *nx.second << std::endl;
	std:: cout << "Valor menor: " << *nx.first << std::endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
   int a, b, c, d, e;
   cout << "Input 5 numbers: ";
   cin >> a >> b >> c >> d >> e;
   cout << "Maximum: " << max( { a, b, c, d, e } ) << '\n';
   cout << "Minimum: " << min( { a, b, c, d, e } ) << '\n';   
}
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void valor_maior(void)
{
    int a, b, c, d, e, valor_maior;
    cout << "Digite o [A]: ";
    cin >> a;
    cout << "Digite o [B]: ";
    cin >> b;
    cout << "Digite o [C]: ";
    cin >> c;
    cout << "Digite o [D]: ";
    cin >> d;
    cout << "Digite o [E]: ";
    cin >> e;
    
    valor_maior = a;
    
    if (valor_maior < b)
        valor_maior = b;
    
    if (valor_maior < c)
            valor_maior = c;
    
    if (valor_maior < d)
                valor_maior = d;
    
    if (valor_maior < e)
                    valor_maior = e;
    
    cout << "Valor maior: " << valor_maior << endl;
    
    cin.get();
    return;
}

void valor_menor(void)
{
    int a, b, c, d, e, valor_menor;
    
    cout << "Digite o [A]: ";
    cin >> a;
    cout << "Digite o [B]: ";
    cin >> b;
    cout << "Digite o [C]: ";
    cin >> c;
    cout << "Digite o [D]: ";
    cin >> d;
    cout << "Digite o [E]: ";
    cin >> e;
    
    valor_menor = a;
    
    if (valor_menor > b)
        valor_menor = b;
    
    if (valor_menor > c)
            valor_menor = c;
    
    if (valor_menor > d)
                valor_menor = d;
    
    if (valor_menor > e)
                    valor_menor = e;
    
    cout << "Valor menor: " << valor_menor << endl;
    cin.get();
    return;
}

void pausa(void)
{
    cout << endl;
    cout << "Tecle <Enter> para encerrar...";
    cin.get();
    return;
}

int main(void)
{
    valor_maior();
    valor_menor();
    
    return 0;
}
Topic archived. No new replies allowed.