#NEW TO C++

11 messages · Page 1 of 1 (latest)

past rainBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dreamy kettle
#

Hello, I have made this number guessing the code won't be the best but I am willing to improve it by my own and by you all . This code works fine until all the values given are numbers but when I try to pass an string or anything else the code doesn't gives an error but does a weird thing that I will is in the screenshot

#
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int random_number(int limit)
{
    if (limit == 0)
    {
        limit = 1;
    }
    return (rand() % limit) + 1;
}

int get_user_guess()
{
    cout << "Guess: ";
    int guess{};
    cin >> guess;
    return guess;
}

int get_limit()
{
    cout << "Define limit: ";
    int limit{};
    cin >> limit;
    return limit;
}

bool check(int number, int guess)
{
    if (guess == number)
    {
        cout << "Correct guess!" << endl;
        return true;
    }

    if (guess > number)
    {
        cout << "Wrong guess! Try guessing less!" << endl;
        return false;
    }

    if (guess < number)
    {
        cout << "Wrong guess! Try guessing more!" << endl;
        return false;
    }
}

int main()
{
    srand(time(0));
    
    int attempts = 6;
    int limit = get_limit();
    int number = random_number(limit);
    int guess = get_user_guess();
    --attempts;
    

    while (!check(number, guess))
    {
        if (attempts != 0)
        {
            guess = get_user_guess();
            --attempts;
            cout << "Remaining chances: " << attempts << endl;
        }

        if (attempts == 0)
        {
            cout << "You fail! Correct number was: " << number << endl;
            cout << "Play again? (y/n): ";
            string confirm;
            cin >> confirm;

            if (confirm == "y")
            {
                limit = get_limit();
                number = random_number(limit);
                attempts = 6;
                guess = get_user_guess();
                continue;
            }

            else
            {
                cout << "Thanks for playing!" << endl;
                break;
            }
        }
    }
}
#

This happens when I pass a non integer character

#

Whenever in the game it asks for input

#

As long as it receives a number it works as intended

past rainBOT
#

@dreamy kettle

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

#

@dreamy kettle Has your question been resolved? If so, type !solved :)

dreamy kettle
#

Man sorry i wasn't creating more

#

Its just it was giving me errors and not creating so i thought it never created this one

#

!solved