• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 21:45
CEST 03:45
KST 10:45
  • 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
A Eulogy for the Six Pool BoxeR's Wings Episode 2 - Fan Translation Greatest Players of All Time: 2025 Update #1: Maru - Greatest Players of All Time 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
Post ASL20 Ro24 discussion. BGH Auto Balance -> http://bghmmr.eu/ No Rain in ASL20? How do I speak directly to Coinbase?1-(888)-419-97 Recent recommended BW games
Tourneys
[IPSL] CSLAN Review and CSLPRO Reimagined! [ASL20] Ro24 Group D [ASL20] Ro24 Group E Small VOD Thread 2.0
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: 2362 users

Help with programming in C++ - Page 3

Blogs > KiLL_ORdeR
Post a Reply
Prev 1 2 3 All
LastWish
Profile Blog Joined September 2004
2013 Posts
Last Edited: 2010-10-10 15:18:41
October 10 2010 15:15 GMT
#41
On October 08 2010 06:33 Adeny wrote:
This works fine...

int x = 0;
cin >> x;
if (cin.good() > 0)
{
cout << "user input is: " << x << endl;
}
else
{
cout << "user input is bad" << endl;
}


LOLOL DISREGARD THIS ENTIRE MESSAGE

+ Show Spoiler +
Ok, let's validate the input so that only digits between 0 and 9 will be accepted. If you use cin >> on an integer, your program will blow up if someone starts typing anything but strictly numbers. Because of that we have to jump hoops...

HUH, DISREGARD THIS LOL. It seems if you cin >> to an int, with characters and nonsense, the int stays 0... God damnit it.
+ Show Spoiler +

#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
cout << "Input number or w/e\n";
string input;
cin >> input;
for (int x = 0; x < input.size(); x++)
{
if ( input[x] < 48 && input[x] > 57 )
{
cout << "Only 0-9";
break;
}
}

int validated = atoi(input.c_str());
if (validated > 0)
{
cout << validated;
}
else
{
cout << "Number must be over 0\n";
}

getch();
return 0;
}


What it does is take the users input, places it in to a string. Now it dissects the string, and checks each character seperately if its ASCII value is above or below 48-57, which is 0-9 on an ASCII table (ASCII Chart). Then convers the string to an integer using atoi(), and finally checks if the ints value is above 0.

Chances are though, your professor doesn't expect that of you and just poorly worded the assignment, maybe he wants you to assume that a user will always input numbers, in which case simply doing the following should work:

+ Show Spoiler +

int input;
cin >> input;
if (input > 0)
{
// do whatever here
}
else
{
// tell the user his input was bad.
}


Also god damn conversions in C++, jeez.


Seems to me you need some help aswell.

You can use :

if(cin.fail()){
// print bad stuff
return -1;
}

Alternatively you can set cin.exceptions(istream::failbit) and catch istream::failure exception.


if (input[x] < 48 && input[x] > 57 )

You don't have to use 48 or 57 but :

if (input[x] < '0' && input[x] > '9' )

does the same.

Edit : Oh ok, you have edited the former message..
- It's all just treason - They bring me down with their lies - Don't know the reason - My life is fire and ice -
Adeny
Profile Blog Joined January 2009
Norway1233 Posts
October 10 2010 16:52 GMT
#42
^Yeah, it's been a while since I C++'d, the manual method is probably from C I dunno, lately trying to learn C# has left my head spinning, too many C's.
KiLL_ORdeR
Profile Blog Joined June 2009
United States1518 Posts
October 10 2010 17:02 GMT
#43
On October 09 2010 09:11 Spazer wrote:
My god Wascana Eclipse is bloated. Dev C++ is 9MB compared to this 150MB monster.

Anyway, I thought I'd show you how to debug simple programs. I'm gonna use your ocean levels program as an example and go over your mistake in there at the same time.

+ Show Spoiler [Warning: really long] +
To begin with, your screen should look something like this:
[image loading]

Click on the text editor window (the window with all your code in it). Now go to Run > Debug As > Local C/C++ Application
[image loading]

This message box might come up. Choose yes to change perspectives.
[image loading]

Your screen should now look like this:
[image loading]
Here's an overview of the various important screens:
  1. This kinda tells you what the system's doing. Right now it's saying that the program is suspended at a breakpoint. I'll explain this in a minute. You can probably ignore this for the most part.
  2. This is the variables screen. It tells you about variables are used in the program as they are declared and changed.
  3. This is your program. Note the line I've circled, and how there's a tiny blue arrow on the left hand side. This means the program is currently stopped at that line.
  4. This is the console. It will show you the output of your program as it would normally appear in the command prompt. You've probably used this section before.


