注册 登录
编程论坛 C++教室

怎么样从程序出发来管理输入流,使程序在读入约定个字符后不在读入

Naturelove 发布于 2008-10-19 22:06, 652 次点击
#include<iostream>
#define NULL 0

using namespace std;
struct data
{
    int num;//序号
    int x;//密码
    data *next;
};
int find(int,int);
data *creat(int);
int main()
{   

    int m=0,n=0;
    cout<<"The programme will end up with m=0!"<<endl<<endl;
    for(int count=0;;count++)
    {       cout<<"intput your data(m and n):"<<"  ";
             cin>>m;
        
        if(m<=0){cout<<"测试结束,测试组数:"<<count<<endl;break;}
            cin>>n;
               
        if(n<=0){cout<<"n<=0,error!"<<endl<<endl;continue;}
            find(m,n);
    }
    return 0;
}
data *creat(int i)
{
    data *tail=NULL;
    data *head ,*p1,*p2;head=p1=p2=NULL;
    cout<<"Input your passwords(>0):"<<"  ";
    for(int n=1;n<=i;n++)
    {  
        p2=new data;
        cin>>p2->x;
        p2->num=n;
        if(n==1)head=p2;
        else p1->next=p2;
        p1=p2;  
    }
    p1->next=head;
    tail=p1;
    return tail;
}
int find(int m,int n)
{  data *tem=NULL;
    tem=creat(n);//尾指针
    cout<<"The right order is:"<<"  ";
   do
   {
         if(m%n!=0)m=m%n;
        else m=n;
         for(int count=1;count<m;count++)
        {     
            tem=tem->next;
        }
        m=tem->next->x,cout<<tem->next->num<<' ';//取出密码,输出序号
        data *p=NULL;
        p=tem->next;
        tem->next=p->next;
        delete p;
   }while(--n);
    cout<<endl<<endl;
    return 0;
}
    
    我的这段代码,1 5 20 7↙ 它把最后一个20在第二次执行时付给了m,把7给了n。我不想让程序怎么做。请问大家怎么样在输入流上进行控制使满足要求
2 回复
#2
Naturelove2008-10-21 22:22
我自己顶!!
#3
zxwangyun2008-10-21 22:49
cin.width(n);//n为整数也可以实现程序在读入约定个(n-1个)字符后不在读入,不过只能用于字符串
1