分析如下程序意图及错误之处

来源:百度知道 编辑:UC知道 时间:2024/05/25 23:28:08
bool found=false;
string str=“micky”;
for(int i=0; found; i++){
if(str[i]==‘y’)
found=true;
}
if (found)
cout<<“I have found the character y”;
else
cout<<“The character y is absent”;
循环条件为found是啥意思??

如果字符串中有y,输出 i have found the character y
否则输出The character y is absent

for的判断条件
for(int i=0; i<len(str); i++){

如果改!found,会是死循环或者出错吧

程序有点问题吧 for(int i=0; found; i++){ 应该是 for(int i=0; !found; i++){
要不逻辑有问题 就是找找字符串里面有没有y这个字符
我试了 没问题 中间那个是成立才执行 所以 应该是found为真时才执行 就是这个意思

用found标记是否找到了字母y刚开始肯定是假定找不到所以found=false
一旦找到y则令found=true并且退出循环,并在循环外打印找到y