Counting characters in a c string

In this program it is supposed to have a user input a string and then the program will, depending on the user's choice, output the number of vowels, consonants, or both and also gives the user the ability to add another sentence to be counted up. The program works in that the first sentence inputted by the user can be counted properly but I get errors when the user selects to add another sentence. There is clearly a problem with the allocation of memory with that option but I can't figure out what would make it right. Any help would be 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
  #include <iostream>
#include <cstring>
using namespace std;

int vowelCount(char*);
int consonantCount(char*);

int main()
{
const int length = 100;
char *sentence;
sentence = new char[length];
int vowel=0, consonant=0, allLetters=0;
char choice;
bool not_done=true;

cout << "Enter a sentence: ";
cin.getline(sentence, length);
vowel = vowelCount(sentence);
consonant = consonantCount(sentence);
allLetters = vowel + consonant;

while(not_done){
    cout << "\tMenu\n";
    cout << "A) Count the number of vowels in the string\n";
    cout << "B) Count the number of consonants in the string\n";
    cout << "C) Count both the vowels and consonants in the string\n";
    cout << "D) Enter another string\n";
    cout << "E) Exit the program\n";

    cout << "Enter your choice: ";
    cin >> choice;

    switch(choice)
    {
            case 'a':
            case 'A':
                cout<<"Number of vowels is " << vowel << endl;
                break;
            case 'b':
            case 'B':
                cout<<"Number of consonants is " <<consonant <<endl;
                break;
            case 'c':
            case 'C':
                cout<< "Number of letters is " << allLetters << endl;
                break;
            case 'd':
            case 'D':
                delete [] sentence;
                sentence = new char[length];
                cout<< "Enter a new sentence: " << cin.getline(sentence, length)  << endl;
                break;
            case 'e':
            case 'E':
                not_done=false;
                break;
            default:
                cout << "Error: '" << choice << "' is an invalid selection -";
                cout <<  "try again"<< endl << endl;
                break;
    }
};
return 0;
}

//function definitions 
There is clearly a problem with the allocation of memory with that option but I can't figure out what would make it right.

Repeatedly delete[]'ing and new[]'ing sentence is not needed in this case, because the length is a hard-coded constant.

So you could just allocate memory at startup and deallocate it at the end.
Even better, use a "regular" array instead of a dynamically allocated one.

char sentence[length];

The program works in that the first sentence inputted by the user can be counted properly but I get errors when the user selects to add another sentence.

What errors? Please remember, always copy-paste the errors.

Anyway, I see that even after you read the new sentence, you don't run the counting functions to update your vowel and consonant variables.
Last edited on
The line:

cout<< "Enter a new sentence: " << cin.getline(sentence, length) << endl;

isn't doing what you intend it to do. The way you have it at the beginning of your program is fine, just replicate it in your switch statement.
The error when the user inputs "d" is that some memory location is outputted in hexadecimal.

ex:"Enter your choice: d
Enter a new sentence:0x477908"
Right, and that's because you're trying to write to the standard stream and read from it at the same time (in the same statement).

This works just fine:

 
cout << "Enter a new sentence: " << endl; cin.getline(sentence, length);


It's pretty much the same as what you wrote at the beginning of your main() function.
Yulingo, I'm not sure I understand.

1
2
cout << "Enter a sentence: ";
cin.getline(sentence, length);

and
 
cout<< "Enter a new sentence: " << cin.getline(sentence, length)  << endl;


the second one is essentially a replication already of the first, what did you mean by that?
Well the two pieces of code in your last post aren't the same. One has two statements, the other one only has one. The one on the bottom will give you the error you saw, and the one on top doesn't.
haha, you're right, didn't think about the operators.
It can get tricky sometimes, for sure.
this is my current code.

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
#include <iostream>
#include <cstring>
using namespace std;

int vowelCount(char*);
int consonantCount(char*);

int main()
{
const int length = 100;
char sentence[length];
int vowel=0, consonant=0, allLetters=0;
char choice;
bool not_done=true;

cout << "Enter a sentence: ";
cin.getline(sentence, length);

while(not_done){
    vowel = vowelCount(sentence);
    consonant = consonantCount(sentence);
    allLetters = vowel + consonant;

    cout << "\tMenu\n";
    cout << "A) Count the number of vowels in the string\n";
    cout << "B) Count the number of consonants in the string\n";
    cout << "C) Count both the vowels and consonants in the string\n";
    cout << "D) Enter another string\n";
    cout << "E) Exit the program\n";

    cout << "Enter your choice: ";
    cin >> choice;

    switch(choice)
    {
            case 'a':
            case 'A':
                cout<<"Number of vowels is " << vowel << endl;
                break;
            case 'b':
            case 'B':
                cout<<"Number of consonants is " <<consonant <<endl;
                break;
            case 'c':
            case 'C':
                cout<< "Number of letters is " << allLetters << endl;
                break;
            case 'd':
            case 'D':
                cout<< "Enter a new sentence: ";
                cin.getline(sentence, length);
                break;
            case 'e':
            case 'E':
                not_done=false;
                break;
            default:
                cout << "Error: '" << choice << "' is an invalid selection -";
                cout <<  "try again"<< endl << endl;
                break;
    }
};
return 0;
}

//function definitions 


the initial sentence entered works with the menu options but the option "d" where a new sentence is entered still doesn't work. here is what happens when it compiles:

Enter a sentence: hi there
Menu
A) Count the number of vowels in the string
B) Count the number of consonants in the string
C) Count both the vowels and consonants in the string
D) Enter another string
E) Exit the program
Enter your choice: d
Enter a new sentence: Menu
A) Count the number of vowels in the string
B) Count the number of consonants in the string
C) Count both the vowels and consonants in the string
D) Enter another string
E) Exit the program
Enter your choice:

it simply skips right back to the menu, I've tried rearranging the code a bunch but can't figure out what I need to change to make it work. any help would be appreciated, and thank you for the help so far.
does anyone have suggestions? i am still stuck
it simply skips right back to the menu
What are you trying to do? That seems like it is doing what you want..Go back to the menu with the new string then ask what they want to do with the new string.
I'd like the user to be able to enter a new sentence there which the user could then count the vowels and consonants, etc. Right now it simply loops right back to the menu and doesn't allow the user to enter a new string.
Last edited on
Ah sorry I didn't realize you called cin with operator >> right before calling cin.getline() that causes a slight problem because of the way they both read. You are going to want to throw in an ignore before calling cin.getline() because there is a newline left in the buffer.
awesome, that fixed it, funny how simple solutions can be. thanks.
Topic archived. No new replies allowed.