How to include spaces in counting string characters

So i have this program that's supposed to count all the characters including the spaces without using strlen but it only counts the number of the first word's characters and it does not include the rest of the words after the first space.
how do i include spaces in the count?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace std;
int strlen(char [ ]);
main ()
{
char mess[100];
int n;
cout<<"\nEnter a message:\n\n ";
cin>>mess;
n = strlen(mess);
cout<<endl<<n<<" character(s)";
main();
}

int strlen (char x[ ])
{	int counter = 0;
	for (int index = 0 ; index < 100; index++)
	{
			if (x[index] == '\0')
			break;
		counter++;
	}
	return counter;
}
Change
 
    cin>>mess;
to
 
    cin.getline(mess, sizeof(mess));


Also don't try to call main() as though it was an ordinary function. It is not valid C++.

If you want to repeat the processing, use a while loop.
http://www.cplusplus.com/doc/tutorial/control/
my professor usually uses main () instead of an int main (). he hasn't explained to us the difference bet. the two.

cin.getline(mess, sizeof(mess)); worked though. thanks!
I was referring to this, it is not valid C++:
1
2
3
4
5
int main ()
{
    // some other code
    main(); // INVALID attempting to call main() 
}


instead do something like this
1
2
3
4
5
6
7
int main ()
{
    while (true)
    {
        // some other code
    }
}

- use whatever condition you feel is appropriate to control the while loop. My example gives an infinite loop.
i still have to go over while and do-while loops since i only know how to use if-else. i'll take note of that anyhow
Ok. For now, you could use a for loop if you wanted to repeat something - you already seem familiar with those.

My reason for pointing these things out is that sometimes people learn things which are non-standard, and sooner or later may find themselves using a different compiler which simply will reject the non-standard usage. Or similarly, find themselves working in an environment (or maybe at a job interview) where such usage might not be appropriate.
Topic archived. No new replies allowed.