I need to use an array of chars to input chars from the console but I don't know how many chars will be actually inputed. How can I have an array of chars whithout knowing its size in advance?
I could put the array with a very high size but that would be a huge waste of memory, I guess.
The array will hold every char inputed from the console till it gets a specific char. It is easy to test the condition but my problem is really how to have such an array that will be able to store any number of items as it gets.

I cannot use std::string because my program specification states array, so I have to use arrays in one way or another

I need to use an array of chars to input chars from the console but I don't know how many chars will be actually inputed. How can I have an array of chars whithout knowing its size in advance?
I could put the array with a very high size but that would be a huge waste of memory, I guess.
The array will hold every char inputed from the console till it gets a specific char. It is easy to test the condition but my problem is really how to have such an array that will be able to store any number of items as it gets.

I cannot use std::string because my program specification states array, so I have to use arrays in one way or another

Well, you need to either use a dynamic array or, as you say, make the array bigger than will ever be needed, which will take a lot of memory. You can pick some medium "best guess" size, say 1000, then resize the array dyanamically if more memory is needed. Basically write your own vector. I'm assuming that if you can't use string, you can't use vector, but it's the same idea.

I don't know how familiar you are with dynamic arrays. Here's a tutorial. There are lots of other tutorials and lots of threads on Daniweb.

http://www.cplusplus.com/doc/tutorial/dynamic.html

ok.. lets say you creat an array of size 1000. Now, you will have to keep count of every input. try to use a getc(), and repeat it until you get to a specific character. this way, if you near the end point in array's size, say you are at 990, you can code a function to expand the array by 200 more. This way you will be able to keep a close watch on the input. Also, for better memory management, you can code a function to shorten the array after it contains the necessary data. :)

Also, if you are inputing straingth from the console, you may try getchar(), the one i mentioned above is for reading from streams. :IDEA:

Also, if you are inputing straingth from the console, you may try getchar(), the one i mentioned above is for reading from streams. :IDEA:

There's and edit button for a reason :P

@OP
You may think it looks a bit confusing but it's really not that complex

Chris

There's and edit button for a reason :P

@OP
You may think it looks a bit confusing but it's really not that complex

Chris

LOL!! ya i remembered it after i posted the second one! sorry!

May be you can use some container in STL, such as vector.

Perhaps you should pay attenyion to the posts....the idea looks to be that he isn't allowed to hence why it was suggested he implemented his OWN version of a vector

Chris

This article has been dead for over six months. Start a new discussion instead.