Is there any way to store characters AND integers into an array?

Recommended Answers

All 14 Replies

Member Avatar for iamthwee

No just store them as characters, then if need be convert them to ints.

Not in a simple way. Because things are strongly typed in C/C++, you need to have a single type that can hold your data. You could do something along the lines of this though (pseudocode):

class Data {};
class Int : Data {public: int i};
class Char : Data { public: char c};
Data* theArray[someSize];
theArray[0] = new Int; 
theArray[1] = new Char;

No just store them as characters, then if need be convert them to ints.

I'd go the other way (int to char) so you don't lose data if the int value is >= 256...

Member Avatar for iamthwee

I'd go the other way (int to char) so you don't lose data if the int value is >= 256...

Nah store them as chars, I always do. ints are unsafe. If the user doesn't type in an int where the program expects an int, it crashes, whereas with chars you can't go wrong, if it's bad, you just discard the input and tell them to try again.

Believe me this is the easiest way.

Frigg, I posted it and it got swallowed up.
An array of unions might do.

Nah store them as chars, I always do. ints are unsafe. If the user doesn't type in an int where the program expects an int, it crashes, whereas with chars you can't go wrong, if it's bad, you just discard the input and tell them to try again.

Believe me this is the easiest way.

It depends on where the data is coming from. If it is coming from an input stream, there should be handling in place for the event of bad input. Even if you treat it all as characters, you'll have to parse numbers out of it anyways (unless you're only keeping single-digit ints). What if I do want to store 452 as a data value, assuming that it is valid input? Using a char, that would break your code.

Easiest isn't always best.

Nah store them as chars, I always do. ints are unsafe. If the user doesn't type in an int where the program expects an int, it crashes, whereas with chars you can't go wrong, if it's bad, you just discard the input and tell them to try again.

Believe me this is the easiest way.

Sorry, I can't believe you unless you give an example of the danger. This comment makes no sense, for the reason Infarction stated.

Or are you talking about scanf() vs. fgets() ? If so, please be clearer. Even I get confused with half an idea like that.

Thanks guys this all helps me out alot

Member Avatar for iamthwee

It depends on where the data is coming from. If it is coming from an input stream, there should be handling in place for the event of bad input. Even if you treat it all as characters, you'll have to parse numbers out of it anyways (unless you're only keeping single-digit ints). What if I do want to store 452 as a data value, assuming that it is valid input? Using a char, that would break your code.

Easiest isn't always best.

It depends what you mean by char.

char crap;

or char crap[];

With the second example you can store 452 as a data value, and parse the integers out of it.

It depends what you mean by char.

char crap;

or char crap[];

With the second example you can store 452 as a data value, and parse the integers out of it.

True, but now you've gone from storing int and char to storing char* with the overhead of marking which ones may need to later be converted to integers. No need to overcomplicate things... ;)

Member Avatar for iamthwee

True, but now you've gone from storing int and char to storing char* with the overhead of marking which ones may need to later be converted to integers. No need to overcomplicate things... ;)

Well it depends how complex the OP's project is. We don't know what he/she is doing? When they used the word characters they could have meant char[].

In regards to overcomplicating things, an array or rather class specifically designed to handle both chars and ints is surely more complicated? If we use your code sample as an example?

How's the microsoft job going? Have you started yet?

In regards to overcomplicating things, an array or rather class specifically designed to handle both chars and ints is surely more complicated? If we use your code sample as an example?

Qutie possible. I'd say it's less complicated than using char*, but I think it gets subjective at that point. Using an array of integers seems like an acceptable solution unless the OP intended to store strings and numbers.

How's the microsoft job going? Have you started yet?

Not started yet. I'll start at the end of June, after I graduate and take a quick summer vacation ;)

I'd go the other way (int to char) so you don't lose data if the int value is >= 256...

You could use RUBY then you don't have to worry about silly issues like that.

....except that changing the language is not the topic under discussion here.

Btw there is a plethora of interpreted / compiled langauges out there which provide the same ease of use, so I guess Ruby isn't that spectacular after all....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.