logical size of a character array

I'm studying for loops now and I'm trying to incorporate a bodiless for loop that will give me the logical size of a character array. The array's physical size would be 80.

cout<<"Enter your name: "
cin>>name;

Ostensibly, "name" will have white space characters which I do not want to count. I can't figure out if this would be done by counting the entire array and subtracting the white space characters or only counting the printable characters.

Any hints would be appreciated. I have a feeling this is much simpler than I'm making it.
hi,

firstly cin will only input one word and splits at the first whitespace it encounters.

so i would use

 
cin.getline(name,80)


and then use a loop like below

1
2
3
4
5
6
7
8
9
        int	count = 0;

	for (int i=0;i<strlen(name);i++)
	{
		if (name[i] != ' ')
		{
			count++;
		}
	}


you will also need to include <stdio.h> to use strlen

Hope this helps
Shredded
I forgot, I can't use strlen. :(
I think the trick would be to loop a counter while evaluating for the '\0' character. Since that marks the end of a c-string, it's index would be the same as the logical length.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream.h>

main()
{
      char string[80] = "I am browni3141";
      int i, count = 0;
      for (i = 0; string[i]!='\0'; i++)
          cout << string[i];
      cout << "\nThe null terminator was found at " << i << ".\n";
      for (i = 0; string[i]!='\0'; i++)
          if (string[i] != ' ')
             count++;
      cout << "The string contained " << count << " nonspace characters.\n";
      system("pause");
      return 0;
}


Lines 7-9 demonstrate what Tabian said. The rest of the code demonstrates what shredded gave with an example string.
Topic archived. No new replies allowed.