Page 1 of 1

[Solved] Question on data types

Posted: Fri Jun 26, 2009 6:57 pm
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?

Re: Question on data types

Posted: Fri Jun 26, 2009 7:01 pm
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.

Re: Question on data types

Posted: Mon Jun 29, 2009 8:11 pm
by zodiac976
I see thank you for the input :).

Re: [Solved] Question on data types

Posted: Tue Jul 07, 2009 5:44 pm
by Ginto8
scanf FTW!

Just putting that out there

Re: [Solved] Question on data types

Posted: Wed Jul 08, 2009 4:45 am
by zodiac976
Ginto8 wrote:scanf FTW!

Just putting that out there
Isn't that for C and not C++?

Re: [Solved] Question on data types

Posted: Wed Jul 08, 2009 11:26 am
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:

Re: [Solved] Question on data types

Posted: Wed Jul 08, 2009 11:41 am
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:.