• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 10:28
CET 16:28
KST 00:28
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
RSL Season 3: RO16 results & RO8 bracket13Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge2[TLMC] Fall/Winter 2025 Ladder Map Rotation14Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA17
StarCraft 2
General
SC: Evo Complete - Ranked Ladder OPEN ALPHA Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge RSL Season 3: RO16 results & RO8 bracket RSL Season 3 - Playoffs Preview Mech is the composition that needs teleportation t
Tourneys
RSL Revival: Season 3 $5,000+ WardiTV 2025 Championship StarCraft Evolution League (SC Evo Biweekly) Constellation Cup - Main Event - Stellar Fest 2025 RSL Offline Finals Dates + Ticket Sales!
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death
Brood War
General
Data analysis on 70 million replays What happened to TvZ on Retro? soO on: FanTaSy's Potential Return to StarCraft 2v2 maps which are SC2 style with teams together? BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL21] RO16 Tie Breaker - Group B - Sun 21:00 CET [BSL21] RO16 Tie Breaker - Group A - Sat 21:00 CET Small VOD Thread 2.0
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread Should offensive tower rushing be viable in RTS games? Clair Obscur - Expedition 33
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread The Games Industry And ATVI Things Aren’t Peaceful in Palestine About SC2SEA.COM
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
TL Community
The Automated Ban List
Blogs
The Health Impact of Joining…
TrAiDoS
Dyadica Evangelium — Chapt…
Hildegard
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2105 users

Anyone decent with C++ (Need basic help!)

Blogs > renixian
Post a Reply
renixian
Profile Blog Joined February 2010
United States50 Posts
Last Edited: 2010-10-07 12:44:09
October 07 2010 12:40 GMT
#1
I'm trying to do a code that the program "guesses" your number between 1-100 then lists the number of attempts it took to achieve that. But I can't seem to get my coding to work, I'm quite new to this so I'm sure I made multiple mistakes but I can't see them at the moment nor does VStudios tell me :[


The major issue I'm currently having is that it's not changing the variable guess to the estimate, keep thinking that it's still 100+1/2 so constantly outputs 50. I'm sure I'm missing a way to change the constant within the switch or I'm missing a loop but I don't see it right now :[

Any help/advice would be greatly appreciated!

The code is as follows


#include <iostream>
using namespace std;

int main()
{
int low = 1, high = 100;
bool correct = false;
int guess = (high + low) / 2;
int response;
int counter=1;

cout << "Think of a number between 1 and 100\n";
cout << "tell me if I'm too high(1), too low(2), or if I'm correct(3)\n";

do
{
cout << "My guess is " << guess << " ,is it..\n";
cout << "1 - High?\n";
cout << "2 - Low?\n";
cout << "3 - Correct?\n";
cin >> response;

for(counter = 1; counter >=100; counter++);

switch(response)
{
case 1:
high = guess - 1;
break;
case 2:
low = guess + 1;
break;
case 3:
correct = true;
break;
default:
cout << "Invalid response\n";
continue;
}
}

while(!correct);

cout << "Your number is " << guess << "this was achieved in " << counter << "tries.\n";
return 0;
}

Random()
Profile Blog Joined August 2004
Kyrgyz Republic1462 Posts
October 07 2010 12:48 GMT
#2
You change high and low but you never change guess.

Try moving "guess = (high + low) / 2" line inside the do block (but keep the declaration outside).
renixian
Profile Blog Joined February 2010
United States50 Posts
October 07 2010 12:59 GMT
#3
Thank you! That solved my problem of the value not changing! \o/
Inkarnate
Profile Blog Joined April 2010
Canada840 Posts
Last Edited: 2010-10-07 13:04:13
October 07 2010 13:00 GMT
#4
Yeah, when you initially declare the variables, thats only the value they are created with. Every time you want to update the value you need to run that line of code again.

