注册 登录
编程论坛 C++教室

大小写转换--为什么在 while循环中 第二个条件 else if 不执行?

xinyu_ufo 发布于 2016-06-11 12:18, 3459 次点击
//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;
}
2 回复
#2
yangfrancis2016-06-11 16:09
if(ch==tolower(ch))
#3
xinyu_ufo2016-06-11 22:45
回复 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);
    }*/
1