strlen function

i wrote this programme just to check out the strlen function but it allwaya return the character count as 1

please help


#include<iostream>
#include<cstring>
using namespace std;

int main()
{

char data[50];
int size=0;;

cout<<"please input the word\n";
cin>>data[50];
size=strlen(data);
cout<<"the number of letters is "<<size;

return 0;

}



I've just compiled and run it on my UNIX box without problem. You could try changing cin>>data[50]; to cin>>data;. Other than that (and an extra semi-colon on you int size = 0; line which shouldn't cause this) I can't see anything wrong.

If that doesn't fix it, post your compiler details, someone may be able to be of more help than me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    char data[50];
    int size = 0;   //removed extra ; (would not cause an error)

    cout << "please input the word\n";
    cin >> data;  //changed from: cin >> data[50];
    size = strlen(data);
    cout << "the number of letters is " << size;

    return 0;
}


EDIT: Beaten by bnbertha, but on my Windows box I needed to change cin >> data[50];

Last edited on
now that the problem is the programme compiles perfectly but as the chararcter count it returns only 1 as the value of size
What compiler are you using?

EDIT: and are you running this exact code or have you cut and pasted a piece of it here?
Last edited on
yes indeed & i am using Vi editor in unix environment
OK. Are you just using ':w' to write the code? Are you compiling it each time and what is the command line you are using to compile the code?

EDIT I'm thinking you may not be running the executable you've just built.
Last edited on
ok i just figured it out it was not any thing with the code it was a stupid computer & OS any way thankx all you people it was really help full thankx again
Topic archived. No new replies allowed.