不停歇的旅者 2021-10-30 17:26 采纳率: 0%
浏览 38

c++中for循环之后cin输入错误


#include<iostream>
using namespace std;
struct tree{
    //string date;
    int weight, parent, lchild, rchild;
};

void creat(struct tree *t, int n);
//void trave(struct tree *t);

int main(){
    int n, choice;
    n = 8;
    int m = 2*n;
    tree *t = new tree[m];
    //cout<<"输入叶子个数"<<endl;
    //cin<<n;
    creat(t, n);
    //cout<<t[m-1].weight<<endl;
    /*while(1){
        cout<<"请选择"<<endl;
        cin>>choice;
        switch(choice){
            case 1:
                creat(t, n);
                break;
            case 2:
                cout<<t[2*n-1].weight<<endl;
                break;    
        }
    }*/
}

void creat(struct tree *t, int n){
    int i, o, p;
    int m = 2*n;
    for(i=1;i<=m;i++){
        t[i].lchild = 0;
        t[i].rchild = 0;
        t[i].parent = 0;
    }//把这个for循环删掉之后就正常了
    cout<<"输入权重"<<endl;
    for(i=1;i<=n;i++){
        //cin.clear();    
        cin>>p;//错误处
        cout<<p<<endl;
        t[i].weight = p;
    }
    int m1= 1, m2= 1;
    for(o=n+1;o<=m-1;o++){
        for(i=1;i<o;i++){
            if(t[i].weight < t[m1].weight && t[i].parent == 0){
                m1 = i;
            }
        }
        for(i=1;i<=o-1;i++){
            if(t[i].weight < t[m1].weight && i != m1 && t[i].parent == 0){
                m2 = i;
            }
        }
        t[o].weight = t[m1].weight + t[m2].weight;
        t[o].lchild = m1;
        t[o].rchild = m2;
        t[o].parent = 0;
        t[m1].parent = o;
        t[m2].parent = o;
        m1 = o;
        m2 = o;
    }
}

当用cin输入后再输出p,输出的不是之前输入的数,并且会跳过之后的cin

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-30 17:39
    关注

    测试了一下,没出现你说的现象啊

    评论

报告相同问题?

问题事件

  • 创建了问题 10月30日

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题