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.