• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:54
CEST 17:54
KST 00:54
  • 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
TL.net Map Contest #21: Voting6[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5
Community News
Weekly Cups (Oct 6-12): Four star herO65.0.15 Patch Balance Hotfix (2025-10-8)78Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition325.0.15 Balance Patch Notes (Live version)119
StarCraft 2
General
How to Block Australia, Brazil, Singapore Servers 5.0.15 Patch Balance Hotfix (2025-10-8) Revisiting the game after10 years and wow it's bad TL.net Map Contest #21: Voting The New Patch Killed Mech!
Tourneys
Crank Gathers Season 2: SC II Pro Teams LiuLi Cup - September 2025 Tournaments SC4ALL $6,000 Open LAN in Philadelphia Sparkling Tuna Cup - Weekly Open Tournament Master Swan Open (Global Bronze-Master 2)
Strategy
Custom Maps
External Content
Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More
Brood War
General
BW General Discussion Map with fog of war removed for one player? BW caster Sayle Pros React To: BarrackS + FlaSh Coaching vs SnOw After 20 seasons we have a lot of great maps
Tourneys
[ASL20] Semifinal B [ASL20] Semifinal A SC4ALL $1,500 Open Bracket LAN [Megathread] Daily Proleagues
Strategy
Relatively freeroll strategies Current Meta BW - ajfirecracker Strategy & Training Siegecraft - a new perspective
Other Games
General Games
Dawn of War IV Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Megathread Path of Exile
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
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
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Men's Fashion Thread Sex and weight loss
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Inbreeding: Why Do We Do It…
Peanutsc
From Tilt to Ragequit:The Ps…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1942 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
Netherlands2748 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
Hyrule19137 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
OSC
12:00
King of the Hill #227
WardiTV887
IndyStarCraft 212
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
LamboSC2 267
IndyStarCraft 212
JuggernautJason3
StarCraft: Brood War
Britney 34354
Calm 7154
Hyuk 5812
Rain 3685
Flash 2054
Shuttle 1495
Soma 937
Mini 707
EffOrt 699
ZerO 533
[ Show more ]
Light 440
Stork 359
Larva 344
firebathero 281
Snow 261
PianO 250
Hyun 244
BeSt 216
Soulkey 143
hero 130
Rush 75
Barracks 62
Mind 59
JYJ37
Killer 33
Free 32
zelot 30
Mong 29
Aegong 21
Terrorterran 20
scan(afreeca) 17
Movie 15
TY 1
Dota 2
qojqva4406
syndereN534
Fuzer 193
Heroes of the Storm
XaKoH 103
Other Games
singsing2488
B2W.Neo922
hiko796
Mlord408
Beastyqt329
Hui .292
crisheroes267
Lowko261
KnowMe183
Sick176
XcaliburYe132
C9.Mang0114
ArmadaUGS73
kaitlyn40
Dewaltoss23
Rex22
ZerO(Twitch)10
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 6
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 3044
• lizZardDota240
• Noizen39
League of Legends
• TFBlade830
Other Games
• Shiphtur213
Upcoming Events
Wardi Open
19h 6m
CranKy Ducklings
1d 18h
Safe House 2
2 days
Sparkling Tuna Cup
2 days
Safe House 2
3 days
Tenacious Turtle Tussle
6 days
The PondCast
6 days
Liquipedia Results

Completed

Acropolis #4 - TS2
WardiTV TLMC #15
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
EC S1
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
CranK Gathers Season 2: SC II Pro Teams
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
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.