-
Why dont my else execute =/
This code will tel a use how many times a certain charicter occurs in text...even if the charicter is upper case/lower case...
previously I had the results couted within the loop which resulted in multiple returns of the result lol...so ive changed it but now i have a small problem i cant quite figure out...ay help is appreciated
Code:
cout <<"Insert a single charicter to produce its occurence count" <<endl;
cin >> f; //get charicter
f = tolower(f); //make sure input is lowecase//
char cng = toupper(f); //to account for uppercase,create an uppercase varibale//
do{
for (int i=0; i<s.length();i++)
if (f!=s[i]&&cng!=s[i])
found = false;
else if(f==s[i]||cng==s[i])
fc++;
found = true;
}
while (i<s.length());
if (found = true)
cout <<"The charicter " <<f<< " occures " <<fc<<" number of times"<<endl;
else if (found = false)
cout <<"Charicter not present"<<endl;
}
for instance if i enter.. " Hh "
& i ask the program to give me the results for "h" it will dsplay 2...all good there!
if i ask the program to give me the results for "z"
it will state "the charicter z occurs 0 number of times"
why is it not entering the else if at the end of my code :confused:
hmmmz is it because of the ; here.. 'of times"<<endl;'
Last edited by process; 11-19-2004 at 03:46 AM.
-
Your loop uses the same termination condition twice, and confuses the caseof letters. Instead of trying to fix it, let's think fo a simple algorithm for doing this job.
First, read the requested char from cin. convert it to uppercase.
Then use a loop that traverses the text. On each iteration, read the next character from the string, convert that char to uppercase and compare it to the sought-after uppercase character. if they match, increment the letter counter. The loop terminates when it has read the entire string.
Danny Kalev
-
Please don't do this. Found is already a boolean, so you don't have to compare it with true or false:
Code:
if (found = true) {
//do something
}
else if( found = false) {
//do something else
}
Instead do this:
Code:
if (found) {
//do something
}
else {
//do something else
}
Much more readable, isn't it?
-
Oh yeah, you are also not comparing there. That is why your else does not execute. You are assigning the value true to found in the if statement!
Another reason to not compare bool with true and false.
Here is the offending code:
Code:
if (found = true) // This is assignment!
-
if found == true is more readable really... being explicit is almost always more readable, at least to me. I also find stray brackets to be a mess..
some line of code { //not lined up
stuff;
} //this bracket is "stray", it does not line up with any other
I know publishers do this to save pages, but its ugly style.
-
explicite in some cases is goodness, but
if(bool == true)
is error prone, as we see in the original post (because '=' was used, not '==')
-
my bad with the mistake of the =/==assignment.....cant believe i did that!
When theres an error & things aint going right i think its a good idea to take a break & then return to study the code.....im sure if i did this & wasnt so inpatient i would of picked up on that.
so ill clean it up & hope for the best...if its still furked ill think up a new method
thanks a o for all ur help, much appreciated
-
you also want to turn up your warnings... you should have been warned about that mistake by the compiler, saving much grief.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks