toupper返回值类型问题
程序如下:转换输入的一组单词为大写。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void main()
{
string word;
vector <string> svec;
cout < < "请输入各个单词: " < <endl;
while(cin> > word)
svec.push_back(word);
cout < < "原语句: " < <endl;
for(vector <string> ::size_type i=0;i!=svec.size();i++)
cout < <svec[i] < < " ";
cout < <endl;
cout < < "转换为大写: " < <endl;
for(vector <string> ::size_type j=0;j!=svec.size();j++)
{
for(string::size_type k=0;k <svec[j].size();k++)
cout < <toupper(svec[j][k]);
cout < < " ";
if((j+1)%7==0)
cout < <endl;
}
}
cout < <toupper(svec[j][k]);直接输出了数字,但是换成svec[j][k]=toupper(svec[j][k]);后再输出,就是大写字母了,迷惑中。
[解决办法]
toupper()函数原形是
int toupper(int ch);
返回是整型
你应该改成
cout < <static_cast <char> (toupper(svec[j][k]));