I am making a very simple programs just trying to learn c++ as a beginner. It is designed to simply count the number of vowells and the number of consenants in a short sentence. I have come across an interesting little dilema while doing it.
First of all here is the code:
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int main()
{ int vowells = 0, consenants = 0;
char sentence[100];
char alphabet[] = {'a','e','i','o','u','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
cout << "Please type in a sentence (up to 100 characters max, lower case only): ";
cin >> sentence;
cin.ignore();
cout << "Your sentence was: " << sentence;
for (int i=0; i < sizeof(sentence); i++)
{
if (sentence[i] == alphabet[0]||sentence[i] == alphabet[1]||sentence[i] == alphabet[2]||sentence[i] == alphabet[3]||sentence[i] == alphabet[4])
{vowells++;}
else if (sentence[i] == alphabet[5]||sentence[i] == alphabet[6]||sentence[i] == alphabet[7]||sentence[i] == alphabet[8]||sentence[i] == alphabet[9]||sentence[i] == alphabet[10]||sentence[i] == alphabet[11]||
sentence[i] == alphabet[12]||sentence[i] == alphabet[13]||sentence[i] == alphabet[14]||sentence[i] == alphabet[15]||sentence[i] == alphabet[16]||sentence[i] == alphabet[17]||sentence[i] == alphabet[18]||
sentence[19] == alphabet[20]||sentence[i] == alphabet[21]||sentence[i] == alphabet[22]||sentence[23] == alphabet[24]||sentence[i] == alphabet[25])
{consenants++;}
}
cout << "\nThe number of vowells is: " << vowells;
cout << "\nThe number of consenants is: " << consenants;
cin.get();
}
Sorry it looks really messy. Basically after the cin >> sentence; line i put the cin.ignore() function to get rid of the enter key pressed after inputing the sentence. At the end of the function the cin.get() is simply suppose to serve as a breaking point so that the program wants another input before it closes. If i input just 1 word with no spaces, the program runs and pauses at the end as desired. If i put in multiple words with spaces, it just runs and closes immediately without giving me time to even see it. I assume this is because of the spaces for some reason... Though i'm not sure why they would affect it in this way.
So basically, is it the spaces that are giving me my problems? If so how do i go about either getting rid of them or at least having the program ignore them?
Thanks!
EDIT*** So i was told that i could use the windows Sleep() command to get it to pause, and that worked. Now the problem is that as another person commented, the cin function only accepts the first word, and doesn't take into account the rest of the sentence. So i guess i need to get rid of the spaces or somehow use a different input function to get it to work properly. Any suggestions on how to go about doing this?