为什么按回车输入还不停止
```c++
#include
#include
using namespace std;
class Set
{
public:
Set(){
this->length=0;
memset(this->Setnum,0,sizeof(Setnum));
cout<<"默认构造函数"<Set(int *n){
for(int i=0;i<_msize(n)/sizeof(*n);i++)
this->addnum(n[i]);
cout<<"普通构造函数"<Set(Set &s){
length=s.length;
for(int i=0;i"拷贝构造函数"<Set(){
cout<<"析构函数"<Set jiaoji(Set &s2);
Set bingji(Set &s2);
Set chaji(Set &s2);
bool equals1(Set &s2);
bool ifinclude(Set &s2);
bool ifempty();
int numsize();
void beempty();
private:
int Setnum[100];
int length=0;
};
void Set::addnum(int num){
int flag=0;
for(int i=0;iif(Setnum[i]==num){
flag=1;
break;
}
}
if(flag==0){
Setnum[length]=num;
length++;
}
}
void Set::delnum(int num){
int n=length;
while(n--){
if(Setnum[n]==num)
break;
}
for(int i=n;ifor(int i=0;i" ";
}
cout<Set Set::jiaoji(Set &s2){
Set s;
for(int i=0;ilength;i++){
s.Setnum[i]=this->Setnum[i];
s.length=this->length;
}
for(int i=0;iSet Set::bingji(Set &s2){
Set s;
for(int i=0;ilength;i++){
for(int j=0;jif(this->Setnum[i]==s2.Setnum[j]){
s.Setnum[i]==s2.Setnum[j];
s.length++;
}
}
}
}
Set Set::chaji(Set &s2){
int flag=0;
Set s,s3;
s3=this->bingji(s2);
for(int i=0;ilength;i++){
flag=0;
for(int j=0;jif(this->Setnum[i]==s3.Setnum[j]){
flag=1;
break;
}
}
if(flag==0){
s.Setnum[s.length]=this->Setnum[i];
s.length++;
}
}
return s;
}
bool Set::equals1(Set &s2){
if(this->length!=s2.length)
return false;
for(int i=0;ilength;i++){
if(this->Setnum[i]!=s2.Setnum[i])
return false;
}
return true;
}
bool Set::ifinclude(Set &s2){
Set *s3=new Set();
*s3=this->bingji(s2);
if(s3->length==this->length||s3->length==s2.length)
return true;
else
return false;
}
bool Set::ifempty(){
if(this->length==0)
return true;
else
return false;
}
int Set::numsize(){
return this->length;
}
void Set::beempty(){
memset(Setnum,0,sizeof(Setnum));
this->length=0;
}
int main()
{
Set s1;
char *ch='ch';
int num;
cout<<"Enter multiple numbers"<while(ch!='endl'){
cin>>num;
s1.addnum(num);
cin>>ch;
}
s1.shownum();
}
```
参考GPT和自己的思路:根据你贴出的代码,我看到你在输入数字时使用了一个 while 循环,判断的条件是 ch 不等于 "endl",但是 ch 先被赋值为了一个 char 指针 "ch",并没有进行正确的赋值。正确的做法应该是将 ch 声明为字符类型,然后在循环中使用 getchar() 函数进行输入,直到输入的字符为回车键时跳出循环即可。修改后的代码如下:
int main()
{
Set s1;
char ch;
int num;
cout<<"Enter multiple numbers"<<endl;
while((ch=getchar())!='\n'){
cin>>num;
s1.addnum(num);
}
s1.shownum();
}
这样输入数字后按回车即可停止输入。
你这都写的啥啊。
int main()
{
Set s1;
char ch;
int num;
cout<<"Enter multiple numbers"<<endl;
while(ch!='\n'){
cin>>num;
s1.addnum(num);
cin>>ch;
}
s1.shownum();
}
不知道你这个问题是否已经解决, 如果还没有解决的话:特点