So here's what debug mode does: it stops your program at certain locations called breakpoints, and allows you to step through your program one line at a time. This way, you can observe the changes in your variables and locate any problems easier. Normally, you'd be able to set breakpoints wherever you'd like in your source file, but I can't figure out how to properly use this stupid program. Even the online help wasn't any use. -_-

When you enter debug mode using the steps I've described above, the breakpoint is automatically set at the first line of the program. Thus, your program is currently stopped at the line int years = 0;
Look at the variables window. Notice how years has shown up because it has been declared, but that the value is all screwed up. This is because the value has not been assigned to the variable.

Let's hit step into to advance the code one line:
[image loading]

[image loading]
Notice that the little blue arrow has gone down one line. Also notice that the variable years now has a value of 0.

I'm gonna keep going.
[image loading]
Now we're at the loop. Take a look at the variable screen. years is 0, which is less than or equal to 25, so we enter the loop.

Stepping further...
[image loading]
Look at the console and notice that we now have output. The reason why the first two cout statements didn't show up before this is that text is sometimes stored in the buffer instead of immediately being displayed. Suffice it to say that this is nothing to worry about.

Anyway, this is the part where your program screws up. This line:
cout << years << "\t\t" << (years + 1.5) <<endl;

Since years is 0, (years +1.5) gives you an output of 1.5 instead of 0.
As others have mentioned in this thread, having (years * 1.5) would fix this.

Strictly speaking, the expression (years +1.5) is bad since type int isn't meant to contain decimal values. However, the program is smart enough to typecast the integer as a float or double to compensate for this.

Stepping some more...
[image loading]
years has now incremented, as you can see.
The while loop has also ended, so we're now reevaluating the condition right now. years is still under 25, so we'll end up entering the loop again.

Stepping more...
[image loading]
Here we see that the output for 1 year is 2.5 instead of 1.5
Again, using the expression (years * 1.5) would solve this problem, giving an output of 1.5

Hit resume (outlined in green) to make the program run to the end without further stoppages. Hit terminate (outlined in red) to make the program stop running completely (means no more stepping) at its current location.
[image loading]


So that's a basic overview of how to debug your programs. Using debug mode will give you a better understanding of what your program is doing at every step, and help you locate those nasty bugs. It's a really nice feature you should learn to use.

Edit: By the way, I really hate Wascana Eclipse. It's not very intuitive at all. Given a choice, I'd use another development environment, but you guys might not get a choice in that. =/


Wow man, just wow.

We went over the debugging process for about half of one 50 minute class period, and haven't touched it since. This is really, really informative though, i'm really glad that you went through the effort of doing this, thank you very much.

We don't get a choice of which program to use. There is a list of programs we can use on the course webpage, but most of them cost money, and Wascana is free, and what we use in class
In order to move forward, we must rid ourselves of that which holds us back. Check out my stream and give me tips! twitch.tv/intotheskyy
Spazer
Profile Blog Joined March 2009
Canada8031 Posts
October 10 2010 17:19 GMT
#44
On October 11 2010 02:02 KiLL_ORdeR wrote:
Show nested quote +
On October 09 2010 09:11 Spazer wrote:
My god Wascana Eclipse is bloated. Dev C++ is 9MB compared to this 150MB monster.

Anyway, I thought I'd show you how to debug simple programs. I'm gonna use your ocean levels program as an example and go over your mistake in there at the same time.

+ Show Spoiler [Warning: really long] +
To begin with, your screen should look something like this:
[image loading]

Click on the text editor window (the window with all your code in it). Now go to Run > Debug As > Local C/C++ Application
[image loading]

This message box might come up. Choose yes to change perspectives.
[image loading]

Your screen should now look like this:
[image loading]
Here's an overview of the various important screens:
  1. This kinda tells you what the system's doing. Right now it's saying that the program is suspended at a breakpoint. I'll explain this in a minute. You can probably ignore this for the most part.
  2. This is the variables screen. It tells you about variables are used in the program as they are declared and changed.
  3. This is your program. Note the line I've circled, and how there's a tiny blue arrow on the left hand side. This means the program is currently stopped at that line.
  4. This is the console. It will show you the output of your program as it would normally appear in the command prompt. You've probably used this section before.


So here's what debug mode does: it stops your program at certain locations called breakpoints, and allows you to step through your program one line at a time. This way, you can observe the changes in your variables and locate any problems easier. Normally, you'd be able to set breakpoints wherever you'd like in your source file, but I can't figure out how to properly use this stupid program. Even the online help wasn't any use. -_-

When you enter debug mode using the steps I've described above, the breakpoint is automatically set at the first line of the program. Thus, your program is currently stopped at the line int years = 0;
Look at the variables window. Notice how years has shown up because it has been declared, but that the value is all screwed up. This is because the value has not been assigned to the variable.

