• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:31
CEST 01:31
KST 08:31
  • 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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence5Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
Weekly Cups (Sept 8-14): herO & MaxPax split cups3WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia7Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29LiuLi Cup - September 2025 Tournaments3
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues
Tourneys
WardiTV TL Team Map Contest #5 Tournaments Maestros of The Game—$20k event w/ live finals in Paris RSL: Revival, a new crowdfunded tournament series Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
[ASL20] Ro16 Preview Pt2: Turbulence BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ ASL20 General Discussion Playing StarCraft as 2 people on the same network
Tourneys
[ASL20] Ro16 Group C Is there English video for group selection for ASL [ASL20] Ro16 Group B [IPSL] ISPL Season 1 Winter Qualis and Info!
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile General RTS Discussion Thread Nintendo Switch Thread Borderlands 3
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread Russo-Ukrainian War Thread The Big Programming Thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1296 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
Hyrule19086 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
Next event in 29m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
NeuroSwarm 258
CosmosSc2 43
ROOTCatZ 41
StarCraft: Brood War
Artosis 641
ggaemo 39
sSak 18
ajuk12(nOOB) 17
Counter-Strike
fl0m809
Stewie2K337
Super Smash Bros
Liquid`Ken98
Other Games
summit1g4588
Grubby3650
FrodaN2228
shahzam904
ToD219
JimRising 149
C9.Mang0147
Maynarde86
SortOf70
Trikslyr58
Nathanias18
fpsfer 2
Organizations
Other Games
gamesdonequick502
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• davetesta37
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• LaughNgamezSOOP
• Kozan
StarCraft: Brood War
• blackmanpl 16
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota22197
• WagamamaTV520
• Ler52
Other Games
• imaqtpie1028
• Scarra974
Upcoming Events
OSC
29m
Sparkling Tuna Cup
10h 29m
Afreeca Starleague
10h 29m
Light vs Speed
Larva vs Soma
2v2
11h 29m
PiGosaur Monday
1d
LiuLi Cup
1d 11h
RSL Revival
2 days
Maru vs Reynor
Cure vs TriGGeR
The PondCast
2 days
RSL Revival
3 days
Zoun vs Classic
Korean StarCraft League
4 days
[ Show More ]
BSL Open LAN 2025 - War…
4 days
RSL Revival
4 days
BSL Open LAN 2025 - War…
5 days
RSL Revival
5 days
Online Event
5 days
Wardi Open
6 days
Liquipedia Results

Completed

BSL 20 Team Wars
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
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
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL Polish World Championship 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 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.