0
I was doing an exercise in C++ that takes the smallest and the largest number typed by the user, I tried to leave something more automatic, where he goes typing until he doesn’t want more. So I’m trying to define that if the user type 'k' for example, the code stops and checks is done only with the numbers that were typed before. and if it enters another letter other than the 'k' that is for the output, the program informs that n is a valid value.
I was able to make the code, and it was working, but I couldn’t get past this step of recognizing k or another letter, since the array that receives user values is int/float. and the character 'k' is of the char type, then gives error in the comparison.
Is there a way to make comparisons between different variable types? or convert, something? I’m still beginner in C++
float valor[20];
float menor;
float maior{ 0.0 };
std::cout << "Informe os valores para vermos qual é o menor e maior numero!\n(Maximo permitido são 20 valores)(pressione K para encerrar)\n";
for (int i = 0; i < 19; i++) {
std::cin >> valor[i];
menor = valor[0];
if (valor[i] == 'k') {
i = 20;
}
menor = valor[i]<=menor?valor[i]:menor;
maior = valor[i]>maior?valor[i]:maior;
system("cls || clean");
}
std::cout << "\nO menor numero digitado foi: " << menor;
std::cout << "\nO maior numero digitado foi: \n" << maior;
Please add the code you produced so that someone can better understand the context and help you.
– Valmor Flores
added the code
– HvCeaser
You can read the line with
getline
and check its contents as a character and, if applicable, convert the read string to a float using the functionstof
. To not restrict the values that can be informed also use the initial value of higher as the first number read.– anonimo