C++新人求助!!
#include<iostream>#include<string.h>usingnamespacestd;classcounter{char*str;intwords;...
#include<iostream>
#include<string.h>
using namespace std;
class counter{
char* str;
int words;
public:
counter(){
words=0;
str=new char[100];
memset(str,0,100);
}
~counter(){
delete str;
}
void countWord(){
char *p=str;
for(;p!='\0';p++){
if(*p==' ') {
words++;}
}
}
void set(){
cin.getline(str,100);
}
void show(){
cout<<"Words="<<words<<endl;
}
};
int main(){
counter One;
One.set();
One.countWord();
One.show();
return 0;
}
输入一句话,判断有几个单词。想通过空格的个数来判断,最后能编译,但程序已停止工作。
已经发现了。 展开
#include<string.h>
using namespace std;
class counter{
char* str;
int words;
public:
counter(){
words=0;
str=new char[100];
memset(str,0,100);
}
~counter(){
delete str;
}
void countWord(){
char *p=str;
for(;p!='\0';p++){
if(*p==' ') {
words++;}
}
}
void set(){
cin.getline(str,100);
}
void show(){
cout<<"Words="<<words<<endl;
}
};
int main(){
counter One;
One.set();
One.countWord();
One.show();
return 0;
}
输入一句话,判断有几个单词。想通过空格的个数来判断,最后能编译,但程序已停止工作。
已经发现了。 展开
展开全部
#include<iostream>
#include<string.h>
using namespace std;
class counter {
char* str;
int words;
public:
counter() {
words=0;
str=new char[100];
memset(str,0,100);
}
~counter() {
delete str;
}
void countWord() {
char *p=str;
for(; *p; p++) {
if(*p==' ') {
words++;
}
}
}
void set() {
cin.getline(str,100);
}
void show() {
cout<<"Words="<<words+1<<endl;
}
};
int main() {
counter One;
One.set();
One.countWord();
One.show();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询