【问题标题】:Not being able to enter input during runtime在运行时无法输入输入
【发布时间】:2020-05-25 07:29:24
【问题描述】:

当我用输入编译代码时,它工作正常。但是当我想用用户输入运行它时,它只是不接受输入。它不会给出任何错误。

// kefaa and first steps
// 2 2 1 3 4 1

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n, a[n], counter=0, maxIncr=0;
    cin >> n;
    cin.sync(); 
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i=0; i < n-1; ++i){
        if (a[i] <= a[i+1]){
            counter += 1;
            if(maxIncr<counter)
                maxIncr=counter;
        }else{
            counter=1;
        }
    }
    cout << maxIncr;
    return 0;
}

【问题讨论】:

  • int a[n] 不是标准 C++ 并且调用 未定义的行为,因为您还没有初始化 n
  • 最好不要使用cin.sync

标签: c++ dynamic-programming


【解决方案1】:

首先阅读this 了解您的包含以及为什么我们不包含这样的内容。

其次,如果你想让你的程序运行:

  int n; 
  cin >> n;
  int a[n], counter=0, maxIncr=0;

就像@UnholySheep 写给你的一样,你在 n 尚未被赋值时初始化 a[n]。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2022-01-20
    • 2011-08-30
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多