??? I don't know how to say what's going on

So I wrote the program below as an assignment. It asks the user to enter a sentence and the program will tell them how many vowels, how many words, and how many characters. When I compile it, there's no errors, but when I run it and enter a sentence...I don't know how to explain what happens. It seems like it just keeps printing my stat lines...

I feel like it has something to do with the loop that checks for the end of the line. I tried "NULL" and "\n"; NULL makes it just sit there while \n makes it do what it does now. I got the \n from the debugger, it showed that was at the end of the sentence.

Please and Thank You for your help!!

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include<iostream>
#include<cstdlib>
#include<cctype>
#include<iomanip>

using namespace std;

//declare user functions
int isvowel (char letter);

int main()
{	
	//set variables
	const char SENTINEL = '~';

	char letter;	
	
	int word_count = 0;
	int vowel_count = 0;
	int char_count = 0;

	double mod_count = 0;
	
	//request sentence
	cout<<fixed<<showpoint;
	cout<<"Please enter a sentence or enter ~ to end the program"<<endl;
	cin.get(letter);
	
		
	//loop until end char
	while (letter != SENTINEL)
	{
		//loop until the end of the sentence		        
		while (letter != '\n')
 		{
 			char_count += 1;
    		
    		if (letter == ' ' || '.')
    		{
    			word_count += 1;
    		}
			
			//call on user defined function
			isvowel(letter);
			
			cin.get(letter);
		}
				
		//display totals
		cout<<"\n\nTotal number of vowels in your sentence: "<<setw(48)<<right<<vowel_count;
		cout<<"\nTotal number of words in your sentence: "<<setw(45)<<right<<word_count;
		cout<<"\nTotal number of characters in your sentence: "<<setw(50)<<right<<char_count;
		cout<<"\n\t(inluding spaces & punctuation)"<<endl;
	}
		
	//end program
	cout<<endl<<endl;
	system("pause");
	return 0;
  }

  //setup user functions
  int isvowel (char letter)
  {
	int vowel_count = 0;
	
	switch (letter)
	{
		case 'A':
		case 'a':
			vowel_count += 1;
			break;
			
		case 'E':
		case 'e':
			vowel_count += 1;
			break;
			
		case 'I':
		case 'i':
			vowel_count += 1;
			break;
			
		case 'O':
		case 'o':
			vowel_count += 1;
			break;
			
		case 'U':
		case 'u':
			vowel_count += 1;
			break;
			
		default:
			vowel_count +=0;
			break;
	}
	
	return vowel_count;
  }
closed account (2LzbRXSz)
First, you should store input in a string not a char. That way you can grab the full sentence. Use getline(cin, letter); now that it's a string.

Your loops are a little off. You have the right mindset - you were close, it just needed some reformatting:)
Here's what it should look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//loop until end char
    for (int i = 0; i < letter.length(); i++)
    {
        char_count += 1;
    
        if (letter[i] == SENTINEL)
        {
            exit(0);
        }
            
        if (letter[i] == ' ' || '.')
        {
            word_count += 1;
        }
        
        //call on user defined function
        isvowel(letter[i]);
    }

You need to use a for loop, that way you have a counter. It will increase until < the length of the string. You use this counter (int i) to loop through your string and examine each character.

Other than those things, I'm pretty sure everything was fine. I didn't keep the best track of what I changed though, so just in case that isn't it - here is what the code should look like once it's said and done.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<iomanip>

using namespace std;

//declare user functions
int isvowel (char letter);

int main()
{
    //set variables
    const char SENTINEL = '~';
    
    string letter;
    
    int word_count = 0;
    int vowel_count = 0;
    int char_count = 0;
    
    double mod_count = 0;
    
    //request sentence
    cout<<fixed<<showpoint;
    cout<<"Please enter a sentence or enter ~ to end the program"<<endl;
    getline(cin, letter);
    
    
    //loop until end char
    for (int i = 0; i < letter.length(); i++)
    {
        char_count += 1;
    
        if (letter[i] == SENTINEL)
        {
            exit(0);
        }
            
        if (letter[i] == ' ' || '.')
        {
            word_count += 1;
        }
        
        //call on user defined function
        isvowel(letter[i]);
    }
        
        //display totals
        cout<<"\n\nTotal number of vowels in your sentence: "<<setw(48)<<right<<vowel_count;
        cout<<"\nTotal number of words in your sentence: "<<setw(45)<<right<<word_count;
        cout<<"\nTotal number of characters in your sentence: "<<setw(50)<<right<<char_count;
        cout<<"\n\t(inluding spaces & punctuation)"<<endl;
    
    //end program
    return 0;
}

