• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 06:28
CEST 12:28
KST 19: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
herO wins GSL Code S Season 1 (2025)13Code S RO4 & Finals Preview: herO, GuMiho, Classic, Cure6Code S RO8 Preview: Classic, Reynor, Maru, GuMiho4Code S RO8 Preview: ByuN, Rogue, herO, Cure5[ASL19] Ro4 Preview: Storied Rivals7
Community News
Weekly Cups (May 12-18): Clem sweeps WardiTV May3Code S Season 2 (2025) - Qualifier Results212025 GSL Season 2 (Qualifiers)14Code S Season 1 - Classic & GuMiho advance to RO4 (2025)4[BSL 2v2] ProLeague Season 3 - Friday 21:00 CET7
StarCraft 2
General
Code S Season 2 (2025) - Qualifier Results herO wins GSL Code S Season 1 (2025) Code S RO8 Preview: Classic, Reynor, Maru, GuMiho Replay Cast Power Rank: October 2018
Tourneys
WardiTV Mondays RSL: Revival, a new crowdfunded tournament series DreamHack Dallas 2025 announced (May 23-25) [GSL 2025] Code S Season 1 - RO4 and Grand Finals PIG STY FESTIVAL 6.0! (28 Apr - 4 May)
Strategy
Simple Questions Simple Answers [G] PvT Cheese: 13 Gate Proxy Robo
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 474 Futile Resistance Mutation # 473 Cold is the Void Mutation # 472 Dead Heat Mutation # 471 Delivery Guaranteed
Brood War
General
BW General Discussion who is JiriKara /Cipisek/ from CZ ASL 19 Tickets for foreigners Where is effort ? BGH auto balance -> http://bghmmr.eu/
Tourneys
[ASL19] Semifinal B [Megathread] Daily Proleagues [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET The Casual Games of the Week Thread
Strategy
I am doing this better than progamers do. [G] How to get started on ladder as a new Z player Creating a full chart of Zerg builds
Other Games
General Games
What do you want from future RTS games? Stormgate/Frost Giant Megathread Beyond All Reason Grand Theft Auto VI Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
LiquidLegends to reintegrate into TL.net
Heroes of the Storm
Simple Questions, Simple Answers
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia TL Mafia Community Thread TL Mafia Plays: Diplomacy TL Mafia: Generative Agents Showdown Survivor II: The Amazon
Community
General
Men's Fashion Thread Russo-Ukrainian War Thread US Politics Mega-thread Things Aren’t Peaceful in Palestine UK Politics Mega-thread
Fan Clubs
Serral Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread [Books] Wool by Hugh Howey
Sports
2024 - 2025 Football Thread Formula 1 Discussion NHL Playoffs 2024 NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Cleaning My Mechanical Keyboard How to clean a TTe Thermaltake keyboard?
TL Community
The Automated Ban List TL.net Ten Commandments
Blogs
Narcissists In Gaming: Why T…
TrAiDoS
Poker
Nebuchad
Info SLEgma_12
SLEgma_12
SECOND COMMING
XenOsky
WombaT’s Old BW Terran Theme …
WombaT
Heero Yuy & the Tax…
KrillinFromwales
Customize Sidebar...

Website Feedback

Closed Threads



Active: 9237 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
Netherlands2745 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
Hyrule19017 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
The PondCast
10:00
Episode 48
CranKy Ducklings39
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft: Brood War
Calm 8528
Sea 2450
Stork 1752
Mong 1131
Bisu 885
GuemChi 245
Mini 226
EffOrt 202
Light 134
Dewaltoss 113
[ Show more ]
Last 74
Liquid`Ret 50
GoRush 48
ToSsGirL 47
Shine 38
Aegong 23
Sexy 21
Rush 21
JulyZerg 21
NotJumperer 20
NaDa 19
sSak 6
scan(afreeca) 5
Dota 2
Gorgc2196
ODPixel688
XcaliburYe540
Fuzer 215
Counter-Strike
x6flipin349
Super Smash Bros
Mew2King197
Other Games
ceh9710
SortOf159
XaKoH 157
Trikslyr20
Organizations
StarCraft: Brood War
Kim Chul Min (afreeca) 680
Other Games
gamesdonequick504
StarCraft 2
ESL.tv129
StarCraft: Brood War
lovetv 9
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• LUISG 53
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota282
League of Legends
• Stunt1425
Upcoming Events
OSC
32m
Solar vs YoungYakov
Spirit vs MindelVK
MaNa vs TriGGeR
ByuN vs ArT
SHIN vs Percival
ShowTime vs Bunny
Nicoract vs Arrogfire
BSL: GosuLeague
7h 32m
Replay Cast
13h 32m
Replay Cast
23h 32m
Replay Cast
1d 13h
Replay Cast
1d 23h
Road to EWC
2 days
Replay Cast
2 days
SC Evo League
3 days
Road to EWC
3 days
[ Show More ]
Afreeca Starleague
3 days
BeSt vs Soulkey
Road to EWC
4 days
Wardi Open
5 days
SOOP
5 days
Replay Cast
5 days
Replay Cast
6 days
GSL Code S
6 days
The PondCast
6 days
Liquipedia Results

Completed

China & Korea Top Challenge
2025 GSL S1
Calamity Stars S2

Ongoing

JPL Season 2
ASL Season 19
YSL S1
BSL 2v2 Season 3
BSL Season 20
KCM Race Survival 2025 Season 2
NPSL S3
Heroes 10 EU
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
ECL Season 49: Europe
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025
YaLLa Compass Qatar 2025
PGL Bucharest 2025
BLAST Open Spring 2025
ESL Pro League S21

Upcoming

Rose Open S1
Copa Latinoamericana S4
CSLPRO Last Chance 2025
CSLAN 2025
K-Championship
SEL Season 2 Championship
Esports World Cup 2025
HSC XXVII
Championship of Russia 2025
Bellum Gens Elite Stara Zagora 2025
2025 GSL S2
DreamHack Dallas 2025
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
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.