d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Reading Numbers In File Into A 2d Array
Prev12
Add Reply New Topic New Poll
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:28pm
Quote (KrzaQ2 @ May 31 2014 07:26pm)
I already answered your question. You read the arraysize to tempfile (a double!) and do not sanitize the input. Then you proceed to access elements of the temp array with indexes between 0 and that unknown value you just read, when the only legit index values are 0, 1, 2, 3, 4 and 5. Accessing array out of bounds is UB, plain and simple.


hmm aright
let me try and make these changes and see what happens
thanks
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:41pm
okay so i made a few changes, and it does print and output something BUT they are not the correct numbers.
Not sure how this could happen.

MY OUPUT:
46 50 10 55 50 46
49 10 55 50 46 53
10 55 52 46 49 10
55 46 50 10 54 57
46 52 10 55 49 46

and i should be getting something very different.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <stdio.h>
#include <fstream>
using namespace std;

const int COLS = 6; //global parameter
void copyFile(int [][COLS], int);
void showArray(int [][COLS], int);

int main()
{
const int ROWS = 6;

int temp[ROWS][COLS];
copyFile(temp, ROWS);
showArray(temp, ROWS);

}

void copyFile(int temp[][COLS], int ROWS)
{
int tempfile;
ifstream infile;
infile.get();

infile.open("temperature.txt");
infile >> tempfile;

for(int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
temp[row][col] = infile.get();
}
}
infile.close();
}

void showArray(int temp[][COLS], int rows)
{
for(int x = 0; x < rows; x++)
{
for(int y = 0; y < COLS; y++)
{
cout << fixed << showpoint << setprecision(2);
cout << setw(4) << temp[x][y] << " ";
}
cout << endl;
}
}
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 31 2014 09:42pm
You need to show us the contents of "temperature.txt"
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:43pm
Quote (KrzaQ2 @ May 31 2014 07:42pm)
You need to show us the contents of "temperature.txt"


i did before but here it is again

OUTPUT SHOULD BE:
68.2 69.4 68.9 68.6 68.1 68.3
72.1 71.1 70.5 69.9 69.3 68.8
72.5 71.9 70.9 70.4 69.8 69.6
74.1 73.1 71.5 70.8 70.2 70.5
74.4 73.6 72.8 71.5 70.9 70.9
74.2 73.7 73.0 72.2 71.2 70.9
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 31 2014 09:46pm
Well, you said it's the expected output, not input.

In that case, you're using array of integers to hold real values, this obviously cannot work. Besides, your first read goes to fill tempfile.
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:48pm
Quote (KrzaQ2 @ May 31 2014 07:46pm)
Well, you said it's the expected output, not input.

In that case, you're using array of integers to hold real values, this obviously cannot work. Besides, your first read goes to fill tempfile.


oh sorry yea i ment input.
okay, let me see if i can make some more changes
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 31 2014 09:48pm
Quote (mrxskyline12 @ May 31 2014 11:43pm)
i did before but here it is again

OUTPUT SHOULD BE:
68.2 69.4 68.9 68.6 68.1 68.3
72.1 71.1 70.5 69.9 69.3 68.8
72.5 71.9 70.9 70.4 69.8 69.6
74.1 73.1 71.5 70.8 70.2 70.5
74.4 73.6 72.8 71.5 70.9 70.9
74.2 73.7 73.0 72.2 71.2 70.9


instead of labeling it as OUTPUT, you should have labelled it as input
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:50pm
Quote (carteblanche @ May 31 2014 07:48pm)
instead of labeling it as OUTPUT, you should have labelled it as input


lol yes i know, my mistake
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll