#include <iostream>
#include <ctime>
#include <limits>
int main() {
srand(time(NULL));
int proby = 0;
int liczba = rand() % 100 + 1;
int input;
do {
std::cout << "Zgadnij liczbe od 1 do 100: " << '\n';
std::cin >> input;
while (std::cin.fail() || input > 100 || input < 1) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail()) {
std::cout << "Nieprawidlowy input! Podaj liczbe od 1 do 100: " << '\n';
} else if (input > 100) {
std::cout << "Podales liczbe powyzej 100! Sprobuj jeszcze raz: " << '\n';
} else if (input < 1) {
std::cout << "Podales liczbe ponizej 1! Sprobuj jeszcze raz: " << '\n';
}
std::cin >> input;
}
proby++;
} while (liczba != input);
std::cout << "Gratulacje! Udalo ci sie zgadnac liczbe w " << proby << " probie.\n";
return 0;
}
#GUYS HELP ME PLSSSSS
36 messages · Page 1 of 1 (latest)
whnever cin.fail activates
instead of oprinting
"Nieprawidlowy input! Podaj liczbe od 1 do 100: "
it prints
} else if (input < 1) {
std::cout << "Podales liczbe ponizej 1! Sprobuj jeszcze raz: " << '\n';
anyone have idea why ?
im not sure i understand the code due to the language, but im assuming youre trying to check that input is between 1 and 100
your conditions are wrong though
nope
you have
input > 100
input < 1
well basicly
its validation
if user inputs number
over 100
or less than 1
and cin fails
is for
whenever
he inouts
not supported chracter
(letter)
which means that
if (std::cin.fail())
will always be false
no worries
❤️
now it works 😄
#include <iostream>
#include <ctime>
#include <limits>
int main() {
srand(time(NULL));
int proby = 0;
int liczba = rand() % 100 + 1;
int input;
do {
std::cout << "Zgadnij liczbe od 1 do 100: " << '\n';
std::cin >> input;
while (std::cin.fail() || input > 100 || input < 1) {
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Nieprawidlowy input! Podaj liczbe od 1 do 100: " << '\n';
} else if (input > 100) {
std::cout << "Podales liczbe powyzej 100! Sprobuj jeszcze raz: " << '\n';
} else if (input < 1) {
std::cout << "Podales liczbe ponizej 1! Sprobuj jeszcze raz: " << '\n';
}
std::cin >> input;
}
proby++;
} while (liczba != input);
std::cout << "Gratulacje! Udalo ci sie zgadnac liczbe w " << proby << " probie.\n";
return 0;
}
ty so much !