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

菜鸟问题!cin未被执行!

sudong0913 发布于 2010-08-30 16:15, 1400 次点击
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void main()
{
    cout<<"请输入数据!"<<endl;
    double x;
    vector<double> data;
    while (cin>>x)
    {
        data.push_back(x);
    }
    int m=data.size(),n=0;
    if (m==0)
    {
        throw domain_error("数据不能为空!");
    }
    else
    {
        double number;
        cout<<"请输入你要找的数!"<<endl;
        cin>>number;                         //此处未被执行!!
        for (int i=0;i<m ;i++)
        {
            if (number==data[i])
            {
                n++;
            }
        }
        if (n==0)
        {
            cout<<"没有你要找的数"<<endl;
        }
        else
        {
        cout<<number<<"有"<<n<<"个"<<endl;
        }
    }
}
请高手指教!!!有bug待完善!!
22 回复
#2
ToBeOOP2010-08-30 18:18
好邪恶的问题啊...
#3
toadswan2010-08-30 18:20
就把while (cin>>x)改成while (cin>>x,x)试试,好像行的……
#4
toadswan2010-08-30 18:22
这样数据最后输入0就好了……
#5
sudong09132010-08-30 18:42
回复 3楼 toadswan
不行!!
崩溃了!

[ 本帖最后由 sudong0913 于 2010-8-30 18:43 编辑 ]
#6
ToBeOOP2010-08-30 18:42
程序代码:
#include<vector>
#include<algorithm>
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::domain_error;

int main( )
{
    cout<<"请输入数据!(Ctrl + Z to )"<< endl;
    double x = 0;
    vector<double> data;
    while (cin >> x)
    {
        data.push_back(x);
    }
    cin.clear( );
    int n = 0;
    if (data.size( ) == 0 )
    {
        throw domain_error("数据不能为空!");
    }
    else
    {
        double number = 0.0;
        cout <<"请输入你要找的数!" << endl;
        cin >> number;
        for (int i=0 ; i != data.size( ) ; ++i)
        {
            if (number == data[i])
            {
                n++;
            }
        }
        if (n==0)
        {
            cout<<"没有你要找的数"<<endl;
        }
        else
        {
        cout<< number<< "" << n << "" << endl;
        }
    }
    system ("Pause");
    return 0;
}
试下...受到LS的启发...学习了...
#7
toadswan2010-08-30 18:45
回复 5楼 sudong0913
哦我水平太低了,不过我电脑上运行的挺好的……
#8
sudong09132010-08-30 18:46
回复 6楼 ToBeOOP
依然被跳过!!!
#9
sudong09132010-08-30 18:49
回复 7楼 toadswan
我用的是EditPlus~~你的是?
#10
toadswan2010-08-30 19:14
回复 9楼 sudong0913
那个没用过,我用的是vc6
#11
mjsxjy2010-08-30 21:15
楼主啊。你标错了,未被执行的不是那一句。你这程序问题出在上面,那个while(cin>>x)竟然没有跳出循环。不过我不怎么会C++哈,特别是你用的模版我是一窍不通。但我也大概明白你程序的意思。
你看一下我给你改的
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void main()
{
    double x;
    vector<double> data;
    cout<<"请输入数据!"<<endl;
    while (cin>>x)
    {
        data.push_back(x);
        if (x==0)
        {
            cout<<"输入完毕!"<<endl;
            break;
   
        };
    };
int m=data.size(),n=0;
    if (m==0)
    {
        throw domain_error("数据不能为空!");
    }
    else
    {
        double number;
        cout<<"请输入你要找的数!"<<endl;
        cin>>number;                         //此处未被执行!!
        for (int i=0;i<m ;i++)
        {
            if (number==data[i])
            {
                n++;
            }
        }
        if (n==0)
        {
            cout<<"没有你要找的数"<<endl;
        }
        else
        {
        cout<<number<<"有"<<n<<"个"<<endl;
        };
    };
}

这样是可以了吧。改动了一点,跳出while的条件。

