-2

I'm making code that read a text file the display the number then sort them from least to greatest I'm using C++ to code this. I'm running into two problems fist I'm getting two numbers that are not in the least a big negative number and 622 with is not in the list. I think this is because of a null retune to an array

#include<iostream>
#include<cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    const string INFILENAME = "100Numbers.txt"; //this find the name in the program foulder
    ifstream inFile;
    inFile.open(INFILENAME.c_str());//this open the file

    const int NUMBER = 100;
    int OneHundredNumber[NUMBER];
    int NewOneHundredNumbe[NUMBER];

    int Size = 0;
    int FindSize;
    int found = 0;

    if (inFile.is_open())
    {
        while (!inFile.eof()) { //in the File when it hits -1 i what it to not input -1 and stop it
            inFile >> OneHundredNumber[Size];//this is put the text in to the first arry 
            Size++;
        }
        printArray(NewOneHundredNumbe, Size, "");//note i this this is not need 
        loopfortheLoop(Size, NewOneHundredNumbe, OneHundredNumber, "The contents of the array are: ");


        sort(OneHundredNumber, OneHundredNumber + Size);
        loopfortheLoop(Size, NewOneHundredNumbe, OneHundredNumber, "The array has been sorted.");


        for (int i = 0; i < Size; i++)
        {
            if (OneHundredNumber[i] == 234) {
                found++;
            }
        }
        cout << endl << "The number ""234"", was found " << found << " times.";
        inFile.close(); // CLose input file
    }
    else { //Error message
        cerr << "Can't find input file " << INFILENAME << endl << "Pleace add it to the Progeran Files or cheack if the name of the text file is 100Numbers.txt";
    }
    return 0;
}

100Numbers.txt

137 354 328 263 120 36 124 138 175 365 180 170 179 144 45 379 97 31 357 274 142 314 3 327 321 137 122 331 308 366 352 71 363 14 85 266 287 80 91 370 117 374 57 204 224 148 268 280 292 363 399 388 325 234 230 331 89 390 212 55 297 622 151 266 272 63 331 21 250 141 154 15 61 234 23 143 99 389 196 29 361 180 288 47 62 50 347 180 234 334 130 230 184 214 177 154 129 391 234 -1
  • 2
    This is too much code, it is unlikely anyone will take the time to go through your whole program to find a problem. You should read about [MCVE] and provide a better code sample. – François Andrieux Nov 04 '21 at 22:58
  • One issue I notice: I think you should keep reading not until `inFile.eof()` is true but until the last read value is `-1`. – 500 - Internal Server Error Nov 04 '21 at 22:59
  • 1
    Since you are saying there is a bug in this code -- have you stepped through this code in a debugger? That is normally the next step to take. – Drew Dormann Nov 04 '21 at 23:00
  • 1
    Probably unrelated: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Nov 04 '21 at 23:00
  • To begin, your input loop is dangerous because it does not do any bounds-testing such as `Size < NUMBER`. Your `addElementToArray` function seems to be responsible for incrementing the array size, but that is passed _by value_ into the function, so the caller will never see that change. In general, there's way too much splitting of code into functions for operations that are trivial and common. It adds unnecessary complexity and room for mistakes. To top that off, there's way too much comments for my taste. That style of documentation might be acceptable if you're building an API. – paddy Nov 04 '21 at 23:01
  • Too much code here. Your code has a lot of sections that all need to work correctly, and work together correctly, for the exercise. Trying to find problems in the code as a whole is not a good idea. Instead, write small parts (in isolation) and test that each part is working. Then put a couple of tested parts of the code together, and test how they work together. Fix any problems (if necessary, when doing that, go back to testing of individual parts). Repeat until you get all the parts working together. – Peter Nov 04 '21 at 23:07
  • When `i` reaches `100` you get a stack buffer overflow at `if(oldNumber[i] != -1)` – Ted Lyngmo Nov 04 '21 at 23:09
  • In `addElementToArray`, what happens when you add to a full array? You should not pass an array, but a reference to a pointer of the start of the array. This will allow you to expand the array as necessary; **just like `std::vector` does**. – Thomas Matthews Nov 04 '21 at 23:11
  • is there a why to stop the while loop when it gets to -1 that all i need – Bryce Robinson Nov 04 '21 at 23:26

1 Answers1

0

This loop is the biggest problem:

while(!inFile.eof()) {
    inFile >> OneHundredNumber[Size]; // this is put the text in to the first arry
    Size++;
}
  • It doesn't check if Size is less than NUMBER
  • You check if end of file has been hit before extracting a value - and you don't check if a value was actually extracted but increase Size anyway.
  • The result is that you write out of bounds, making your program have undefined behavior.

This can be fixed like below. There is no eof() check needed:

while(Size < NUMBER && inFile >> OneHundredNumber[Size]) {
    Size++;
}

There is also something fishy about this:

void loopfortheLoop(int NewNumbersSize, int NewNumber[], int oldNumber[],
                    string say) {
    for(int i = 0; i < NewNumbersSize; i++) {
        if(oldNumber[i] != -1) {
            addElementToArray(NewNumber, i, oldNumber[i]);
        }
    }
    printArray(NewNumber, NewNumbersSize, say);
}

This leaves the elements in NewNumber[0] and NewNumber[99] uninitialized - but you read those in printArray which makes your program have undefined behavior.

Demo with debug print out of the uninitialized elements

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108