in C++, what data type will store both numbers and characters? so that you could input IP addresses or handles or something that contained both?
Printable View
in C++, what data type will store both numbers and characters? so that you could input IP addresses or handles or something that contained both?
you could use type char[size] or string or char*. Note with those you would have to check if the value contained is numeric or alpha. If your working on *nix and want to work with things such as ip address check out socket.h. You could also build your own class to handle it the types.
Hope that helps.
Cheers,
-D
characters will store numbers and characters, because numbers are represented by characters like "4" and "7" just like 'characters' (letters) are stored as characters like "a" or "G" or ">" for that matter.
however if you make a character array, or #include the string library and use a "string" datatype, you can then hold characters.
When you want to perform an arithmetic operation on a character ( or string[x] ), you can cast the character "7" as the integer "7". or with any number or letter or even symbol.
char characterNAME = '2' ;
int integerNAME = 3 ;
cout << integerNAME + int(characterNAME) ;
the output should be 5 naturally, unless you changed the value for 2 or 3.
cast integers as characters:
int ONE = 1 ;
char characterstoredinONE = char(ONE) ;
thank you, and </day> goodnight...
C++ doesn't have a string <variable> datatype per se. thats VB. If you would like to use the standard library you could use this:
#include <iostream>
#include <string.h>
main()
{
std::string <variable>
// that variable will now be of string datatype.
}
or if you want to be more traditional you could use something like.
#include <stdio.h>
#define STR_LEN 80
int main()
{
char strIN[STR_LEN+1];
// this will hold up to an 80 character string, (which you can adjust for your needs) plus the NULL terminator. i.e. \0.
}
Hope this helps.
The header file <string> holds a class for a string type in C++:
i.e.:
<quote>
include<string>
int main(){
string my_string_var;
my_string_var = "My string data";
return 0;
}
</quote>
However I'd agree, that it would probably be best to create your own class to handle the data types