标题:大小写转换--为什么在 while循环中 第二个条件 else if 不执行?
取消只看楼主
xinyu_ufo
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-6-11
结帖率:0
已结贴  问题点数:10 回复次数:1 
大小写转换--为什么在 while循环中 第二个条件 else if 不执行?
//ch6-1 keybrod input code_1
#include<iostream>
#include<cctype>
#include<string>
int main()
{
    using namespace std;
    cout<<"Enter the text for transform: ";
    char ch;
    cin.get(ch);
    while(ch !='@')
    {
        if(ch=tolower(ch))
            cout<<ch;
        else if(ch=toupper(ch))  //----为什么不执行呢?
            cout<<ch;
        cin.get(ch);
    }
    //cout<<a<<" and "<<b<<" and "<<c;
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: include 
2016-06-11 12:18
xinyu_ufo
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-6-11
得分:0 
回复 2楼 yangfrancis
非常感谢您指出的错误。我改了一下我的程序,请斧正。
//ch6-1 keybrod input code_1
#include<iostream>
#include<cctype>
void up_low_later();
char ch,T,t;
using namespace std;
int main()
{
    cout<<"Enter the text for transform: ";
    up_low_later();
    while (isdigit(ch))
    {
        cout<<"Please enter the leter: ";
        cin.sync();
        up_low_later();
    }
    system("pause");
    return 0;
}        
void up_low_later()
{
    cin.get(ch);
    while(ch !='@')                     //遇到@ 退出
    {
        if (isupper(ch))                //先判定输入的是大写字母
        {
            T=tolower(ch);              //将大写转换为小写
            cout<<T;
        }
        else if(islower(ch))            //先判定输入的是小写字母
        {
            t=toupper(ch);
            cout<<t;
        }
        else if(ch ==' ')               //必须为“双等号”
            cout<<ch;
        else if(isdigit(ch))            //判断字符是否为0~9
        {
            break;
        }
        cin.get(ch);
    }
};
/*while(ch !='@')
    {
        if(tolower(ch))      //tolower(ch)检测输入如果是大写将转换为小写,如果不是将会原样输出。
                              //所以如果ch为小写,if(tolower(ch))将返回小写,这就意味着else if(toupper(ch))是多余的!
        {
            ch=tolower(ch);
            cout<<ch;
        }
        else if(toupper(ch)) //--所以不会被执行。
        {
            ch=toupper(ch);
            cout<<ch;
        }
        cin.get(ch);
    }*/
2016-06-11 22:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465834-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.101810 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved