【问题标题】:Compilator won't stop printing the same, wrong answer编译器不会停止打印相同的错误答案
【发布时间】:2015-11-30 08:49:43
【问题描述】:

所以我应该从文档中做一个单行矩阵,但编译器一直打印错误的答案。更不用说用另一个元素替换一个元素时,程序不会对命令做出反应。

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

const char FV[] = "Masyvas.txt";
using namespace std;
int main()
{
    int A[10];
    int i,j, m, n;
    ifstream Mas(FV);
    cout << "Parasykite masyvo elemento indekso numeri, pries kuri iterpsite nauja elementa - ";
    cin >> n;
    cout << "irasykite iterpiamo elemento reiksme - ";
    cin >> m;
    for (i = 0; i < 5; i++)
    {
        Mas >> A[i];
    }
    for ( j = n; j < 5; j++)
    {
        A[j + 1] = A[j];
        A[n] = m;
    }
    for (i = 0; i < 5; i)
    {
        cout << " " << A[i] << endl;
    }
    Mas.close();
    return 0;
}

【问题讨论】:

  • "copilator", "compilator" – 如果你连正在使用的工具的名字都拼不出来,我应该假设你在研究这个问题上付出了多少努力?跨度>
  • 你给程序什么输入?该输入的实际输出是什么?什么是预期的输出?你读的文件内容是什么?

标签: c++ infinite-loop


【解决方案1】:

问题出在这一行:

for (i = 0; i < 5; i)

这里有无限循环,因为条件i &lt; 5 始终为真。

很容易在您身边找到它!下次使用任何调试器来分析这么简单的问题。

所以,只需用这一行替换该行即可修复它:

for (i = 0; i < 5; i++)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多