Computer Guesses Your Number












-6














I need to make a "Guessing game" but I'm stuck because I'm not sure if this is the right way to make it, since it's so hard to actually find the number I'm thinking of when playing the game.



#include <iostream>
#include <string>
using namespace std;
int main ()
{
int guess;
int n=500;
cout<<"Think of a number from 1 to 1000."<<endl;
cout<<"The number is : 1. 500"<<endl;
cout<<"2. Bigger than 500"<<endl;
cout<<"3. Smaller than 500"<<endl;
cin>>guess;

while(n<=2000)
{
if(guess==1)
{
cout<<"The computer has guessed the number!";
break;
}
else if(guess==2)
{
n+=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
else if(guess==3)
{
n-=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
}
}


I can't seem to find a better way to make the game. The number always goes over 1000 so I had to set the while to under 2000 for it to work.



It would be good if I could set the n+=n/2 formula so that it splits the last number that was the n variable. For example : 500+(500/2), then 750+(250/2), then 875+(125/2) (not sure how it would continue when the 125 will get split into 62.5) or the same with the n-=n/2.










share|improve this question




















  • 4




    if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
    – user4581301
    Nov 23 '18 at 22:48












  • @edlars47. Read this example: guessing game for inspiration
    – eapetcho
    Nov 23 '18 at 22:57






  • 2




    This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
    – Lightness Races in Orbit
    Nov 23 '18 at 23:04










  • Look up "binary search". You're on the right track but your implementation is flawed.
    – Fei Xiang
    Nov 23 '18 at 23:29


















-6














I need to make a "Guessing game" but I'm stuck because I'm not sure if this is the right way to make it, since it's so hard to actually find the number I'm thinking of when playing the game.



#include <iostream>
#include <string>
using namespace std;
int main ()
{
int guess;
int n=500;
cout<<"Think of a number from 1 to 1000."<<endl;
cout<<"The number is : 1. 500"<<endl;
cout<<"2. Bigger than 500"<<endl;
cout<<"3. Smaller than 500"<<endl;
cin>>guess;

while(n<=2000)
{
if(guess==1)
{
cout<<"The computer has guessed the number!";
break;
}
else if(guess==2)
{
n+=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
else if(guess==3)
{
n-=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
}
}


I can't seem to find a better way to make the game. The number always goes over 1000 so I had to set the while to under 2000 for it to work.



It would be good if I could set the n+=n/2 formula so that it splits the last number that was the n variable. For example : 500+(500/2), then 750+(250/2), then 875+(125/2) (not sure how it would continue when the 125 will get split into 62.5) or the same with the n-=n/2.










share|improve this question




















  • 4




    if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
    – user4581301
    Nov 23 '18 at 22:48












  • @edlars47. Read this example: guessing game for inspiration
    – eapetcho
    Nov 23 '18 at 22:57






  • 2




    This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
    – Lightness Races in Orbit
    Nov 23 '18 at 23:04










  • Look up "binary search". You're on the right track but your implementation is flawed.
    – Fei Xiang
    Nov 23 '18 at 23:29
















-6












-6








-6







I need to make a "Guessing game" but I'm stuck because I'm not sure if this is the right way to make it, since it's so hard to actually find the number I'm thinking of when playing the game.



#include <iostream>
#include <string>
using namespace std;
int main ()
{
int guess;
int n=500;
cout<<"Think of a number from 1 to 1000."<<endl;
cout<<"The number is : 1. 500"<<endl;
cout<<"2. Bigger than 500"<<endl;
cout<<"3. Smaller than 500"<<endl;
cin>>guess;

while(n<=2000)
{
if(guess==1)
{
cout<<"The computer has guessed the number!";
break;
}
else if(guess==2)
{
n+=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
else if(guess==3)
{
n-=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
}
}


I can't seem to find a better way to make the game. The number always goes over 1000 so I had to set the while to under 2000 for it to work.



It would be good if I could set the n+=n/2 formula so that it splits the last number that was the n variable. For example : 500+(500/2), then 750+(250/2), then 875+(125/2) (not sure how it would continue when the 125 will get split into 62.5) or the same with the n-=n/2.










share|improve this question















I need to make a "Guessing game" but I'm stuck because I'm not sure if this is the right way to make it, since it's so hard to actually find the number I'm thinking of when playing the game.



#include <iostream>
#include <string>
using namespace std;
int main ()
{
int guess;
int n=500;
cout<<"Think of a number from 1 to 1000."<<endl;
cout<<"The number is : 1. 500"<<endl;
cout<<"2. Bigger than 500"<<endl;
cout<<"3. Smaller than 500"<<endl;
cin>>guess;

while(n<=2000)
{
if(guess==1)
{
cout<<"The computer has guessed the number!";
break;
}
else if(guess==2)
{
n+=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
else if(guess==3)
{
n-=n/2;
cout<<"The number is :n1."<<n<<endl;
cout<<"2. Bigger than "<<n<<endl;
cout<<"3. Smaller than "<<n<<endl;
cin>>guess;
}
}
}


I can't seem to find a better way to make the game. The number always goes over 1000 so I had to set the while to under 2000 for it to work.



It would be good if I could set the n+=n/2 formula so that it splits the last number that was the n variable. For example : 500+(500/2), then 750+(250/2), then 875+(125/2) (not sure how it would continue when the 125 will get split into 62.5) or the same with the n-=n/2.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:50









bcperth

2,0011514




2,0011514










asked Nov 23 '18 at 22:42









edlars47edlars47

32




32








  • 4




    if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
    – user4581301
    Nov 23 '18 at 22:48












  • @edlars47. Read this example: guessing game for inspiration
    – eapetcho
    Nov 23 '18 at 22:57






  • 2




    This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
    – Lightness Races in Orbit
    Nov 23 '18 at 23:04










  • Look up "binary search". You're on the right track but your implementation is flawed.
    – Fei Xiang
    Nov 23 '18 at 23:29
















  • 4




    if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
    – user4581301
    Nov 23 '18 at 22:48












  • @edlars47. Read this example: guessing game for inspiration
    – eapetcho
    Nov 23 '18 at 22:57






  • 2




    This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
    – Lightness Races in Orbit
    Nov 23 '18 at 23:04










  • Look up "binary search". You're on the right track but your implementation is flawed.
    – Fei Xiang
    Nov 23 '18 at 23:29










4




4




if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
– user4581301
Nov 23 '18 at 22:48






if the number is known to be over 500, you never want to select a 500 or less ever again. Store a min and a max and keep picking numbers between the min and max, moving the min or max when the user provides new information.
– user4581301
Nov 23 '18 at 22:48














@edlars47. Read this example: guessing game for inspiration
– eapetcho
Nov 23 '18 at 22:57




@edlars47. Read this example: guessing game for inspiration
– eapetcho
Nov 23 '18 at 22:57




2




2




This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
– Lightness Races in Orbit
Nov 23 '18 at 23:04




This isn't a C++ problem, it's a logic problem. I recommend pen, paper and patience!
– Lightness Races in Orbit
Nov 23 '18 at 23:04












Look up "binary search". You're on the right track but your implementation is flawed.
– Fei Xiang
Nov 23 '18 at 23:29






Look up "binary search". You're on the right track but your implementation is flawed.
– Fei Xiang
Nov 23 '18 at 23:29














1 Answer
1






active

oldest

votes


















0














#include <iostream>
using namespace std;

int main()
{
int guess;
int min = 0, current = 500, max = 1000;
cout << "Think of a number from 1 to 1000." << endl;
cout << "The number is : 1. 500" << endl;
cout << "2. Bigger than 500" << endl;
cout << "3. Smaller than 500" << endl;
cin >> guess;
while (current <= 2000)
{
if (guess == 1)
{
cout << "The computer has guessed the number!";
break;
}
else if (guess == 2)
{
min = current;
current = (min + max) / 2;

cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
else if (guess == 3)
{
max = current;
current = (min + max) / 2;
cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
}
}


This should work. the min and max effectively restrict the domain after each guessing.






share|improve this answer























  • also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
    – Richard w
    Nov 23 '18 at 23:32










  • while (current <= 2000) is useless as an exit condition.
    – user4581301
    Nov 24 '18 at 0:48










  • yep, forgot to delete that
    – Richard w
    Nov 24 '18 at 3:01











Your Answer






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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453606%2fcomputer-guesses-your-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














#include <iostream>
using namespace std;

int main()
{
int guess;
int min = 0, current = 500, max = 1000;
cout << "Think of a number from 1 to 1000." << endl;
cout << "The number is : 1. 500" << endl;
cout << "2. Bigger than 500" << endl;
cout << "3. Smaller than 500" << endl;
cin >> guess;
while (current <= 2000)
{
if (guess == 1)
{
cout << "The computer has guessed the number!";
break;
}
else if (guess == 2)
{
min = current;
current = (min + max) / 2;

cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
else if (guess == 3)
{
max = current;
current = (min + max) / 2;
cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
}
}


This should work. the min and max effectively restrict the domain after each guessing.






share|improve this answer























  • also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
    – Richard w
    Nov 23 '18 at 23:32










  • while (current <= 2000) is useless as an exit condition.
    – user4581301
    Nov 24 '18 at 0:48










  • yep, forgot to delete that
    – Richard w
    Nov 24 '18 at 3:01
















0














#include <iostream>
using namespace std;

int main()
{
int guess;
int min = 0, current = 500, max = 1000;
cout << "Think of a number from 1 to 1000." << endl;
cout << "The number is : 1. 500" << endl;
cout << "2. Bigger than 500" << endl;
cout << "3. Smaller than 500" << endl;
cin >> guess;
while (current <= 2000)
{
if (guess == 1)
{
cout << "The computer has guessed the number!";
break;
}
else if (guess == 2)
{
min = current;
current = (min + max) / 2;

cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
else if (guess == 3)
{
max = current;
current = (min + max) / 2;
cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
}
}


This should work. the min and max effectively restrict the domain after each guessing.






share|improve this answer























  • also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
    – Richard w
    Nov 23 '18 at 23:32










  • while (current <= 2000) is useless as an exit condition.
    – user4581301
    Nov 24 '18 at 0:48










  • yep, forgot to delete that
    – Richard w
    Nov 24 '18 at 3:01














0












0








0






#include <iostream>
using namespace std;

int main()
{
int guess;
int min = 0, current = 500, max = 1000;
cout << "Think of a number from 1 to 1000." << endl;
cout << "The number is : 1. 500" << endl;
cout << "2. Bigger than 500" << endl;
cout << "3. Smaller than 500" << endl;
cin >> guess;
while (current <= 2000)
{
if (guess == 1)
{
cout << "The computer has guessed the number!";
break;
}
else if (guess == 2)
{
min = current;
current = (min + max) / 2;

cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
else if (guess == 3)
{
max = current;
current = (min + max) / 2;
cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
}
}


This should work. the min and max effectively restrict the domain after each guessing.






share|improve this answer














#include <iostream>
using namespace std;

int main()
{
int guess;
int min = 0, current = 500, max = 1000;
cout << "Think of a number from 1 to 1000." << endl;
cout << "The number is : 1. 500" << endl;
cout << "2. Bigger than 500" << endl;
cout << "3. Smaller than 500" << endl;
cin >> guess;
while (current <= 2000)
{
if (guess == 1)
{
cout << "The computer has guessed the number!";
break;
}
else if (guess == 2)
{
min = current;
current = (min + max) / 2;

cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
else if (guess == 3)
{
max = current;
current = (min + max) / 2;
cout << "The number is :n." << current << endl;
cout << "2. Bigger than " << current << endl;
cout << "3. Smaller than " << current << endl;
cin >> guess;
}
}
}


This should work. the min and max effectively restrict the domain after each guessing.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 0:48









user4581301

19.6k51831




19.6k51831










answered Nov 23 '18 at 23:31









Richard wRichard w

16




16












  • also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
    – Richard w
    Nov 23 '18 at 23:32










  • while (current <= 2000) is useless as an exit condition.
    – user4581301
    Nov 24 '18 at 0:48










  • yep, forgot to delete that
    – Richard w
    Nov 24 '18 at 3:01


















  • also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
    – Richard w
    Nov 23 '18 at 23:32










  • while (current <= 2000) is useless as an exit condition.
    – user4581301
    Nov 24 '18 at 0:48










  • yep, forgot to delete that
    – Richard w
    Nov 24 '18 at 3:01
















also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
– Richard w
Nov 23 '18 at 23:32




also, int variable doesn't have decimals. it will round if an odd number is divided by 2.
– Richard w
Nov 23 '18 at 23:32












while (current <= 2000) is useless as an exit condition.
– user4581301
Nov 24 '18 at 0:48




while (current <= 2000) is useless as an exit condition.
– user4581301
Nov 24 '18 at 0:48












yep, forgot to delete that
– Richard w
Nov 24 '18 at 3:01




yep, forgot to delete that
– Richard w
Nov 24 '18 at 3:01


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453606%2fcomputer-guesses-your-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)