文の文字数を数える次の関数があります
// A function to get a length of any sentence.
int length(char *str){
int size = 0;
while(str[size] != '\0'){
size++;
}
return size;
}
int main(){
char *name = new char;
int cnt = 0;
cin.getline(name, length(name));
cout << length(name) << endl;
return 0;
}
しかし、文を入力してその長さを取得すると、文の長さがわずか2文字であることがわかりました。
なぜそれが起こるのですか?