• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:10
CEST 01:10
KST 08:10
  • 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
[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris34Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
BoxeR's Wings Episode 2 - Fan Translation Greatest Players of All Time: 2025 Update #1: Maru - Greatest Players of All Time A Eulogy for the Six Pool Geoff 'iNcontroL' Robinson has passed away
Tourneys
$5,000 WardiTV Summer Championship 2025 Maestros of The Game—$20k event w/ live finals in Paris $5,100+ SEL Season 2 Championship (SC: Evo) Esports World Cup 2025 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
External Content
Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Post ASL20 Ro24 discussion. No Rain in ASL20? How do I speak directly to Coinbase?1-(888)-419-97 Recent recommended BW games
Tourneys
[ASL20] Ro24 Group D [ASL20] Ro24 Group E Small VOD Thread 2.0 [Megathread] Daily Proleagues
Strategy
Muta micro map competition Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV Path of Exile
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Customize Sidebar...

Website Feedback

Closed Threads



Active: 967 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
Hyrule19082 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 50m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech122
CosmosSc2 57
StarCraft: Brood War
Aegong 978
Artosis 649
scan(afreeca) 37
Purpose 11
Counter-Strike
Stewie2K721
taco 410
Foxcn293
Other Games
summit1g7478
tarik_tv3691
Grubby2272
FrodaN2117
Sick355
C9.Mang0195
ViBE194
shahzam181
ToD175
Day[9].tv91
Mew2King48
PPMD31
Organizations
Other Games
BasetradeTV23
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• RyuSc2 47
• musti20045 35
• IndyKCrew
• Migwel
• sooper7s
• AfreecaTV YouTube
• intothetv
• Kozan
• LaughNgamezSOOP
StarCraft: Brood War
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21937
League of Legends
• Doublelift4268
Counter-Strike
• Shiphtur172
Other Games
• imaqtpie1000
• Day9tv91
Upcoming Events
Replay Cast
50m
LiuLi Cup
11h 50m
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
16h 50m
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
Big Brain Bouts
16h 50m
Iba vs GgMaChine
TriGGeR vs Bunny
Reynor vs Classic
Serral vs Clem
BSL Team Wars
19h 50m
Team Hawk vs Team Dewalt
BSL Team Wars
19h 50m
Team Hawk vs Team Bonyth
Code For Giants Cup
23h 20m
SC Evo League
1d 12h
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
Maestros of the Game
1d 16h
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
1d 18h
[ Show More ]
SC Evo League
2 days
Maestros of the Game
2 days
SHIN vs Creator
Astrea vs Lambo
Bunny vs SKillous
HeRoMaRinE vs TriGGeR
BSL Team Wars
2 days
Team Bonyth vs Team Sziky
BSL Team Wars
2 days
Team Dewalt vs Team Sziky
Monday Night Weeklies
3 days
Replay Cast
4 days
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
Replay Cast
6 days
The PondCast
6 days
RSL Revival
6 days
Maru vs SHIN
MaNa vs MaxPax
Liquipedia Results

Completed

CSL Season 18: Qualifier 1
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
Acropolis #4 - TS1
CSL Season 18: Qualifier 2
SEL Season 2 Championship
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
Skyesports Masters 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
FISSURE Playground #2
BLAST Open 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.