C++如何判断输入字符串是否为字母

qq_33341459 2016-03-24 10:14:16
#include <iostream>
#include <string>
#define SIZE 3
using namespace std;
int main()
{
string name[SIZE], temp ;
cout << "输入三个名字\n" ;
for(int i = 0; i != SIZE; ++i)
{
cout << "输入第" << i+1 << "个名字:" ;
getline(cin,name[i]);
cin.ignore(80,'\n');
if(islower(name))
{
break;
}
else
{
cout << "ee" ;
}
}
for(i=0; i<SIZE; i++)
{
for(int j=SIZE-1; j>i; j--)
{
if(name[j] < name[j-1])
{
temp = name[j];
name[j] = name[j-1];
name[j-1] = temp;
}
}
}
for(i = 0; i != SIZE; ++i)
{
cout << name[i] << ' ' ;
}
return 0;
}
这是输入姓名按照字母顺序输出的,想在中间加个判断输入字符串是否为字母,不是字母提示错误重新输入的
...全文
2335 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝猫淘气 2016-03-31
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
if (!(
         ('a'<=name[i][j] && name[i][j]<='z')
      || ('A'<=name[i][j] && name[i][j]<='Z')
      || (' '==name[i][j]                   )
     )
   )
paschen 版主 2016-03-24
  • 打赏
  • 举报
回复
引用 3 楼 qq_33341459 的回复:
为什么不能用islower() 进行判断? 提示error C2664: 'islower' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [3]' to 'int'
islower需要的参数不是string
qq_33341459 2016-03-24
  • 打赏
  • 举报
回复
为什么不能用islower() 进行判断? 提示error C2664: 'islower' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [3]' to 'int'
赵4老师 2016-03-24
  • 打赏
  • 举报
回复
if((name[i][j] < 'a' || name[i][j] > 'z') && (name[i][j] < 'A' || name[i][j] > 'Z')  )
//改为
if (!(
         ('a'<=name[i][j] && name[i][j]<='z')
      || ('A'<=name[i][j] && name[i][j]<='Z')
     )
   )
sdghchj 2016-03-24
  • 打赏
  • 举报
回复
getline(cin,name[i]);
bool bValid = true;
for(size_t j = 0; j < name[i].size(); ++ j)
{
      if((name[i][j] < 'a' || name[i][j] > 'z') && (name[i][j] < 'A' || name[i][j] > 'Z')  )
      {
              bValid = false;
              break;
      }
}

if(!bValid)
{
       -- i;
       continue;
}
赵4老师 2016-03-24
  • 打赏
  • 举报
回复
if (!(
         ('a'<=name[i][j] && name[i][j]<='z')
      || ('A'<=name[i][j] && name[i][j]<='Z')
      || (' '==name[i][j]                   )
     )
   )
qq_33341459 2016-03-24
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
if((name[i][j] < 'a' || name[i][j] > 'z') && (name[i][j] < 'A' || name[i][j] > 'Z')  )
//改为
if (!(
         ('a'<=name[i][j] && name[i][j]<='z')
      || ('A'<=name[i][j] && name[i][j]<='Z')
     )
   )
英文名字以空格分开,如何不排除空格
paschen 版主 2016-03-24
  • 打赏
  • 举报
回复
string类型并不是用\0来结尾
qq_33341459 2016-03-24
  • 打赏
  • 举报
回复
引用 4 楼 paschen 的回复:
[quote=引用 3 楼 qq_33341459 的回复:] 为什么不能用islower() 进行判断? 提示error C2664: 'islower' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [3]' to 'int'
islower需要的参数不是string [/quote] #include <iostream> #include <string> using namespace std; int main() { string sen ; int space = 0 ; cout << "输入一条句子,按回车结束\n" ; getline(cin,sen); for(int i = 0; i != sen.length(); ++i) { if(isspace(sen[i])) //遇到空格space++ { space++ ; cout << ' '; while(!isalpha(sen[i])) //直到遇到下一个字母 { i++; if(sen[i] == '\0' || isspace(sen[i])) { space-- ; break; } } --i; } else { cout << sen[i] ; } } cout << "\n单词个数为:" << space+1 << "个" << "\n单词总长度为:" << sen.length() << "\n平均长度为:" << (double)(sen.length()) / (double)space << endl; return 0; } 请问应该怎么算出句子的平均长度,看下代码

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