| | |
Files and Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
When I run this program I can only access the first row of data. Can any one help so that I can access all the lines? I am trying to store numbers into a 2-dimensional array.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
ifstream inData;
float numbers[25][10];
int rows=0,cols;
inData.open("data.txt");
while (inData){
for (rows=0; rows<=7; rows++){
for (cols=0; cols<=7; cols++)
{
inData >> numbers[rows][cols];
cout<<fixed<<showpoint<<setprecision(1)<<numbers[rows][cols]<<" ";
}
cout<<endl;
}
}
cout<<endl;
cout<<fixed<<showpoint<<setprecision(1)<<numbers[0][1];
return 0;
} Last edited by JackDurden; Jul 28th, 2008 at 10:19 pm.
How about this one? But this has the danget of writing outside the bounds of the array, so you need to add more checkiing.
C++ Syntax (Toggle Plain Text)
rows = cols = 0; while( infile >> numbers[rows][cols]) { cols++; if(cols > 7) { cols = 0; rows++; } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
It looks like you have declared an array of 25 rows and 10 columns. I'd suggest you intialize the rows to their value(rows=25) and columns (cols=10). Try my example
dont forget to declare your array numbers[rows][cols]
C++ Syntax (Toggle Plain Text)
for (i=0; i<=rows; i++) { for (j=0; j<=cols; cols++) { inData >> numbers[i][j]; cout<<fixed<<showpoint<<setprecision(1)<<numbers[i][j]<<" "; } }
Last edited by joshmo; Jul 29th, 2008 at 4:42 am.
>> for (i=0; i<=rows; i++)
I hope you realize that is going to cause data overflow errors which might possibly crash your program or destroy other parts of your program if that code is incorporated into a larger program. Why? Because the loop performs one too many iterations -- if the value of rows is 25 then the above loop will count from 0 to 26, which is one too many. Same with the inner j loop.
What you want is this:
Then there is the danger that the data file doesn't contain that many numbers (rows * columns).
I hope you realize that is going to cause data overflow errors which might possibly crash your program or destroy other parts of your program if that code is incorporated into a larger program. Why? Because the loop performs one too many iterations -- if the value of rows is 25 then the above loop will count from 0 to 26, which is one too many. Same with the inner j loop.
What you want is this:
for (i=0; i < rows; i++) Then there is the danger that the data file doesn't contain that many numbers (rows * columns).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
•
•
•
•
>> for (i=0; i<=rows; i++)
I hope you realize that is going to cause data overflow errors which might possibly crash your program or destroy other parts of your program if that code is incorporated into a larger program. Why? Because the loop performs one too many iterations -- if the value of rows is 25 then the above loop will count from 0 to 26, which is one too many. Same with the inner j loop.
What you want is this:
for (i=0; i < rows; i++)
Then there is the danger that the data file doesn't contain that many numbers (rows * columns).
![]() |
Similar Threads
- Need reference to Random Accessing of Text files (C++)
- help with parrallel arrays (C++)
- Problem with Character Arrays (C++)
- i/o files (C++)
- Double Arrays HELP!!!!!! (C++)
- Can someone that knows arrays check my program out so far (C++)
- Doubt about compiling. (Legacy and Other Languages)
- array of files in C (C)
- C file input/output 2D arrays. (C)
- sorting 2d arrays (C)
Other Threads in the C++ Forum
- Previous Thread: C to C++
- Next Thread: Need Mini Project
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






