hey guys...my code is working fine..only problem i have is my first do-while loop doesnt work...every time i try to compare the word given by the user to 'q' ||'Q'...it gives an error saying no match for the operator..please help me ....thanks!

#include <iostream>
#include <string>
using namespace std;
bool isVowel(char c); // returns true if c is a vowel and false otherwise
int countVowels(string s); // returns the number of vowels in s.
int main ()
{
	string word;
	int answer;
	do
	{
		cout << "Please enter a word or 'q' to quit: ";
		cin >> word;
		answer = countVowels(word);
		cout << answer << endl;
	}
	while (word != 'q' || word != 'Q')
		return 0;
}
int countVowels(string s)
{
	int i;
	int total = 0;
	for (i = 0; i < (s.length()); i++)
	{
		if (isVowel(s[i]))
			total++;
	}
	return total;
}
bool isVowel (char c)
{
	bool status;
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		status = true;
	else
		status = false;
	return status;
}

by doing a comparison for 'q' you are comparing a string to a char which is not supported. first thing to try "q"... then maybe more errors may become apparent...

How about if I make 'q' a string and than do the comprasion using strcmp or is it possible to insert a bool variable to validate the while ? And thanks again for your help

How about if I make 'q' a string and than do the comprasion using strcmp or is it possible to insert a bool variable to validate the while ? And thanks again for your help

just replace 'q' with "q" and 'Q' with "Q". Still doesn't work, think about the logic in you comparision statement...

just replace 'q' with "q" and 'Q' with "Q". Still doesn't work, think about the logic in you comparision statement...