An easier way to do the problem would be to get rid of high and low altogether. Since you know the value is between 1 and 100 already you can just start guess at 50 and then add or subtract 1 depending on how the position. This uses less variables and is generally more efficient.
renixian
Profile Blog Joined February 2010
United States50 Posts
October 07 2010 13:11 GMT
#5
Yeah I was thinking of doing that, but I felt it would be annoying for the end-user to sit there and hit lower 49 times til it finally gets to 1.

Now I'm having a run-time issue :[ I'll ask a friend more in-depth tomorrow. Thanks for your help already though!
Navane
Profile Blog Joined February 2007
Netherlands2749 Posts
October 07 2010 13:18 GMT
#6
You are not supposed to just add or substract 1. You are supposed to go from 50 to 25, to 34 to 29 to 27 to 28; go to halfway. That way you reach your destination much quicker.
Inkarnate
Profile Blog Joined April 2010
Canada840 Posts
October 07 2010 13:27 GMT
#7
See above, efficiency is a big deal especially when dealing with large sorting algorithms. Pretty much the logical approach to the question is:

Guess 50, find out if higher or lower.
Guess 25 or 75 depending, find out if higher or lower.

So and an so forth, halving the possible remaining numbers. Even though this may be inefficient for a range of 100, a correctly coded program can accept any parameters (range > 100).

I'll let you figure out how to code it

tofucake
Profile Blog Joined October 2009
Hyrule19161 Posts
October 07 2010 15:32 GMT
#8
first, code tags are awesome.

second,
#include <iostream.h>

int main()
{
int guess = 50;
int in = 0;
int n = 3;
bool cond = false;

while(cond == false)
{
cout << "Enter a number between 1 and 100: ";
cin >> in;
cond = in < 1 || in > 100 ? false : true;
}

while(in != guess)
{
cout << "Is your number less than [0], greater than [1], or equal to [2] " << guess << "? ";
cin >> n;
if(n == 2) break;
if(n == 0) guess /= 2;
if(n == 1) guess += (guess / 2);
}
return 0;
}
Liquipediaasante sana squash banana
Please log in or register to reply.
Live Events Refresh
Wardi Open
12:00
#62
WardiTV1094
Harstem308
TKL 295
Rex168
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Harstem 308
TKL 295
Rex 168
LamboSC2 23
Codebar 5
StarCraft: Brood War
Britney 43958
Horang2 1846
Soulkey 1679
Calm 1447
actioN 1005
Stork 815
Larva 672
Soma 670
Light 607
Hyuk 548
[ Show more ]
firebathero 383
ZerO 301
BeSt 265
Rush 140
Snow 82
Hyun 66
Mind 43
sas.Sziky 39
Backho 36
Free 28
ToSsGirL 24
Terrorterran 22
zelot 19
scan(afreeca) 15
SilentControl 10
Hm[arnc] 5
Dota 2
singsing2917
Gorgc2347
qojqva1572
Dendi755
XcaliburYe120
febbydoto7
Counter-Strike
fl0m4636
olofmeister1037
zeus997
byalli201
oskar92
markeloff86
Other Games
B2W.Neo2088
hiko574
Fuzer 359
Lowko323
Hui .314
XaKoH 127
Mew2King124
ArmadaUGS74
ZerO(Twitch)15
Liquid`VortiX6
Organizations
Dota 2
PGL Dota 2 - Main Stream352
StarCraft: Brood War
Kim Chul Min (afreeca) 12
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 2538
• WagamamaTV474
League of Legends
• Nemesis2988
• Jankos1829
• TFBlade934
• HappyZerGling165
Upcoming Events
Monday Night Weeklies
1h 32m
OSC
7h 32m
Wardi Open
20h 32m
PiGosaur Cup
1d 9h
Replay Cast
1d 17h
Wardi Open
1d 20h
OSC
1d 21h
Tenacious Turtle Tussle
2 days
The PondCast
2 days
Replay Cast
3 days
[ Show More ]
OSC
4 days
LAN Event
4 days
Replay Cast
4 days
Replay Cast
4 days
Sparkling Tuna Cup
5 days
Replay Cast
6 days
Wardi Open
6 days
Liquipedia Results

Completed

SOOP Univ League 2025
RSL Revival: Season 3
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
META Madness #9
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.