//setup user functions
int isvowel (char letter)
{
    int vowel_count = 0;
    
    switch (letter)
    {
        case 'A':
        case 'a':
            vowel_count += 1;
            break;
            
        case 'E':
        case 'e':
            vowel_count += 1;
            break;
            
        case 'I':
        case 'i':
            vowel_count += 1;
            break;
            
        case 'O':
        case 'o':
            vowel_count += 1;
            break;
            
        case 'U':
        case 'u':
            vowel_count += 1;
            break;
            
        default:
            vowel_count +=0;
            break;
    }
    
    return vowel_count;
}


If there's anything else you're confused about, let me know:)

Edit: I almost forgot!
1
2
3
4
 if (letter[i] == SENTINEL)
        {
            exit(0);
        }

calls exit(0); to exit the program - the 0 is to state that the program exited without error.

Edit 2: I also removed
1
2
cout<<endl<<endl;
system("pause");

cout<<endl<<endl; is incorrect - the correct way to do it is cout << endl;

I removed system("pause"); because system functions aren't recommended + I'm on a Mac so it doesn't work for me (feel free to put it back if you want to - I know some compilers just close if you don't put it there. However, if you need to pause your program in the future for some other reason, I recommend using http://www.cplusplus.com/reference/thread/this_thread/sleep_for/ )

Edit 3: I am very tired so please excuse any grammatically incorrect sentences I know I shouldn't be posting if I'm tired, but ironically exhaustion doesn't affect my programming knowledge - only my ability to speak proper English! Haha.
Last edited on
Thank you so much for your help! The reason I had the double endl is because I wanted it to skip a couple line before displaying the pause. And my teacher wants us to use the system("pause"), but thank you for the link! This works so much better!!

Edit:
Ok so I compiled and tried running and it tells me "letter" and "i" were not delcared in this scope. This is in my isVowel function and the switch.
Last edited on
closed account (2LzbRXSz)
No problem!

Hmm, I just ran it again and I'm not getting that error. Did you add anything new to your isvowel function since I edited it?
I tried putting "[i]" after each instance of [letter] because it's not counting my vowels.

Also, for some reason it's counting each letter as a word.

**lol on edit 3, I'm the same!**

Edit:
So I figured out part of the problem on my vowel count. It keeps resetting back to zero. So I decided to define that in the main func instead. I have found that somewhere, some how after the end of my vowel function, it is reset to zero. I tried adding "vowel_count = vowel_count" in my loop, but that does nothing.
Last edited on
closed account (2LzbRXSz)
Your function doesn't need to loop through a string and check each character because letter[i] == char (letter[i] is a single character in a string).

vowel count resets to 0 because it's called in the for loop, so each time the function is called, it resets (because of vowel count = 0).

I'm posting from my phone right now, but I'm about to get on my computer. I'll play around with the code and post what I come up with! :)
Last edited on
closed account (2LzbRXSz)
I left your function alone since the last time I edited, but I changed the call to the function.

Rather than isvowel(letter[i]);, I changed it to vowel_count += isvowel(letter[i]); (you had a vowel_count variable declared, but I don't think you were adding the number of vowels to it, which is why it was showing up as 0).

Edit: I've noticed a new problem - word_count is printing the same value as char_count.

Edit 2: I've found the error!
if (letter[i] == ' ' || '.') is invalid.
It should be
if (letter[i] == ' ' || letter[i] == '.')
if you don't tell it to check letter[i] == '.' it won't assume to based on the || operator. || allows you to add another condition, but it has to be a full condition:)

Now - I think everything should be working great. If there's anything else you need help with or anything your confused about I'm more than happy to help!
Last edited on
Thank you!! Works great!!
closed account (2LzbRXSz)
No problem:)
Topic archived. No new replies allowed.