Still doesn't work I even tried to make second variable a string still doesn't work :(

Still doesn't work I even tried to make second variable a string still doesn't work :(

OK, now focus on your while() loop's exit condition. How does it actually work, given the way you have coded it?

OK, now focus on your while() loop's exit condition. How does it actually work, given the way you have coded it?

yes, thankyou...

do
{
... body of loop
} while ( word != "q" || word != "Q" );

will execute body of loop when?

When
(word != "q" || word != "Q")
evaluates to true...

so...

Well it's a do while loop thus the way I coded it if while loop is not equal to q repeat the loop n if the string contains q than terminate the program .. But still if I use to strcmp it would compare two char values not a string and char .. I'm confused :((

o o sorry I didn't see the code u made ok so now it makes sense so I should make it ==0 to see if it's true :)

Still stuck I used ((word!="q")==0) it would run te program however if I use "q" it will not terminate the program :(

Still stuck I used ((word!="q")==0) it would run te program however if I use "q" it will not terminate the program :(

Right... the whole expression needs to evaluate to false for the loop to exit.

while( word != "q" || word != "Q" );

essentially breaks down to....

while( a || b )

where a is ( word != "q" )
and b is ( word != "Q" )

so if either one of these is true the whole expression evaluates to true. If word == "q" making expression a false then what happens to expression b?

thanks for all the help guys:)
i just modified the code and now it works
thanks again
here s the final code....pls let me know if you something looks wrong to guys
thanks again!!!

#include <iostream>
#include <string>
using namespace std;
bool isVowel(char c); // returns true if c is a vowel and false otherwise
int countVowels(string s); // returns the number of vowels in s.
int main ()
{
	string word;
	int answer;				//variable to store the final answer
	do						//do-while loop to play(restart) game
	{
		cout << "Please enter a word or 'q' to quit: ";
		cin >> word;
		if (word == "q" || word == "Q")	//terminate program if user enters q or Q
		{
			cout << "GoodBye!" << endl;
		}
		else
		{
			answer = countVowels(word);	//run the function countVowel and assign answer to asnwer variable
			cout << answer << endl;		//output total number of vowels
		}
	}
	while ((word == "q" || word == "Q") == 0);		//do while loop to repeat the program
	return 0;
}
int countVowels(string s)		//function to calucate the total vowals
{
	int i;
	int total = 0;
	for (i = 0; i < (s.length()); i++)	//loop to change char value and set max to string length s
	{
		if (isVowel(s[i]))		//if bool expression returned is true than add 1 to total
			total++;
	}
	return total;
}
bool isVowel (char c)	//check
{
	bool status;	//check with bool variale to see if char is vowel
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		status = true;	//return true if char is vowel
	else
		status = false;	//return false if char is not a vowel
	return status;		//assign status to the funcisVowel
}

thanks for all the help guys:)
i just modified the code and now it works
thanks again
here s the final code....pls let me know if you something looks wrong to guys
thanks again!!!

#include <iostream>
#include <string>
using namespace std;
bool isVowel(char c); // returns true if c is a vowel and false otherwise
int countVowels(string s); // returns the number of vowels in s.
int main ()
{
	string word;
	int answer;				//variable to store the final answer
	do						//do-while loop to play(restart) game
	{
		cout << "Please enter a word or 'q' to quit: ";
		cin >> word;
		if (word == "q" || word == "Q")	//terminate program if user enters q or Q
		{
			cout << "GoodBye!" << endl;
		}
		else
		{
			answer = countVowels(word);	//run the function countVowel and assign answer to asnwer variable
			cout << answer << endl;		//output total number of vowels
		}
	}
	while ((word == "q" || word == "Q") == 0);		//do while loop to repeat the program
	return 0;
}
int countVowels(string s)		//function to calucate the total vowals
{
	int i;
	int total = 0;
	for (i = 0; i < (s.length()); i++)	//loop to change char value and set max to string length s
	{
		if (isVowel(s[i]))		//if bool expression returned is true than add 1 to total
			total++;
	}
	return total;
}
bool isVowel (char c)	//check
{
	bool status;	//check with bool variale to see if char is vowel
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		status = true;	//return true if char is vowel
	else
		status = false;	//return false if char is not a vowel
	return status;		//assign status to the funcisVowel
}

personally i would have changed the loop exit criteria to

while( word != "q" && word != "Q" );

and then set the goodbye statement after the loop so that you are not conditionally testing for this each time through the loop, but there you go. happy to help.

If you like any of the responses then markem up and set thread solved.

thanks for the advice...however this is from what i have learned so far...in a do-while loop u cant put a statement after the final while loop???correct me if im wrong...
and for the while (word != "q" || word != "Q") if is set it to != i dnt know why but it s not terminating the program...even i tried while ((word != "q" || word != "Q")== 0) still only time i would actual terminate using q and Q is by while ((word == "q" || word == "Q") == 0); even though when i logically think about it its sayin when q is true do do the while loop but it does the opposite...if you can explain this to me that be great...i really want to know why its doing that...
thanks again

thanks for the advice...however this is from what i have learned so far...in a do-while loop u cant put a statement after the final while loop???correct me if im wrong...
and for the while (word != "q" || word != "Q") if is set it to != i dnt know why but it s not terminating the program...even i tried while ((word != "q" || word != "Q")== 0) still only time i would actual terminate using q and Q is by while ((word == "q" || word == "Q") == 0); even though when i logically think about it its sayin when q is true do do the while loop but it does the opposite...if you can explain this to me that be great...i really want to know why its doing that...
thanks again

Ok then lets start with the while...

while( word != "q" || word != "Q" );

you have two logic expressions in here a and b where a is

word != "q" ....
which equates true if the variable word does not equal "q" and false if it does equal "q"

and b is
word != "Q"
which equates to true if word != "Q" and false if it does equal "Q".

to exit your loop you need the whole logic expression (a || b) to be false. The way you have written this means that if a == false then b will always be true and vice versa. hence you never exit the loop! try to work through the logic now, doing some truth tables on paper, for the version I wrote.

now to the "goodbye" statement, what I mean here is that you can do something like...

do
{
...
} while( !exit );

cout << "goodbye"; // print goodbye here. this is perfectly valid.

By embedding a test in your loop testing for an exit condition and printing a message when one is found, as well as testing for exit each time round the loop you are duplicating functionality in your code.

thanks for clearing the cout statement for Goodbye!...works like magic..lol..hey i learned something new today...
however (a||b) make sense i understand both needs to be false
and i used the >>>while( word != "q" || word != "Q" );<<<
but i still cnt exit the loop:(...i wrote the loop on the paper which is if q is false run the loop again OR "Q" is false run the loop again...but somehow is still stuck in the loop...could that be because of string and char differences???

thanks for clearing the cout statement for Goodbye!...works like magic..lol..hey i learned something new today...
however (a||b) make sense i understand both needs to be false
and i used the >>>while( word != "q" || word != "Q" );<<<
but i still cnt exit the loop:(...i wrote the loop on the paper which is if q is false run the loop again OR "Q" is false run the loop again...but somehow is still stuck in the loop...could that be because of string and char differences???

You need to reread the preceeding posts in detail. The expression should be
while( word != "q" && word != "Q" );

you need to AND the terms not OR.

Which means that if either comparison is false then you will exit.

o o ok i didnt think of it this way..it makes sense now...thanks for making me aware of the && expression...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.