[ 本帖最后由 mjsxjy 于 2010-8-30 21:28 编辑 ]
#12
mjsxjy2010-08-30 21:20
你调试我上面的代码看看嘛,可以找到需要找的数。
我用if x==0来跳出循环。这里随便你自己想用什么方法来跳出,我只是用最直接最方便的来跳一下。别的地方如果我改的还不合你意,你自己再改改吧。


[ 本帖最后由 mjsxjy 于 2010-8-30 21:31 编辑 ]
#13
ToBeOOP2010-08-30 22:48
弄错了...刚刚是受了toadswan的启发...
#14
ToBeOOP2010-08-30 22:52
我的代码可以啊...你需要输入Ctrl + Z来跳出循环,然后才不会跳过.下面那个cin
#15
pangding2010-08-31 18:34
楼主试 6 楼的代码依然不行吗?我也觉得加个 cin.clear( ); 就应该可以了。
#16
sudong09132010-09-01 09:04
回复 16楼 pangding
只有本站会员才能查看附件,请 登录
要输入两次ctrl+z才能被执行~~~
当输入字符时依然被跳过~~
求教~~~
#17
sudong09132010-09-01 15:21
已经解决~~
cin.clear();只是清楚了流的错误标记~~~没有清空输入缓冲区~~
在后面加个cin.sync();清空输入缓冲区的所有数据就完美解决了~~
//////////////////////////////////////////////////////////////////////
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void main()
{
    cout<<"请输入数据!"<<endl;
    double x;
    vector<double> data;
    while (cin>>x)
    {
        data.push_back(x);
    }
///////////////////////////////
     cin.clear();
     cin.sync();
//////////////////////////////
    int m=data.size(),n=0;
    if (m==0)
    {
        throw domain_error("数据不能为空!");
    }
    else
    {
        double number;
        cout<<"请输入你要找的数!"<<endl;
        cin>>number;
        for (int i=0;i<m ;i++)
        {
            if (number==data[i])
            {
                n++;
            }
        }
        if (n==0)
        {
            cout<<"没有你要找的数"<<endl;
        }
        else
        {
        cout<<number<<"有"<<n<<"个"<<endl;
        }
    }
}

#18
myth_feng2010-09-28 17:17
void main()
{
    cout<<"请输入数据!"<<endl;
    double x;
    vector<double> data;
    while (cin>>x)
    {   if (x<10.0) {
           data.push_back(x);
        }
        else break;                      //这边加条控制跳出语句就可以了。。
    }
    int m=data.size(),n=0;
    if (m==0)
    {
        throw domain_error("数据不能为空!");
    }
    else
    {
        double number=0.0;
        cout<<"请输入你要找的数!"<<endl;
        std::cin>>number;                         //此处未被执行!!
        for (int i=0;i<m ;i++)
        {
            if (number==data[i])
            {
                n++;
            }
        }
        if (n==0)
        {
            cout<<"没有你要找的数"<<endl;
        }
        else
        {
        cout<<number<<"有"<<n<<"个"<<endl;
        }
    }
}
#19
vfdff2010-09-29 00:04
回复 楼主 sudong0913
while (cin>>x) 这个写法不是很好,因为cin返回值不是bool类型的
#20
pangding2010-10-10 01:41
回复 19楼 vfdff
有个自动强转函数 operator void *() const 专门转这个用的。像:
while (cin >> x)
或者
if (cin >> x)
之类的,还是一种标准写法呢~~
#21
2010-10-11 21:57
while (cin>>x)
    {
        data.push_back(x);
    }


你这样写代码有问题   这样写的话  退出循环唯一的方法  就是把流标志弄成bad 或 fail

既然标志出问题  结束循环了    那么你下边的cin>>number; //此处未被执行!

就会一直出现问题  所以你要第一个while退出循环之后  就把标志位还有缓冲区 都重新清除一下  方法如下
 while (cin>>x)
    {
        data.push_back(x);
    }
cin.Clear();                     清除标志位    参数默认为0   
cin.ignore(0x7fffffff, '\n');     忽略缓冲区里的内容  第一个参数是整数最大值   第个参数提前结束标志  
#22
vfdff2010-10-11 22:29
受教!
#23
2010-10-11 22:59
回复 15楼 pangding
清除完标志位有时是可以的  但我感觉最好还是 用cin.ignore()  把缓冲区里的内容忽略一下
1