How to change the size of an array based on input?

I'm wondering what the easiest way to shorten an array based on user input would be? My program allows for a maximum of 140 characters to be entered into the array. However, if the user only enters say 30 characters, I need the array to be shortened to that amount. The program doesn't ask the user how many they are going to enter, so I need some way for the program to determine how many had been entered and resize the array based on that input? I saw an operation called realloc, but my problem is I don't know how to determine how many characters had been entered by the user?
rather than resizing the array, just stuff a flag value of some sort into the array at the element just past the last one entered. This is how native c style strings (which are really just arrays of characters) mark their termination. the null character '/0' is inserted at the end.
Well, I have absolutely no idea how to do that and every search I do for this stuff on the internet brings me up stuff unrelated to my particular desired outcome. I don't know how to add onto an array that is already existing as I don't know how to find where the end of it was. I thought our professor told us that if you only enter x amount of characters into an array, at the point x+1, it adds the "\0" automatically. So, I need to figure out the easiest way to do that. Really, all I need to do is be able to count how many characters were entered into the array. I don't necessarily need to shorten the array, so whatever way would make that easiest would be great.
Last edited on
Also, I have tried using strlen to find how many characters have been entered, but it gives me an error message stating "strlen was not declared in this scope."
I'm not saying to add onto an array, just to stuff a value into it. Let's say you want to use -1 as the flag value to mark the end. You say you have an array of 140 character, but if the user only enters 3 characters as data (for example 'a', 'b', and 'c'). then stuff the flag value into the 4th element.
the array would look like:
arr[0] = 'a'
arr[1] = 'b'
arr[2] = 'c'
arr[3] = -1

then the loops that process the array can look for the flag value to know when to end.
Thanks for the help. I actually found where my problem was. strlen gives me exactly what I need. I just had to include '#include<cstring>" to get it to work.
Ugh, nevermind. After trying a few things out, I realized that it only counts until there is a space (it's a character array with multiple words). Is there a different operation I can use to count until it reaches the end of ALL words in the array rather than just counting till the end of the first?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstring>

const int SIZE = 30;

int main(){

	char str[SIZE] = { NULL };

	std::cout << "Enter a string: ";
	std::cin.getline(str, SIZE);
	std::cout << "You entered " << strlen(str) << " characters.\n";

	return 0;
}
Already tried that mobotus, but as soon as it hits a space between words, it stops counting there. I need something that will count those spaces and for some reason, strlen isn't doing it for me. It counts fine until I hit a space, but that's where I keep hitting the brick wall.
I also tried this, but I'm running into the same issue:

1
2
3
4
5
6
for (int j=0;j<141;j++){
                if (searchText[j]=='\0'){
                    searchLength=j-1;
                    cout << j << endl << endl;
                    MainMenu ();
                    break;


As soon as it hits a break, it's acting as if it found a '\0', but I'm not sure why?
Did you try the program provide by mobotus? The strlen() function stops when it encounters the end of string character ('\0') not any other whitespace character. However if you used the extraction operator then that operation stops when it encounters a whitespace character. The getline() function on the other hand stops when it encounters a new line character or reaches the specified size.

Wow, so I figured out that my problem was in a different part of my code. I somehow overlooked that I used 'cin >> array' instead of 'cin.geline(array, size)'. Don't even ask how I overlooked that. I have over 200 lines of code and have been working on this project now for 10hrs straight.

Thanks for the help. I really appreciate it.
Topic archived. No new replies allowed.