HOAX
HOAX

Reputation: 51

(c++) trying to read numbers into array, getting junk numbers on output

I've searched and searched this topic and still nothing so I'm resorting to this.

NOTE: Can't use vectors at all

So I'm trying to open up a text file in this program and read the numbers into an array. The program opens it up, and reads them (I'm assuming), but when I read the numbers the back, they are junk numbers. Not really sure where it went wrong, would appreciate some help. Here's my code:

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

using namespace std;

void readNumbers(int numbers[]);

const int MAX_SIZE = 12;

int main()
{
    int numbers[MAX_SIZE];

    readNumbers(numbers);

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

    return 0;
}

void readNumbers(int numbers[])
{
    int num = 0;

    ifstream inFile;

    inFile.open("numbers.txt");

    if (!inFile)
    {
        cout << "Cannot open the file" << endl;
    }
    else
    {
        inFile >> num;

        while(inFile)
        {
            int i = 0;
            numbers[i] += num;
            i++;
            inFile >> num;
        }
    }
    inFile.close();
}

Output:

1606416400
32767
0
0
0
0
0
0
0
0
0
0

Upvotes: 0

Views: 283

Answers (4)

Nandu
Nandu

Reputation: 808

fixed few errors in usage of array.

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


using namespace std;

void readNumbers(std::string* numbers);

const int MAX_SIZE = 12;
int nSize = 0;

int main()
{
    std::string numbers[MAX_SIZE];
    readNumbers(numbers);

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

    return 0;
}

void readNumbers(std::string* numbers)
{
    ifstream inFile;
    inFile.open("numbers.txt");

    if (!inFile)
    {
        cout << "Cannot open the file" << endl;
    }
    else
    {
        while(!inFile.eof())
        {
            char temp[100] = {0};
            inFile.getline(temp, 100);
            numbers[nSize++].assign(temp);
            if (nSize == MAX_SIZE) break;
        }
    }
    inFile.close();
}

Upvotes: 0

wrangler
wrangler

Reputation: 3576

Your array numbers[] is not initialized and you are incrementing nothing but garbage values numbers[i] += num;

hence it prints junk numbers.

If incrementing is important use:

int numbers[MAX_SIZE]={0}  //while declaration

If not:

numbers[i]=num   //inside while 

Also int i=0 should be outside while as i will always be 0 inside while.

Upvotes: 1

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

You're not initializing your numbers array, so it'll have garbage in it. Why are you using += when storing num in numbers[i]? Shouldn't you be using an assignment there?

numbers[i] = num;

(And also, as previously mentioned, the i index needs to be declared outside the loop, otherwise you're always adding/assigning to the first element in numbers.)

Upvotes: 0

user3147395
user3147395

Reputation: 541

The i variable is local to the loop. Try moving it outside:

    int i = 0;
    while(inFile)
    {
        numbers[i] += num;
        i++;
        inFile >> num;
    }

Upvotes: 2

Related Questions