Make a line of code that will execute std::cin

I'm trying to make a "Guess the Number" game where the user picks the HiddenNumber and the AI trys to guess the number. I relized that to do this like a normal person would, I need a line of code that will replicate what a Human would do to a std::cin (Type an Answer and it will exeucte)

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
#include <iostream>
#include <string>

void AI();

int HiddenNumber;
std::string AIPrefix = "(AI) ";

int main() {
	while (true) {
		std::cout << "Pick a number 1-100 that my coded AI will try to guess: ";
		std::cin >> HiddenNumber;

		if (std::cin.fail()) {
			std::cout << "Enter an integer, try again.\n\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
		else {
			break;
		}
	}
	std::cout << "\nBeyond this point dont answer anything, as its the AI's guessing turn.\n\n";
	AI();
}

void AI() {
	std::cout << "Enter a Guess 1-100: ";
	// Line of code that replicates a user typing to a std::cin
	return;
}


Example, if the HiddenNumber is 60, and the AI types 50, then it will say to low, etc. (Gives a simpler idea to what Im aiming for)
Last edited on
Something like this, perhaps:

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
#include <iostream>
#include <thread>
#include <chrono>
#include <string>

void simulate_input( const std::string& prompt, const std::string& input )
{
    static const auto initial_delay = std::chrono::milliseconds(1000) ;
    static const auto inter_char_delay = std::chrono::milliseconds(400) ;

    std::cout << prompt << ": " ;
    std::this_thread::sleep_for(initial_delay) ;
    for( char c : input )
    {
        std::cout << c << std::flush ;
        std::this_thread::sleep_for(inter_char_delay) ;
    }

    std::cout << std::endl ;
}

void simulate_input( const std::string& prompt, int input )
{ simulate_input( prompt, std::to_string(input) ) ; }

int main()
{
    int hidden_number ;
    std::cout << "player: enter hidden number (1-100): " ;
    std::cin >> hidden_number ;

    std::cout << "its now the ai's turn...\n";

    int ai_guess = 67 ;
    simulate_input( "AI: type in a guess 1-100", ai_guess ) ;
}
Ty!
Topic archived. No new replies allowed.