| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 235 人关注过本帖
标题:求解:第二组数无法输入
只看楼主 加入收藏
fxbszj
Rank: 5Rank: 5
来 自:阿修罗界
等 级:职业侠客
威 望:4
帖 子:163
专家分:331
注 册:2012-10-8
结帖率:92.86%
收藏
已结贴  问题点数:4 回复次数:3 
求解:第二组数无法输入
程序代码:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> ivec1,ivec2;
    int num1,num2;
    
    cout<<"Enter the first series of numbers:"<<endl;
    while(cin>>num1)
        ivec1.push_back(num1);
        
    cout<<"Enter the second series of numbers:"<<endl;
    while(cin>>num2)
        ivec2.push_back(num2);
        
    vector<int>::iterator it=ivec1.end()<ivec2.end()?ivec1.end():ivec2.end();
    for(vector<int>::iterator it1=ivec1.begin();it1!=it;++it1)
        for(vector<int>::iterator it2=ivec2.begin();it2!=it;++it2)
        {
            if(*it1!=*it2)
                {cout<<"false!"<<endl;
                return -1;
                }
            else{
                 cout<<"ture!"<<endl;
                 if(ivec1.end()<ivec2.end())
                     cout<<"The first numbers are parts of second numbers"<<endl;
                 else if(ivec1.end()==ivec2.end())
                      cout<<"The first numbers are equle to second numbers"<<endl;
                 else
                     cout<<"The second numbers are parts of first numbers"<<endl;
                     
                 return 0;
                 }
        }
} 

程序的要求是输入2组数,少的一组个数为n的话,就比教两组数的前n个是否相同,是就返回true

我按要求写了这么段代码,可是在输完第一组数字后,提示输入第二组数字后,就无法再输入任何内容,过了一段时间,程序会自动结束。实在是找不到是什么问题
2012-11-15 20:30
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:1 
cin的状态标志清除下
2012-11-15 20:47
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:1 
修改了下 供楼主参考

不要用.end()来比较

程序代码:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> ivec1,ivec2;
    int num1,num2;
    
    cout<<"Enter the first series of numbers:"<<endl;
    while(cin>>num1)
        ivec1.push_back(num1);
    
    cin.clear(); //cin的状态标志清除下
    cout<<"Enter the second series of numbers:"<<endl;
    while(cin>>num2)
        ivec2.push_back(num2);
    

 //   vector<int>::iterator it=ivec1.end()<ivec2.end() ?ivec1.end():ivec2.end();
    vector<int>::iterator it1=ivec1.begin();
    vector<int>::iterator it2=ivec2.begin();
    for( ; it2 != ivec2.end() && it1 != ivec1.end(); ++it1,++it2)
    {
        if(*it1 != *it2)
        {
            cout<<"false!"<<endl;
            return -1;
        }
        else
            continue;
    }

    cout<<"ture!"<<endl;
    if(ivec1.size()<ivec2.size())
        cout<<"The first numbers are parts of second numbers"<<endl;
    else if(ivec1.size()==ivec2.size())
        cout<<"The first numbers are equle to second numbers"<<endl;
    else
        cout<<"The second numbers are parts of first numbers"<<endl;
    
    

    return 0;
} 


    

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-11-16 10:46
liufashuai
Rank: 9Rank: 9Rank: 9
来 自:冥界-魔域-魂殿
等 级:蜘蛛侠
威 望:1
帖 子:370
专家分:1374
注 册:2012-6-22
收藏
得分:1 
顶楼上,你比较的只是指针,即地址,它们之和内存分配有关

有一种落差是,你配不上自己的野心,也辜负了所受的苦难。






2012-11-16 13:01
快速回复:求解:第二组数无法输入
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020775 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved