• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 00:52
CET 06:52
KST 14:52
  • 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
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
ComeBackTV's documentary on Byun's Career !9Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15
StarCraft 2
General
Micro Lags When Playing SC2? ComeBackTV's documentary on Byun's Career ! When will we find out if there are more tournament Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win RSL Revival - 2025 Season Finals Preview
Tourneys
$100 Prize Pool - Winter Warp Gate Masters Showdow $5,000+ WardiTV 2025 Championship Winter Warp Gate Amateur Showdown #1 Sparkling Tuna Cup - Weekly Open Tournament RSL Offline Finals Info - Dec 13 and 14!
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress
Brood War
General
Klaucher discontinued / in-game color settings Anyone remember me from 2000s Bnet EAST server? BGH Auto Balance -> http://bghmmr.eu/ How Rain Became ProGamer in Just 3 Months FlaSh on: Biggest Problem With SnOw's Playstyle
Tourneys
[BSL21] LB QuarterFinals - Sunday 21:00 CET Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] WB SEMIFINALS - Saturday 21:00 CET
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Mechabellum PC Games Sales Thread
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
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI YouTube Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
The (Hidden) Drug Problem in…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1905 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
Canada8033 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
Ladder Legends
19:00
WWG Amateur Showdown
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft625
RuFF_SC2 217
Nina 102
StarCraft: Brood War
Larva 575
Shuttle 478
Leta 273
Mong 18
Noble 15
Sea 0
Dota 2
NeuroSwarm64
LuMiX1
League of Legends
JimRising 638
Heroes of the Storm
Khaldor144
Other Games
summit1g9877
Mew2King120
ViBE54
Models2
Organizations
Other Games
gamesdonequick835
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 12 non-featured ]
StarCraft 2
• Berry_CruncH207
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Other Games
• Scarra1628
Upcoming Events
Sparkling Tuna Cup
4h 8m
Ladder Legends
11h 8m
BSL 21
14h 8m
StRyKeR vs TBD
Bonyth vs TBD
Replay Cast
1d 3h
Wardi Open
1d 6h
Monday Night Weeklies
1d 11h
WardiTV Invitational
3 days
Replay Cast
4 days
WardiTV Invitational
4 days
ByuN vs Solar
Clem vs Classic
Cure vs herO
Reynor vs MaxPax
Replay Cast
5 days
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Offline Finals
Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
CSL Season 19: Qualifier 1
WardiTV 2025
META Madness #9
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL Season 19: Qualifier 2
CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.