C++ number-guessing game (computer tries to guess user's chosen number)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
7
down vote

favorite












In this program we input a number. Our PC tries to guess this number.



After every try PC asks us:"Are your number more or less than?".



We input 'l' if our number is less, and input 'h' if our number is greater?



A range of possible values ​​is created.



I think this code is bad. What do you think?



#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max)
return min + rand() % (max - min);


int main()

setlocale(LC_ALL, "rus");
int our_num;
srand(static_cast<unsigned int>(time(0)));
cout << "Input a positive number: " << endl;
cin >> our_num;
int t = 0; //number of attempts
int max = rand() + our_num; //maximum possible value
int min = 0; //minimum possible value
int d = Random(min, max);
do
char lh;
cout << d << " Is this your number?(my number is greater: 'h'; less: 'l')" << endl;
cin >> lh;
++t;
if (lh == 'h')
min = d;
d = Random(min, max);

else if (lh == 'l')
max = d;
d = Random(min, max);

while (d != our_num);
cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
return 0;







share|improve this question





















  • Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
    – A. Romeu
    Mar 4 at 18:47






  • 1




    @A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
    – yuri
    Mar 4 at 18:50






  • 1




    Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
    – A. Romeu
    Mar 4 at 19:03






  • 1




    One tip for improving the searching is to use binary search.
    – NoOneIsHere
    Mar 4 at 21:06






  • 3




    The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
    – Davislor
    Mar 4 at 22:05
















up vote
7
down vote

favorite












In this program we input a number. Our PC tries to guess this number.



After every try PC asks us:"Are your number more or less than?".



We input 'l' if our number is less, and input 'h' if our number is greater?



A range of possible values ​​is created.



I think this code is bad. What do you think?



#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max)
return min + rand() % (max - min);


int main()

setlocale(LC_ALL, "rus");
int our_num;
srand(static_cast<unsigned int>(time(0)));
cout << "Input a positive number: " << endl;
cin >> our_num;
int t = 0; //number of attempts
int max = rand() + our_num; //maximum possible value
int min = 0; //minimum possible value
int d = Random(min, max);
do
char lh;
cout << d << " Is this your number?(my number is greater: 'h'; less: 'l')" << endl;
cin >> lh;
++t;
if (lh == 'h')
min = d;
d = Random(min, max);

else if (lh == 'l')
max = d;
d = Random(min, max);

while (d != our_num);
cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
return 0;







share|improve this question





















  • Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
    – A. Romeu
    Mar 4 at 18:47






  • 1




    @A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
    – yuri
    Mar 4 at 18:50






  • 1




    Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
    – A. Romeu
    Mar 4 at 19:03






  • 1




    One tip for improving the searching is to use binary search.
    – NoOneIsHere
    Mar 4 at 21:06






  • 3




    The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
    – Davislor
    Mar 4 at 22:05












up vote
7
down vote

favorite









up vote
7
down vote

favorite











In this program we input a number. Our PC tries to guess this number.



After every try PC asks us:"Are your number more or less than?".



We input 'l' if our number is less, and input 'h' if our number is greater?



A range of possible values ​​is created.



I think this code is bad. What do you think?



#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max)
return min + rand() % (max - min);


int main()

setlocale(LC_ALL, "rus");
int our_num;
srand(static_cast<unsigned int>(time(0)));
cout << "Input a positive number: " << endl;
cin >> our_num;
int t = 0; //number of attempts
int max = rand() + our_num; //maximum possible value
int min = 0; //minimum possible value
int d = Random(min, max);
do
char lh;
cout << d << " Is this your number?(my number is greater: 'h'; less: 'l')" << endl;
cin >> lh;
++t;
if (lh == 'h')
min = d;
d = Random(min, max);

else if (lh == 'l')
max = d;
d = Random(min, max);

while (d != our_num);
cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
return 0;







share|improve this question













In this program we input a number. Our PC tries to guess this number.



After every try PC asks us:"Are your number more or less than?".



We input 'l' if our number is less, and input 'h' if our number is greater?



A range of possible values ​​is created.



I think this code is bad. What do you think?



#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int Random(int min, int max)
return min + rand() % (max - min);


int main()

setlocale(LC_ALL, "rus");
int our_num;
srand(static_cast<unsigned int>(time(0)));
cout << "Input a positive number: " << endl;
cin >> our_num;
int t = 0; //number of attempts
int max = rand() + our_num; //maximum possible value
int min = 0; //minimum possible value
int d = Random(min, max);
do
char lh;
cout << d << " Is this your number?(my number is greater: 'h'; less: 'l')" << endl;
cin >> lh;
++t;
if (lh == 'h')
min = d;
d = Random(min, max);

else if (lh == 'l')
max = d;
d = Random(min, max);

while (d != our_num);
cout << "I guessed thus number with " << t << " attempts. " << "This number is " << d << endl;
return 0;









share|improve this question












share|improve this question




share|improve this question








edited Mar 4 at 19:00









200_success

123k14142399




123k14142399









asked Mar 4 at 18:41









Bogdasar

385




385











  • Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
    – A. Romeu
    Mar 4 at 18:47






  • 1




    @A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
    – yuri
    Mar 4 at 18:50






  • 1




    Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
    – A. Romeu
    Mar 4 at 19:03






  • 1




    One tip for improving the searching is to use binary search.
    – NoOneIsHere
    Mar 4 at 21:06






  • 3




    The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
    – Davislor
    Mar 4 at 22:05
















  • Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
    – A. Romeu
    Mar 4 at 18:47






  • 1




    @A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
    – yuri
    Mar 4 at 18:50






  • 1




    Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
    – A. Romeu
    Mar 4 at 19:03






  • 1




    One tip for improving the searching is to use binary search.
    – NoOneIsHere
    Mar 4 at 21:06






  • 3




    The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
    – Davislor
    Mar 4 at 22:05















Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
– A. Romeu
Mar 4 at 18:47




Hey Bogdasar, would you mind answering why specifically you believe this code is bad? Cheers!
– A. Romeu
Mar 4 at 18:47




1




1




@A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
– yuri
Mar 4 at 18:50




@A.Romeu Have you never written something and had that terrible feeling that it's bad but couldn't quite say why?
– yuri
Mar 4 at 18:50




1




1




Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
– A. Romeu
Mar 4 at 19:03




Sure, a thousand times! But maybe there is some part you believe it can be definitely better for a reason, or something
– A. Romeu
Mar 4 at 19:03




1




1




One tip for improving the searching is to use binary search.
– NoOneIsHere
Mar 4 at 21:06




One tip for improving the searching is to use binary search.
– NoOneIsHere
Mar 4 at 21:06




3




3




The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
– Davislor
Mar 4 at 22:05




The "rus" locale would not be portable. You are probably better off setting the locale to "" and having the user specify their preferred locale in the environment. However, in this example, you’re doing all the output in English anyway.
– Davislor
Mar 4 at 22:05










2 Answers
2






active

oldest

votes

















up vote
13
down vote



accepted










Some things stand out here:



  • Don't use using namespace std


  • Consider not using rand()


  • Prefer using n over std::endl


  • return 0 can be dropped from main as it's optional


  • You really need better variable names. Names should explain what the variable is used for. If you pick better names your superfluous comments can be removed alltogether






share|improve this answer



















  • 2




    Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
    – cmh
    Mar 4 at 21:21






  • 2




    I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
    – Davislor
    Mar 4 at 22:10







  • 1




    @Yuri you have basically invalidated how I learned to write C++ code
    – Matthew Barclay
    Mar 5 at 3:34

















up vote
3
down vote













Some considerations on user experience rather than programming style:




cout << "Input a positive number: " << endl;
cin >> our_num;



Why user should enter his number before the computer start guessing?? That's not a guessing game.




int max = rand() + our_num; //maximum possible value




should be the maximum number can be stored in an int (or give a range to the user ("choose a number between __ and __"))




" Is this your number?(my number is greater: 'h'; less: 'l')"




I really miss the option when the guess is correct.




int Random(int min, int max) 
return min + rand() % (max - min);




I think it's really a good idea to put some variability in guessing instead of always going for boring binary search.






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188807%2fc-number-guessing-game-computer-tries-to-guess-users-chosen-number%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    13
    down vote



    accepted










    Some things stand out here:



    • Don't use using namespace std


    • Consider not using rand()


    • Prefer using n over std::endl


    • return 0 can be dropped from main as it's optional


    • You really need better variable names. Names should explain what the variable is used for. If you pick better names your superfluous comments can be removed alltogether






    share|improve this answer



















    • 2




      Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
      – cmh
      Mar 4 at 21:21






    • 2




      I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
      – Davislor
      Mar 4 at 22:10







    • 1




      @Yuri you have basically invalidated how I learned to write C++ code
      – Matthew Barclay
      Mar 5 at 3:34














    up vote
    13
    down vote



    accepted










    Some things stand out here:



    • Don't use using namespace std


    • Consider not using rand()


    • Prefer using n over std::endl


    • return 0 can be dropped from main as it's optional


    • You really need better variable names. Names should explain what the variable is used for. If you pick better names your superfluous comments can be removed alltogether






    share|improve this answer



















    • 2




      Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
      – cmh
      Mar 4 at 21:21






    • 2




      I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
      – Davislor
      Mar 4 at 22:10







    • 1




      @Yuri you have basically invalidated how I learned to write C++ code
      – Matthew Barclay
      Mar 5 at 3:34












    up vote
    13
    down vote



    accepted







    up vote
    13
    down vote



    accepted






    Some things stand out here:



    • Don't use using namespace std


    • Consider not using rand()


    • Prefer using n over std::endl


    • return 0 can be dropped from main as it's optional


    • You really need better variable names. Names should explain what the variable is used for. If you pick better names your superfluous comments can be removed alltogether






    share|improve this answer















    Some things stand out here:



    • Don't use using namespace std


    • Consider not using rand()


    • Prefer using n over std::endl


    • return 0 can be dropped from main as it's optional


    • You really need better variable names. Names should explain what the variable is used for. If you pick better names your superfluous comments can be removed alltogether







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Mar 5 at 4:01









    Grant Garrison

    229215




    229215











    answered Mar 4 at 18:57









    yuri

    3,3862832




    3,3862832







    • 2




      Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
      – cmh
      Mar 4 at 21:21






    • 2




      I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
      – Davislor
      Mar 4 at 22:10







    • 1




      @Yuri you have basically invalidated how I learned to write C++ code
      – Matthew Barclay
      Mar 5 at 3:34












    • 2




      Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
      – cmh
      Mar 4 at 21:21






    • 2




      I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
      – Davislor
      Mar 4 at 22:10







    • 1




      @Yuri you have basically invalidated how I learned to write C++ code
      – Matthew Barclay
      Mar 5 at 3:34







    2




    2




    Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
    – cmh
    Mar 4 at 21:21




    Elaborating on your final point, it's best not to use min and max potentially confusing since OP has brought std::min/max into the namespace.
    – cmh
    Mar 4 at 21:21




    2




    2




    I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
    – Davislor
    Mar 4 at 22:10





    I personally prefer to return EXIT_SUCCESS; from main(), albeit largely because that was the only correct way to do it when I was learning the language. I still prefer the consistency of not using the special-case rules for main(), and treating it like any other function that returns an int.
    – Davislor
    Mar 4 at 22:10





    1




    1




    @Yuri you have basically invalidated how I learned to write C++ code
    – Matthew Barclay
    Mar 5 at 3:34




    @Yuri you have basically invalidated how I learned to write C++ code
    – Matthew Barclay
    Mar 5 at 3:34












    up vote
    3
    down vote













    Some considerations on user experience rather than programming style:




    cout << "Input a positive number: " << endl;
    cin >> our_num;



    Why user should enter his number before the computer start guessing?? That's not a guessing game.




    int max = rand() + our_num; //maximum possible value




    should be the maximum number can be stored in an int (or give a range to the user ("choose a number between __ and __"))




    " Is this your number?(my number is greater: 'h'; less: 'l')"




    I really miss the option when the guess is correct.




    int Random(int min, int max) 
    return min + rand() % (max - min);




    I think it's really a good idea to put some variability in guessing instead of always going for boring binary search.






    share|improve this answer

























      up vote
      3
      down vote













      Some considerations on user experience rather than programming style:




      cout << "Input a positive number: " << endl;
      cin >> our_num;



      Why user should enter his number before the computer start guessing?? That's not a guessing game.




      int max = rand() + our_num; //maximum possible value




      should be the maximum number can be stored in an int (or give a range to the user ("choose a number between __ and __"))




      " Is this your number?(my number is greater: 'h'; less: 'l')"




      I really miss the option when the guess is correct.




      int Random(int min, int max) 
      return min + rand() % (max - min);




      I think it's really a good idea to put some variability in guessing instead of always going for boring binary search.






      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        Some considerations on user experience rather than programming style:




        cout << "Input a positive number: " << endl;
        cin >> our_num;



        Why user should enter his number before the computer start guessing?? That's not a guessing game.




        int max = rand() + our_num; //maximum possible value




        should be the maximum number can be stored in an int (or give a range to the user ("choose a number between __ and __"))




        " Is this your number?(my number is greater: 'h'; less: 'l')"




        I really miss the option when the guess is correct.




        int Random(int min, int max) 
        return min + rand() % (max - min);




        I think it's really a good idea to put some variability in guessing instead of always going for boring binary search.






        share|improve this answer













        Some considerations on user experience rather than programming style:




        cout << "Input a positive number: " << endl;
        cin >> our_num;



        Why user should enter his number before the computer start guessing?? That's not a guessing game.




        int max = rand() + our_num; //maximum possible value




        should be the maximum number can be stored in an int (or give a range to the user ("choose a number between __ and __"))




        " Is this your number?(my number is greater: 'h'; less: 'l')"




        I really miss the option when the guess is correct.




        int Random(int min, int max) 
        return min + rand() % (max - min);




        I think it's really a good idea to put some variability in guessing instead of always going for boring binary search.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Mar 5 at 9:45









        Máté Juhász

        471212




        471212






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188807%2fc-number-guessing-game-computer-tries-to-guess-users-chosen-number%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods