我是c ++的新手,我在大学里有一个程序要求计算大小超过两个字符的单词以及用户输入的短语中所有字符的数量(忽略空格)。程序使用头文件中的函数getche()逐个字符地接受短语,直到按下回车键。与程序的交互可能如下所示:
输入文字:welcome to c ++ 字数是:2 字符数是: 我停止了这个步骤并发现忽略两个字的单词的问题,请帮助......
#include <iostream>
#include<conio.h>
using namespace std;
int main(){
int chcount=0;
int wdcount=1;
int spccount=0;
char ch;
cout<<"Please,Enter a phrase :"<<endl;
while((ch=getche())!='\r'){
if(ch!=' '){
chcount++;
}else if (ch==' '){
wdcount++;
}
}
cout<<"Count of words is : "<<wdcount<<endl;
cout<<"Count of Letters is : "<<chcount<<endl;
//cout<<"Count of space :"<<spccount<<endl;
return 0;
}
答案 0 :(得分:0)
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int chcount = 0, wdcount =0, count = 0;
char ch='a';
cout << "Enter your text : ";
while ( ch != '\r' )
{
ch = getche();
if ( ch !=' ' )
{
chcount++;
count++;
}
else if (count > 2)
{
wdcount++;
count=0;
}
}
cout<<"\nCount of words is: "<<wdcount+1<<"\nCount of charcters is: "<<chcount-1<<"\n";
system("pause");
return 0;}
顺便说一句,我是你的同事Karim Fouad :)希望这有帮助