#GUYS HELP ME PLSSSSS

36 messages · Page 1 of 1 (latest)

scarlet light
#
#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;
}
#

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 ?

restive quiver
#

your conditions are wrong though

restive quiver
#

you have
input > 100
input < 1

scarlet light
#

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)

restive quiver
#

well

#

you do cin.clear

#

which means that it resets the fail state

scarlet light
#

i ahve cin clear and ignore

#

thats for all

restive quiver
#

which means that
if (std::cin.fail())
will always be false

scarlet light
#

ohh

#

soryy i am idot

#

ty <#

restive quiver
#

no worries

scarlet light
#

❤️

#

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 !