• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 10:37
CEST 16:37
KST 23:37
  • 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: Voting2[ASL20] Ro4 Preview: Descent6Team 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 herO35.0.15 Patch Balance Hotfix (2025-10-8)66Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition315.0.15 Balance Patch Notes (Live version)119
StarCraft 2
General
IP For new Brazil servers for NA Players Weekly Cups (Oct 6-12): Four star herO 5.0.15 Patch Balance Hotfix (2025-10-8) TL.net Map Contest #21 - Finalists PartinG joins SteamerZone, returns to SC2 competition
Tourneys
WardiTV Mondays SC2's Safe House 2 - October 18 & 19 Sparkling Tuna Cup - Weekly Open Tournament RSL Offline Finals Dates + Ticket Sales! SC4ALL $6,000 Open LAN in Philadelphia
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
[ASL20] Ro4 Preview: Descent I'm making videos again Any rep analyzer that shows resources situation? Whose hotkey signature is this? BW General Discussion
Tourneys
[ASL20] Semifinal B [ASL20] Semifinal A [Megathread] Daily Proleagues [ASL20] Ro8 Day 4
Strategy
Current Meta BW - ajfirecracker Strategy & Training Siegecraft - a new perspective TvZ Theorycraft - Improving on State of the Art
Other Games
General Games
Stormgate/Frost Giant Megathread ZeroSpace Megathread Nintendo Switch Thread Dawn of War IV 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
Things Aren’t Peaceful in Palestine US Politics Mega-thread The Games Industry And ATVI Stop the Construction YouTube Thread
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece 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: 2517 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
2015 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
Next event in 1h 23m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
OGKoka 494
Harstem 274
SteadfastSC 220
Rex 170
ProTech74
StarCraft: Brood War
Britney 41107
Calm 9409
Sea 6227
Hyuk 3369
Rain 2996
Flash 2754
Horang2 1796
PianO 1402
GuemChi 1317
Shuttle 957
[ Show more ]
firebathero 804
EffOrt 500
Mini 497
Light 436
Hyun 320
Larva 220
Soulkey 120
Pusan 101
Mind 91
Backho 82
Mong 66
JYJ62
Rush 48
soO 46
Aegong 37
sas.Sziky 30
ToSsGirL 29
ivOry 28
Movie 27
Sacsri 25
Yoon 24
Rock 16
HiyA 15
Shine 13
scan(afreeca) 13
Terrorterran 12
SilentControl 10
Hm[arnc] 8
Noble 7
Dota 2
Gorgc6084
qojqva3622
420jenkins416
syndereN357
XcaliburYe203
League of Legends
KnowMe25
Counter-Strike
markeloff99
edward32
Other Games
singsing2552
hiko814
B2W.Neo447
crisheroes386
Lowko332
byalli227
Hui .169
Fuzer 123
oskar83
ArmadaUGS50
rGuardiaN33
Liquid`VortiX0
Organizations
StarCraft 2
WardiTV1298
CranKy Ducklings86
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• poizon28 13
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• HerbMon 17
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 3063
League of Legends
• Nemesis3338
• Jankos2248
Other Games
• Shiphtur98
Upcoming Events
Monday Night Weeklies
1h 23m
Replay Cast
9h 23m
Afreeca Starleague
19h 23m
Soma vs Bisu
OSC
23h 23m
OSC
1d 3h
MaxPax vs Gerald
Solar vs Krystianer
PAPI vs Lemon
Ryung vs Moja
Nice vs NightPhoenix
Cham vs TBD
MaNa vs TriGGeR
PiGosaur Monday
1d 9h
The PondCast
2 days
OSC
2 days
Wardi Open
3 days
CranKy Ducklings
4 days
[ Show More ]
Safe House 2
5 days
Sparkling Tuna Cup
5 days
Safe House 2
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
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
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 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.