• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 15:34
CET 21:34
KST 05:34
  • 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
Master's Coliseum 8 - Group A: Serral, SHIN, Astrea, Cure6GuMiho wins HomeStory Cup 2612HomeStory Cup 26 - Info and Preview14[SSL Autumn] Finals Recap: The Bill12[SSL Autumn] Finals Preview: Service!12
Community News
Weekly Cups (Dec 9-15): herO, MaxPax, Clem, Dark split the spoils3[Interview] Grrrr... 202425Weekly Cup Results (Dec 2-8): Protoss sweep cup trio5$15,000 Master's Coliseum #8 - (Dec 14-Jan 19)32Weekly Cup Results (Nov 25-Dec 1): MaxPax & Dark win5
StarCraft 2
General
As an esports viewer, how often would you like to see all-in/cheese strategies in tournament games? How important are macro mechanics in pro SC2? Where are Arcade and Custom Campaign files stored? Weekly Cups (Dec 9-15): herO, MaxPax, Clem, Dark split the spoils Master's Coliseum 8 - Group A: Serral, SHIN, Astrea, Cure
Tourneys
WardiTV Christmas Invitational 2024 Tenacious Turtle Tussle Sparkling Tuna Cup - Weekly Open Tournament SOOPer7s #24 - Ryung vs SHIN $15,000 Master's Coliseum #8 - (Dec 14-Jan 19)
Strategy
Simple Questions Simple Answers
Custom Maps
Simple Questions/Answers Mod: Observer PlusPlus Cyclone Observer PlusPlus Cyclone
External Content
Mutation # 452 Rumble in the Jungle Mutation # 451 Violent Night Mutation # 450 Survival of the Fittest Mutation # 449 Shir Chaos
Brood War
General
BGH auto balance -> http://bghmmr.com/ "The Revolution" docu Thoughts on rarely used units Greatest ASL game of all time? BW General Discussion
Tourneys
[BSL19] GosuLeague grand finals & 3rd place match [BSL19] Grand Finals - Sunday 18:30 CET [BSL19] GosuLeague semi-finals matches & casts [BSL19] LB Finals - Sunday 18:30 CET
Strategy
Good army compositions for zerg vs mech? Does mech work very well in TvZ below pro level? Fighting Spirit mining rates
Other Games
General Games
Battle Aces/David Kim RTS Megathread Stormgate/Frost Giant Megathread Nintendo Switch Thread EVE Corporation Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
WORLDS 2024 Tell me someone about the MMR system in LoL What do you think of the animated series Arcane?
Heroes of the Storm
Simple Questions, Simple Answers
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread The Games Industry And ATVI Canadian Politics Mega-thread Iraq & Syrian Civil Wars
Fan Clubs
Serral Fan Club
Media & Entertainment
[Books] Epic-Fantasy series discussion thread Korean Music Discussion [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2025 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
Simple Questions Simple Answers
TL Community
The Automated Ban List
Blogs
Aizen's message from the…
sosukeAiZEN
Inconsistent DPS? can som…
StaticNine
Crime in Fribourg!
Nebuchad
Why We Need Esports-Specific…
TrAiDoS
Eight Anniversary as a TL…
Mizenhauer
Day5 match report InfiniteC…
Freeedom
Unfinished Business
Acetone
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1660 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
Canada8028 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 4h 26m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
trigger 279
UpATreeSC 168
StarCraft: Brood War
Britney 14107
LancerX 8
Dota 2
Gorgc8495
Pyrionflax124
Counter-Strike
fl0m1203
Foxcn294
Stewie2K270
Heroes of the Storm
Liquid`Hasu452
Other Games
summit1g4214
Grubby3103
FrodaN1703
ScreaM1519
Beastyqt726
Dendi572
ceh9440
B2W.Neo324
Hui .159
KnowMe97
Trikslyr87
Mew2King86
NeuroSwarm61
Organizations
StarCraft 2
ESL.tv140
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 18 non-featured ]
StarCraft 2
• HolyHit 13
• Reevou 7
• IndyKCrew
• Migwel
• sooper7s
• AfreecaTV YouTube
• intothetv
• Kozan
• LaughNgamezSOOP
• Laughngamez YouTube
StarCraft: Brood War
• Pr0nogo 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV855
League of Legends
• TFBlade1711
Other Games
• imaqtpie3709
• Shiphtur487
Upcoming Events
OSC
4h 26m
Fjant vs Iba
ArT vs TBD
NightPhoenix vs TBD
Chance vs TBD
YoungYakov vs TBD
Replay Cast
12h 26m
OlimoLeague
14h 26m
Fire Grow Cup
19h 26m
Big Brain Bouts
20h 26m
OSC
21h 26m
Replay Cast
1d 3h
SOOP
1d 13h
Ryung vs SHIN
Master's Coliseum
1d 15h
MaxPax vs SKillous
MaxPax vs Reynor
SKillous vs Rogue
Fire Grow Cup
1d 19h
[ Show More ]
BSL: GosuLeague
1d 23h
Julia vs dxtr13
Hawk vs UltrA
Master's Coliseum
2 days
Rogue vs MaxPax
Reynor vs SKillous
Reynor vs Rogue
Fire Grow Cup
2 days
BSL: ProLeague
2 days
Mihu vs Zhanhun
Online Event
2 days
Wardi Open
3 days
ForJumy Cup
3 days
Replay Cast
6 days
Liquipedia Results

Completed

CSL Season 15: Qualifier 1
HSC XXVI
Bitka W Nexusie

Ongoing

Acropolis #2
BSL Season 19
KCM Race Survival 2024 Season 5
StarCastTV International League 5
CSL Season 15: Qualifier 1
Top Gun of StarCraft Season2
Master's Coliseum 8
PW Shanghai Major 2024
ESL Impact League Season 6
Shanghai Major: EU RMR B
Shanghai Major: EU RMR A
Shanghai Major: AMER RMR
BLAST Premier World Final
Thunderpick World Champ.
ESL Challenger Katowice '24
Elisa Masters Espoo 2024
RES Regional Champions
IEM Rio 2024
ESL Challenger Atlanta '24

Upcoming

CSL 15: 2024 Winter
Platinum Anchor Qualifying S3
HSC XXVII
Bellum Gens Elite Stara Zagora 2025
OSC Championship Season 12
ESL Pro League S21
Skyesports Souvenir 2025
PGL Cluj-Napoca 2025
IEM Katowice 2025
BLAST Bounty Spring 2025
BLAST Bounty Spring Qual
YaLLa Compass Winter 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 © 2024 TLnet. All Rights Reserved.