d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Reading Numbers In File Into A 2d Array
12Next
Add Reply New Topic New Poll
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 07:13pm
having trouble with this.
here is my code:
temperature.txt is the file that i am reading the numbers from
when i run this, the program just crashes, so im not even sure whats wrong
the compiler that i use is CodeBlocks

#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(double [][COLS], int);
void showArray(double [][COLS], int);

int main()
{
const int ROWS = 6;

double temp[ROWS][COLS];
copyFile(temp, COLS);
showArray(temp, COLS);
}

void copyFile(double temp[][COLS], int ROWS)
{
double tempfilerow;
double tempfilecol;
double count = 0;
ifstream infile;
infile.get();

infile.open("temperature.txt");
infile >> tempfilerow;
infile >> tempfilecol;

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

void showArray(double temp[][COLS], int row)
{
for(int x = 0; x < row; x++)
{
for(int y = 0; y < COLS; y++)
{
cout << setw(4) << temp[x][y] << " ";
}
cout << endl;
}
}

This post was edited by mrxskyline12 on May 31 2014 07:13pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 31 2014 07:39pm
This program is so bad my eyes teared up by themselves.

Do not use C-style arrays unless you're interfacing plain C code. It's as simple as that.

If you do though:
1. use proper constants for rows and columns.
Code
void copyFile(double temp[][COLS], int ROWS)

You expect the number of rows as the second parameter, yet you pass the number of columns
Code
copyFile(temp, COLS);


2. Sanitize your input data.
Code
infile >> tempfilerow;
infile >> tempfilecol;
This is most likely the cause of the crash. If you're compiling in pre-C++11 mode, you're not even checking if your code exhibits undefined behaviour when the stream doesn't parse correctly. But more likely, you're simply reading a way bigger number than six and mindlessly use them as upper bound of the loop that's supposed to loop over AN ARRAY OF SIX ITEMS.

3. Use proper types.
Code
double tempfilerow;
double tempfilecol;
Are you expecting to have 6.28 rows? Why are the indexes not integer?
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 08:24pm
LOL it cant be that bad, this is how my professor wants us to write it. Ive been doing it like this for all my programs and i have been doing well.

probably should have mentioned the output should be this:

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: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 31 2014 08:49pm
Quote (mrxskyline12 @ May 31 2014 10:24pm)
LOL it cant be that bad, this is how my professor wants us to write it. Ive been doing it like this for all my programs and i have been doing well.


just because your prof isn't failing you doesn't mean you're doing it well. profs have to be lenient towards newbies or else so many of them would fail.
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 08:53pm
Quote (carteblanche @ May 31 2014 06:49pm)
just because your prof isn't failing you doesn't mean you're doing it well. profs have to be lenient towards newbies or else so many of them would fail.


i never said i was doing IT (the program) well. I said that i was doing well (in the class)

I have an A

ive understood every other program and have done it properly
i just dont understand how to copy numbers from a file into an array.

plus everybody has their own way of writing programs, this is mine. Never said it was the correct way.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 31 2014 09:11pm
Quote
LOL it cant be that bad,
I'm sorry to be the bearer of bad news, but it is quite bad

Quote
this is how my professor wants us to write it. Ive been doing it like this for all my programs and i have been doing well.
Look, it is not uncommon amongst professors to be stuck in the 80's. That seems to be the case here. And no, it's not "good enough" anymore, the world has changed within last 30 years and what used to be state-of-the-art is now often outdated and/or simply wrong.

Quote
ive understood every other program and have done it properly
Quote
plus everybody has their own way of writing programs, this is mine. Never said it was the correct way.
Way to doublethink, Big Brother would be proud.

There is a matter of personal preferences when you're coding, but there's also a matter of being right or wrong. This is the latter. Naming variables and indentation style could be the former.

This post was edited by KrzaQ2 on May 31 2014 09:12pm
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:15pm
funny thing is, my professor is quite young. He is probably in his late 20's or early 30's

well this is my first C++ class, so i know im pretty bad, you dont have to keep pointing that out.

I do need help though, still not sure what im doing wrong, i changed a few of the things that you pointed out first.
What now?

#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(double [][COLS], int);
void showArray(double [][COLS], int);

int main()
{
const int ROWS = 6;

double temp[ROWS][COLS];
copyFile(temp, ROWS);
showArray(temp, ROWS);
}

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

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

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

void showArray(double 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: 23,862
Joined: Aug 16 2006
Gold: 20.00
May 31 2014 09:18pm
Quote (mrxskyline12 @ May 31 2014 09:24pm)
LOL it cant be that bad, this is how my professor wants us to write it. Ive been doing it like this for all my programs and i have been doing well.

probably should have mentioned the output should be this:

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


child, open your eyes to the modern C++. Heed the words of KrzaQ
Member
Posts: 10,129
Joined: Sep 1 2007
Gold: 7,002.00
May 31 2014 09:19pm
Quote (Eep @ May 31 2014 07:18pm)
child, open your eyes to the modern C++. Heed the words of KrzaQ


haha yea im trying.
geez this is my 1st C++ class, cut me some slack guys

i know im wrong, but why?
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 31 2014 09: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.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll