help please!!!!

how can I terminate this loop using \n (terminate the loop upon pressing enter on keyboard ) this is where i've got so far...
#include <iostream>

using namespace std;

bool isVowel (char);
int main()
{

char ch;
int ctr=0;
cout<<"Enter a sentence. "<<endl;

cin>>ch;

while (ch != '.') //this is what I'm stuck at what to write here in order for the loop to terminate upon encountering a newline???????
{

if (isVowel(ch))
ctr++;
cin>>ch;

}


cout<<"There are "<< ctr << " vowels in this sentence."<<endl;




system("pause");
return 0;

}


bool isVowel (char c)

{

switch (tolower(c))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;

}
return false;

}
use get() function to take the input....

in place of "cin>>ch" use ch=cin.get()

and then in the condition " ch != '\n' "

this should work
I haven't studied get() function yet.. so I can't use it to do this assignment....I want to terminate the loop upon encountering a newline ....
i tried( a different approach) to change the while condition to while (cin)
but then i got stuck even more!!!
cin >> ch is eating the end of line marker. Try ch = cin.get();
As I said previously I can't use get because I haven't learned that yet and we are not allowed to use funtions we didn't learn in our assignment.
@ OP: That's not a valid excuse if you're using C++. Trust me when I say that your instructor, no matter how stupid you might think they are, knows what the STL "get()" member function does. If it would make you feel better to have an excuse ready then tell them that you saw it when you were skimming through the text book and you thought it would work in this instance; it's bound to be somewhere in there.

C++ isn't english class, people are rarely going to have the same answers to a problem. Even though some answers are "more right" then others, the only thing that makes an answer wrong is if it does not work.
Last edited on
What they are trying to tell you is that you cannot get a string with cin>>ch because the stream stops at the first white space.
You need to use cin.get() or cin.getline() or in C++ getline(cin,s).
What you type after getline(cin,s) gets stuffed into string 's'. std::cout<<s; //prints the string.
There is no need to use a while loop........pressing enter ends the string and provides a new line with '\n' .
hope this helps.
Topic archived. No new replies allowed.