[Solved] Question on data types

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

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?
Last edited by zodiac976 on Mon Jun 29, 2009 8:12 pm, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Question on data types

Post by MarauderIIC »

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.
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

I see thank you for the input :).
User avatar
Ginto8
ES Beta Backer
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

Post by Ginto8 »

scanf FTW!

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.
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

Ginto8 wrote:scanf FTW!

Just putting that out there
Isn't that for C and not C++?
User avatar
programmerinprogress
Chaos Rift Devotee
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

Post by programmerinprogress »

zodiac976 wrote:
Ginto8 wrote:scanf FTW!

Just putting that out there
Isn't that for C and not C++?
but you are free to use it, I tend to use strlen() instead of string.length(), because it's just plain easier :lol:
---------------------------------------------------------------------------------------
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
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

programmerinprogress wrote:
zodiac976 wrote:
Ginto8 wrote:scanf FTW!

Just putting that out there
Isn't that for C and not C++?
but you are free to use it, I tend to use strlen() instead of string.length(), because it's just plain easier :lol:
Yep, but I am so used to strings and I always use .length() :lol:.
Post Reply