Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:16:08+00:00 2026-06-14T04:16:08+00:00

I coded an easy game. I know how to use cout/cin but having problems

  • 0

I coded an easy game. I know how to use cout/cin but having problems with printf/scanf. The code below works well with cout/cin. Question is, how to convert them into printf/scanf? Why the codes in comment don’t work?

Edit: I mean if I delete cout/cin lines and when I use printf/scanf instead it doesn’t work properly.

#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    srand(time(NULL));
    int min=0, max=1000, guess, counter=0;
    bool winner=false;
    char answer;

    while(counter<10){
        guess = rand()%(max-min)+min;
        // printf("Did you pick %d? \t", guess);
        // scanf("%c", &answer);
        cout << "Did you pick " << guess << "?\t";
        cin >> answer;
        if(answer=='y'){ // yes
            winner=true;
            // printf("Computer wins.\n");
            // printf("You picked: %d", guess);
            cout << "Computer wins." << endl;
            cout << "You picked: " << guess;
            break;
        }
        else if(answer=='m'){ // more
            min=guess;
            counter++;
        }
        else if(answer=='l'){ // less
            max=guess;
            counter++;
        }
    }
    if(winner==false){
        // printf("User wins.\n");
        cout << "User wins." << endl;
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:16:09+00:00Added an answer on June 14, 2026 at 4:16 am

    The problem is that scanf() doesn’t remove the newline ‘\n’ character from stdin, so on the next iteration the next scanf() reads it and tries to process it, seeming to ignore the input.
    Try this instead:

    scanf("\n%c", &answer);
    

    That way, you expect the newline and scanf() consumes it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's so easy to select a block of code and tab out, but how
I'm pretty sure there's some quick and easy error in this code but somehow
Is it possible to use C/C++ code in Android? And how easy is it?
I know this is very easy question, but I couldn't find the answer anywhere.
I am attempting to use canvas to make an android game, which I know
I have been working on the server code for my game. It works really
Im trying to make a quite simple game but its having an error. I
I'm trying to move my game development into the third dimension, but I'm having
When using Coded UI tests in Visual Studio 2010, is there an easy way
I am looking for something that will make it easy to run (correctly coded)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.