Let's hit step into to advance the code one line:
[image loading]

[image loading]
Notice that the little blue arrow has gone down one line. Also notice that the variable years now has a value of 0.

I'm gonna keep going.
[image loading]
Now we're at the loop. Take a look at the variable screen. years is 0, which is less than or equal to 25, so we enter the loop.

Stepping further...
[image loading]
Look at the console and notice that we now have output. The reason why the first two cout statements didn't show up before this is that text is sometimes stored in the buffer instead of immediately being displayed. Suffice it to say that this is nothing to worry about.

Anyway, this is the part where your program screws up. This line:
cout << years << "\t\t" << (years + 1.5) <<endl;

Since years is 0, (years +1.5) gives you an output of 1.5 instead of 0.
As others have mentioned in this thread, having (years * 1.5) would fix this.

Strictly speaking, the expression (years +1.5) is bad since type int isn't meant to contain decimal values. However, the program is smart enough to typecast the integer as a float or double to compensate for this.

Stepping some more...
[image loading]
years has now incremented, as you can see.
The while loop has also ended, so we're now reevaluating the condition right now. years is still under 25, so we'll end up entering the loop again.

Stepping more...
[image loading]
Here we see that the output for 1 year is 2.5 instead of 1.5
Again, using the expression (years * 1.5) would solve this problem, giving an output of 1.5

Hit resume (outlined in green) to make the program run to the end without further stoppages. Hit terminate (outlined in red) to make the program stop running completely (means no more stepping) at its current location.
[image loading]


So that's a basic overview of how to debug your programs. Using debug mode will give you a better understanding of what your program is doing at every step, and help you locate those nasty bugs. It's a really nice feature you should learn to use.

Edit: By the way, I really hate Wascana Eclipse. It's not very intuitive at all. Given a choice, I'd use another development environment, but you guys might not get a choice in that. =/


Wow man, just wow.

We went over the debugging process for about half of one 50 minute class period, and haven't touched it since. This is really, really informative though, i'm really glad that you went through the effort of doing this, thank you very much.

We don't get a choice of which program to use. There is a list of programs we can use on the course webpage, but most of them cost money, and Wascana is free, and what we use in class

For whatever reason, programming classes usually don't teach you how to use the debugging features. I don't know why - it's just so useful. Debugging tells you so much about how your program operates that I don't know how I got by without it before. Anyway, enjoy.
Liquipedia
TossFloss *
Profile Blog Joined February 2010
Canada606 Posts
October 12 2010 05:29 GMT
#45
On October 08 2010 12:29 Tenrou wrote:
My advice is to read your book and every time you see something new, create a test program to use the new code. Check to see what it does and can't do.


Most textbooks are trash. You should read a book but make sure it's a good one.
TL Android App Open Source http://www.teamliquid.net/forum/viewmessage.php?topic_id=265090
Prev 1 2 3 All
Please log in or register to reply.
Live Events Refresh
Replay Cast
00:00
SEL S2 Championship: Ro16
CranKy Ducklings129
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SpeCial 184
RuFF_SC2 123
ProTech119
CosmosSc2 30
Nina 3
StarCraft: Brood War
Artosis 801
NaDa 72
Aegong 52
Purpose 26
Dota 2
NeuroSwarm102
League of Legends
JimRising 343
Counter-Strike
fl0m1753
Other Games
summit1g11628
shahzam1017
Day[9].tv850
C9.Mang0477
ViBE197
WinterStarcraft137
Mew2King38
ROOTCatZ14
Vindicta6
Organizations
Other Games
BasetradeTV40
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota22109
League of Legends
• Doublelift4319
Other Games
• Day9tv850
Upcoming Events
LiuLi Cup
9h 15m
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
14h 15m
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
Big Brain Bouts
14h 15m
Iba vs GgMaChine
TriGGeR vs Bunny
Reynor vs Classic
Serral vs Clem
BSL Team Wars
17h 15m
Team Hawk vs Team Dewalt
BSL Team Wars
17h 15m
Team Hawk vs Team Bonyth
Code For Giants Cup
20h 45m
SC Evo League
1d 10h
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
Maestros of the Game
1d 14h
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
1d 16h
SC Evo League
2 days
[ Show More ]
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
3 days
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
Replay Cast
5 days
The PondCast
6 days
RSL Revival
6 days
Maru vs SHIN
MaNa vs MaxPax
Liquipedia Results

Completed

CSL Season 18: Qualifier 1
WardiTV Summer 2025
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
Thunderpick World Champ.
MESA Nomadic Masters Fall
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.