PLEASE HELP ME

this is just part of a program which will count the occurrences of characters in a word. im trying to input a word into a string but when the loop finished the word is cut off. for example; i enter the word computer and the output is
comp
3
can anybody tell me why the null is being applied so early?
if i change the string size it makes the user input the word numerous times.
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
28
29
30
31
32
33
34
35
36
37
  #include<string>
#include<iostream.h>
#include<iomanip.h>
int main()
{
    char Word;
    int size = 15;
    char entireword[size];
    size = 0;
    int charnum;
    int loop = 0;
    char   a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;
    char const Letters = 52;
    int const Counter = 52;
    char NumLetters[Letters] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    int LetterCount[Counter]= {0};
    int spot = 0;
    char fullword[spot];
    char Info(int LetterCount[],char NumLetters[], char fullword[], int charnum);
    cout<<endl;
    cin>>Word;
    entireword[size] = Word;
    cout<<entireword[size];
    for(size =0 ;entireword[size] != '\0';size++)
    {
      cin>>Word;
      entireword[size] = Word;
      cout<<entireword[size];
    }
    cout<<endl;
    cout<< strlen(entireword); 
    cout<<endl;
    system("pause");
    return 0;
}
Last edited on
you are assuming that entireword contains a '\0' at exactly the place you need it. The reality is that you didn't put a '\0' in entireword so the behavior is undefined.

a better solution would be to use cin.getline
example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>
using namespace std;

int main()
{

    char fooArray[15];

    cout << "Please enter a word" << endl;
    cin.getline(fooArray, 15);

    for(int i = 0; i < strlen(fooArray); i++)
    {
        cout << fooArray[i];
    }

    cout << endl;

    cin.ignore();
    return 0;
}
Last edited on
wow thanks a lot. it helped me move closer towards finishing my program. is it cool if i come back should any problem pop up?
Topic archived. No new replies allowed.