My program stops after the first function and does not go to the next.

Hello Everyone,
I am very new to C++. The program that I am trying to write is one that allows the user to type in characters into an array then it is suppose to count how many letters were inputted before the '.' and then print it out. So I wrote two functions one to read the letters and the other to print them out with their counts. It compiles fine but when I put in a bunch of random characters ending with a '.' and hit enter the run screen just goes to the next line and stops and does not continue on to the print function. I have not seen an error like this before and am clueless as to how to fix it so it prints. Any hint or clues would be appreciated.

I pasted the program below but did not know how to add the line numbers to the right hand side. Sorry.

code]#include <iostream>

#include <fstream>
#include <string>

using namespace std;

struct alpha {
char letter;
int lCount;
};

void readLetters(alpha list[], int &letterCount);
void printList(const alpha list[], int letterCount);

int main()
{

alpha list[80];
int letterCount;


readLetters(list, letterCount);
printList(list, letterCount);


system("pause");
}

void readLetters(alpha list[], int &letterCount)
{
char letter;
char character;
int count=0;

cout << "Enter a sequence of characters (end with '.'):" ;
cin >> character;

while(character != '.') {
character = islower(character);
if(isalpha(character)){
list[character].letter;
list[character].lCount++;
count++;
}
letterCount=count;
}
}

void printList(const alpha list[], int letterCount)
{

for (int count = 0; count < letterCount; count++){
cout << list[count].lCount << " ";
cout << list[count].letter << endl;
}
}[[/code]
First of all you do not initialize elements of array list.
So the code ist[character].lCount++; is invalid because the initial value the expression was not defined.
Also you array is defined as alpha list[80];. However the ASCII code of, for example, 'a' is 101 so you are trying to change memory outside your array.

This code

1
2
3
for (int count = 0; count < letterCount; count++){
 cout << list[count].lCount << " "; 
cout << list[count].letter << endl; 


also invalid because letterCount does not contains the number of filled elements of the array. It only contains the number of entered letters which can be duplicated.
Thank you Vlad for your comments, they were very helpful. I made some changes and the program is still stopping on me. The changes I made were to get rid of letterCount for it dawned on me after reading your comments that I did not need it for I know the array is 26. Then I think I initialized the array list by saying alpha list [] = {{'a', 0}, {'b',0}....} all the way through z. I also changed the while statement because I thought it looked like it was just putting a letter into the array and changing the count to 1 instead of looking through the array for the correct letter and then changing the count. Any more hints or clues are appreciated. Thanks!

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
struct alpha {
       char letter;
       int lCount;
};

void readLetters(alpha list[]);
void printList(const alpha list[]);

int main()
{
    
    alpha list[]={{'a', 0}, 
                  {'b', 0},
                  {'c', 0},
                  {'d', 0},
                  {'e', 0},
                  {'f', 0},
                  {'g', 0},
                  {'h', 0},
                  {'i', 0},
                  {'j', 0},
                  {'k', 0},
                  {'l', 0},
                  {'m', 0},
                  {'n', 0},
                  {'o', 0},
                  {'p', 0},
                  {'q', 0},
                  {'r', 0},
                  {'s', 0},
                  {'t', 0},
                  {'u', 0}, 
                  {'v', 0},
                  {'w', 0}, 
                  {'x', 0},
                  {'y', 0},
                  {'z', 0}};
        
    readLetters(list);
    printList(list);
    
          
  system("pause");
}

void readLetters(alpha list[] )
{
    
    char character;
    
    
    cout << "Enter a sequence of characters (end with '.'):" ;
    cin >> character;
    
    while(character != '.') {
        character = islower(character);
            if(isalpha(character)){
               for (int count=0; count < 26; count++){
                   if(list[count].letter==character){
                       list[count].lCount++;
                   }
               }               
            }
    }    
}


void printList(const alpha list[])
{   
    for (int count = 0; count < 26; count++){   
        cout << list[count].lCount << " "; 
        cout << list[count].letter << endl; 
    }
} 
It is bettter to use std::map<char, unsigned int> instead of your array of type alpha.

For example

1
2
3
4
5
6
7
8
9
10
11
12
void readLetters( std::map<char, size_t> &m )
{
    char c;
    
    cout << "Enter a sequence of characters (end with '.'):" ;
    
    while ( ( cin >> c ) && ( c != '.'  ) ) 
    {
        c = islower( c );
        if ( isalpha( c ) ) m[c]++;
    }    
}
Last edited on
In readLetters you only get one character from the user.

You then loop endlessly on that character which never changes.

line 56 should be tolower, not islower. Note that this needs to be corrected in Vlad's version as well, if you should experiment with that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void readLetters(alpha list[] )
{   
	cout << "Enter a sequence of characters (end with '.'):" ;

	char character ;
	while ( cin >> character  &&  character != '.' ){
		character = tolower(character);

		if(isalpha(character)){
			unsigned index = 0 ;
			while ( list[index].letter != character )
				++index ;

			++list[index].lCount ;               
		}
	}    
}
Last edited on
It works! I don't think I ever would have been able to figure it out. Thank you both so much for the help and explanations!
Topic archived. No new replies allowed.