简体   繁体   English

Arrays 在 C++ 中输出错误的数字

[英]Arrays Outputting the Wrong Numbers in C++

My goal is to have a player input a bunch of numbers for an array then that array is written into a text file.我的目标是让玩家为数组输入一堆数字,然后将该数组写入文本文件。 Another part of it is to be able to receive a bunch of numbers from a text file and put them into a sorted array of highest to lowest then output that array.它的另一部分是能够从文本文件中接收一堆数字并将它们放入从最高到最低的排序数组中,然后是 output 该数组。 But for some reason, I'm getting a lot of errors, ones that i feel like with some research I can fix.但是由于某种原因,我遇到了很多错误,我觉得通过一些我可以修复的研究。 Unfortunately, there is one very confusing situation where I test to make sure the unsorted array is correct by outputting each element of the array.不幸的是,有一种非常令人困惑的情况,我通过输出数组的每个元素来测试以确保未排序的数组是正确的。 This is not a part of the final program but a test for now.这不是最终计划的一部分,而是目前的测试。 I have a for loop that does so and it works perfectly, outputting each number as expected.我有一个 for 循环,它可以完美地工作,按预期输出每个数字。 Then in the next for loop, the exact same thing is supposed to happen but the numbers being outputted are all messed up.然后在下一个 for 循环中,应该发生完全相同的事情,但是输出的数字都搞砸了。 I do not understand how.我不明白怎么做。 Code below下面的代码

void readFile(string fName) {
    string fileName = fName + ".txt";
    ifstream myFile(fileName);
    char c;
    string num;
    int count = 0;

    // Bring the array from file to int array
    while (!myFile.eof()) {
        myFile.get(c);
        if (isspace(c) && num != "") {
            int n = stoi(num);
            intArray[count] = n;
            count++;
            num = "";
            continue;
        }
        if (!myFile.eof()) {
            num += c;
        }
    }

    for (int i = 0; i < 10; i++) {
        cout << intArray[i] << endl;
    }

    // Sort the array higest to lowest
    for (int i = 0; i < 10; i++) {
        cout << intArray[i] << "       ";
        for (int j = 9; j >= i; j--) {
            if (j == 0) {
                continue;
            }
            if (intArray[j] > intArray[j - 1]) {
                int temp = arr[j];
                intArray[j] = intArray[j - 1];
                intArray[j - 1] = temp;
            }
        }
        cout << endl;
    }
}

sorry about the formatting above, its being weird so imagine the code is within the function.对上面的格式感到抱歉,它很奇怪,所以想象一下代码在 function 内。

This is what this outputs:这就是它的输出:

1
2
3
4
5
6
7
8
99
234
1
1
1
1
1
1
1
1
1
1

The numbers before the series of 1's is the actual array, the 1's is what is apparently the array according the cout in the last section of code where it says cout << intArray[i] 1 系列之前的数字是实际数组,1 显然是根据代码最后一段中的 cout 的数组,其中 cout << intArray[i]

Your array does appear to be sorted.您的数组似乎已排序。 The reason for all the ones being printed is due to the location of the cout << within the outer loop.所有这些都被打印的原因是由于cout <<在外循环中的位置。

Consider what your array looks like after your first iteration through the inner loop:考虑一下您的数组在您第一次通过内部循环后的样子:

234,1,2,3,4,5,6,7,8,99

Now consider that you've incremented i to 1 in the outer loop.现在考虑您已在外循环中将i递增到 1。 When you index your array intArray[i] , the i th element is now 1 because you correctly moved it there.当您索引数组intArray[i]时,第i个元素现在是 1,因为您正确地将它移到了那里。 Each time, you're moving your smaller elements up one position in the array, then indexing to the position where the 1 is.每次,您都在数组中将较小的元素向上移动一个 position,然后索引到1所在的 position。

Don't try to print the sorted array while you're sorting it.排序时不要尝试打印已排序的数组。 Instead loop over it and print it after the sort.而是循环它并在排序后打印它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM