string 和字符数组???

name61 2008-03-26 09:45:02
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
cout<<"enter words (q to quit):\n";
string words; //我试过用char数组
int vowels = 0;
int other = 0;
int consonant = 0;
char ch;
cin>>words; //但为什么用char数组时循环不能判断为"q"的输入
while (words!="q")
{
switch (words[0])
{
case 'a':vowels++;break;
case 'e':vowels++;break;
case 'i':vowels++;break;
case 'o':vowels++;break;
case 'u':vowels++;break;
}
if (isalpha(words[0]))
{
if (words[0]!= 'a'&&words[0]!= 'e'&&words[0]!= 'i'&&words[0]!= 'o'&&words[0]!= 'u')
consonant++;
}
else
other++;

cin>>words;
}
cout<<endl<<vowels<<" words beginning with vowels\n";
cout<<consonant<<" words beginning with consonant\n";
cout<<other<<" other.\n";
return 0;
}
...全文
77 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
myemailsz 2008-03-26
  • 打赏
  • 举报
回复
用strcmp()
name61 2008-03-26
  • 打赏
  • 举报
回复
哈哈
多谢指教
effective_person 2008-03-26
  • 打赏
  • 举报
回复

while (words!=string("q"))
呵呵看错了
ttkk_2007 2008-03-26
  • 打赏
  • 举报
回复
cin>>words; //但为什么用char数组时循环不能判断为"q"的输入
while (words!="q")
=============================================
用char数组比较的是地址,要比也是strcmp,但是string可以直接比
effective_person 2008-03-26
  • 打赏
  • 举报
回复

char ch;
cin>>words; //但为什么用char数组时循环不能判断为"q"的输入
while (words!="q")///? 'q'

64,849

社区成员

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

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