Bracketing Search Exercise Example? c++
Jun 30, 2012 at 10:05pm UTC
Is there a really good example for this one?
I've found this one
http://www.cplusplus.com/forum/beginner/72178/
, but it's struggling with 1 and 100 and it doesn't use user input(too low, too high).
I created one myself, but it doesn't look good, altough it functions properly and the only mistake is that it guesses 100 in 8, not 7 tries:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#include <iostream>
#include <time.h>
#include <string>
int main()
{
srand(time(NULL));
int k;
k=rand()%101+1;
std::cout<<"Think of a number from 1 to 100, now type it and I'll try\n" <<
"to guess it(numbers can easily change \n" <<
"in your mind, but not in mine): " ;
int b;
std::cin>>b;
int h=50;
std::cout<<"\nIf I enter a number too high, say: high, if I enter a number which is too low, \n" <<
"enter: low and if I guess it, enter: true. Here is my first try: " <<h<<"\n\n" ;
char feedback[8];
std::cin>>feedback;
int t=3;
int a=1;
do
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+25<<"?" ;
h=h+25;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-25<<"?" ;
h=h-25;
std::cin>>feedback;
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+12<<"?" ;
h=h+12;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-12<<"?" ;
h=h-12;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+6<<"?" ;
h=h+6;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-6<<"?" ;
h=h-6;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+3<<"?" ;
h=h+3;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-3<<"?" ;
h=h-3;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+2<<"?" ;
h=h+2;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-2<<"?" ;
h=h-2;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+1<<"?" ;
h=h+1;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-1<<"?" ;
h=h-1;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else if (feedback[0]=='l' ||feedback[0]=='L' )
{
std::cout<<h+1<<"?" ;
h=h+1;
std::cin>>feedback;
}
else if (feedback[0]=='h' ||feedback[0]=='H' )
{
std::cout<<h-1<<"?" ;
h=h-1;
std::cin>>feedback;
}
}
if (t!=0)
{
if (feedback[0]=='t' ||feedback[0]=='T' )
{
t=0;
}
else
{
std::cout<<"You picked a wrong number Mr!(Or you just lied to me...)\n" ;
t=0;
a=2;
}
}
}
while (t!=0);
if (a==1&&h==b)
{
std::cout<<"Woot, I guessed it!" ;
}
else if (a==2||h!=b)
{
std::cout<<"This program isn't meant for cheaters, bye!" ;
}
std::cin.ignore();
std::cin.get();
return (0);
}
Any way to do this shorter and at the same time following the instructions?(Which can be found here:
http://www.cplusplus.com/articles/N6vU7k9E/)
Jun 30, 2012 at 10:30pm UTC
Jul 1, 2012 at 8:22am UTC
Yep, that's it. Thank you!
Topic archived. No new replies allowed.