I just got to thinking about how I set up my variables
and I want to know if it is okay to use string variables
for everything and convert them when I need to calculate.
I liked using strings for almost everything so I can rely on
getline() and using fstream or maybe I should just do it the
other way, declare them as integer, double, char, etc and
then convert them?
[Solved] Question on data types
Moderator: Coders of Rage
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
[Solved] Question on data types
Last edited by zodiac976 on Mon Jun 29, 2009 8:12 pm, edited 1 time in total.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Question on data types
Declare what you need. If you want to enforce a datatype input, or if you're expecting a certain datatype as input, use cin with your specified datatype and check for cin failure to see if you got the type that you needed (and do a[getline() or .ignore() or .sync() to remove the trailing \n). If cin.fail() is true, do a .sync() to remove all unread characters from the input buffer, and follow it with cin.flush(), to remove the failure flag. On the other hand, if you really need string manipulation, declare your input variables as strings. But, as you can see, there are better ways to validate than by using a string.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Question on data types
I see thank you for the input .
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: [Solved] Question on data types
scanf FTW!
Just putting that out there
Just putting that out there
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: [Solved] Question on data types
Isn't that for C and not C++?Ginto8 wrote:scanf FTW!
Just putting that out there
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: [Solved] Question on data types
but you are free to use it, I tend to use strlen() instead of string.length(), because it's just plain easierzodiac976 wrote:Isn't that for C and not C++?Ginto8 wrote:scanf FTW!
Just putting that out there
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: [Solved] Question on data types
Yep, but I am so used to strings and I always use .length() .programmerinprogress wrote:but you are free to use it, I tend to use strlen() instead of string.length(), because it's just plain easierzodiac976 wrote:Isn't that for C and not C++?Ginto8 wrote:scanf FTW!
Just putting that out there