How can we iterate a variable sized char array? Suppose the maximum size of the array is 64, but the user input is always far less than that, how do we find out when the last element has been dealt with?
Determine end of variable char array
Page 1 of 15 Replies - 998 Views - Last Post: 01 November 2009 - 10:40 AM
Replies To: Determine end of variable char array
#2
Re: Determine end of variable char array
Posted 01 November 2009 - 12:10 AM
Well, I think I know what you are getting at. Unless you are taking a class that is requiring you to use a standard array, use a vector. They automatically resize and you can get the last element by simply setting up an iterator and getting the last element. Like this.
It is a little more complicated using a standard array, but this should work without having to use a while loop.
If you have some code, feel free to post it.
I hope this helps!
vector<char> MyVector; vector<char>::iterator pos; //Load values into the vector... //Then retrieve what you want. //this gives you the last non-null element pos = MyVector.end() - 1;
It is a little more complicated using a standard array, but this should work without having to use a while loop.
//This gives you the amount of elements in the array int elements = sizeof(YourArray)/sizeof(int) //for sizeof(int), replace int with whatever the type of your array is. //Now that you know how how many elements are in your array you can find the last element like this. cout << array[elements - 1] //subtract one because the array is 0 to N-1 not 1 to N
If you have some code, feel free to post it.
I hope this helps!
This post has been edited by Spencer William: 01 November 2009 - 12:22 AM
#3
Re: Determine end of variable char array
Posted 01 November 2009 - 12:24 AM
Whoops...I forgot the title of this post said it was a "char array". In that case replace sizeof(int) with sizeof(char).
#4
Re: Determine end of variable char array
Posted 01 November 2009 - 04:59 AM
Autocrat, on 31 Oct, 2009 - 11:04 PM, said:
How can we iterate a variable sized char array? Suppose the maximum size of the array is 64, but the user input is always far less than that, how do we find out when the last element has been dealt with?
Depends what you mean by "variable sized char array". Is it something like this, with and example of how to get user input:
char str[64]; fgets(str,60,stdin);
If so then fgets will add a \n and \0 at the end of the input, and you can check for this:
for (int i=0; str[i]; i++)
{
if (isprint(str[i]))
printf("You entered the following character: '%c'\n",str[i]);
}
So the input "Hello" will (should) result in the following output:
You entered the following character: 'H'
You entered the following character: 'e'
You entered the following character: 'l'
You entered the following character: 'l'
You entered the following character: 'o'
This post has been edited by gronk: 01 November 2009 - 05:00 AM
#5
Re: Determine end of variable char array
Posted 01 November 2009 - 10:18 AM
#6
Re: Determine end of variable char array
Posted 01 November 2009 - 10:40 AM
By gronk is the best answer. Thanks to all of you though.
@Spencer William, thanks but I don't do vectors at the moment
@#define, sorry but I forgot to mention I am using C++.
@Spencer William, thanks but I don't do vectors at the moment
@#define, sorry but I forgot to mention I am using C++.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|