Computing.Net > Forums > Programming > c++ 2d array

c++ 2d array

Reply to Message Icon

Original Message
Name: BYUengineer
Date: February 8, 2005 at 10:48:06 Pacific
Subject: c++ 2d array
OS: Windows XP
CPU/Ram: Pentium 4 in a ?Ram
Comment:

I am new to programming, and I need help inputting a 2d array from file. First I will explain what I am trying to do, then I will explain how I am trying to do it. If I'm going about it completely the wrong way I'm open to suggestions.

I am programming in a CAD API and I am trying to creat a series of lines. I have a datafile with x and y coordinates for points that I would like to connect. I have a create line function that requires the input of two, 2d vectors. each 2d vector is one point. the text file is like this:

0.05 0.21
0.35 2.5

etc. for about 200 data points.

Here is How I am trying to do this:

//***Start of Line ***
cout << "---Line" << endl;

//input points from file
string spFileName

cout << "Enter the Name of the Text
File You Would Like to Use"
<< endl;

cin << spFileName << endl;

ifstream inputstream(spFileName);

if (!inputstream)
{
cout << "ERROR: Incorrect
File Type" << endl;
return;
}
else
{
cout << "OK: File Imported"
<< endl;
}

// get vector size (Don't know how)

// I'm not sure if this is right
vector spPoint[spSize][2]

for (i=0, i<= spSize, i++)
{
filein << spPoint[i][0];
filein << spPoint[i][1];
}


am I even close?

Thanks for any help
Eric


Report Offensive Message For Removal


Response Number 1
Name: Don Arnett
Date: February 8, 2005 at 12:19:26 Pacific
Subject: c++ 2d array
Reply: (edit)

We need to know how a 'vector' is defined. According to your code, a vector must be a single value. I would think that a 'vector' is an object that would hold two values.

I don't use C++ I/O, but should the 'filein' lines be something like:

inputstream >> spPoint[i][0];


Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 2
Name: BYUengineer
Date: February 8, 2005 at 13:15:37 Pacific
Subject: c++ 2d array
Reply: (edit)

I think I've fugured out a different way to do it that does not require the vector/array (whichever's best)to be dynamic. I can just have my loop that inputs the points run until a NULL is returned, then have it exit the loop.

It seems like this should work, but is probably not the most secure way to do it.

A vector can hold anywhere from 0 to n values. What I'm trying to make is a series of vectors that each have two values.(an x and y coordinate)


Report Offensive Follow Up For Removal

Response Number 3
Name: BYUengineer
Date: February 8, 2005 at 14:28:04 Pacific
Subject: c++ 2d array
Reply: (edit)

Latest Update:

I've been doing some reading and vectors better. I need to create a vector like so:

vector<double> spPoint(n)(2);

My problems are:

My n and my two might be reversed, I'm not positive that I can even create a 2D vector, and I don't know how to create a vector with an arbitrary size.

Help!

Eric


Report Offensive Follow Up For Removal

Response Number 4
Name: egkenny
Date: February 8, 2005 at 16:15:33 Pacific
Subject: c++ 2d array
Reply: (edit)

point.h
=======
typedef struct
{
float x;
float y;
} point;

data file
=========
12.34 56.0
56.78 90.0
11.1 11.0
22.2 22.0
33.3 33.0
44.4 44.0
55.5 55.0
66.6 66.0
77.7 77.0
88.8 88.0
99.9 99.0

program.cpp
===========
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

#include "point.h"

void main()
{

point p;

vector<point> spPoint;

spPoint.clear();

cout << "---Line" << endl;

//input points from file
char spFileName[80];

cout << " Enter the Name of the Text File You Would Like to Use" << endl;

cin >> spFileName;

ifstream inputstream(spFileName);

if (inputstream.fail())
{
cout << "ERROR: Incorrect File Type" << endl;
return;
}
else
{
cout << "OK: File Imported" << endl;
}

inputstream >> p.x >> p.y;
while(!inputstream.eof())
{
spPoint.push_back(p);
inputstream >> p.x >> p.y;
}

for(int i=0; i<spPoint.size();i++)
{
cout << i << " " << spPoint[i].x << " " << spPoint[i].y << endl;
}
}



Report Offensive Follow Up For Removal

Response Number 5
Name: wizard-fred
Date: February 9, 2005 at 01:59:07 Pacific
Subject: c++ 2d array
Reply: (edit)

I think Don Arnett is right. A line segment is defined by two pounts, each point has an x and y coordinate.

One data set - x1, y1, x2, y2

It doesn't matter if x1 is greater than x2 or y1 is greater than y2 as long as the values are absolute. You always start at x1,x2 and draw/move to x2,y2.

egkenney - you only have 11 data pairs


Report Offensive Follow Up For Removal


Response Number 6
Name: BYUengineer
Date: February 9, 2005 at 15:57:36 Pacific
Subject: c++ 2d array
Reply: (edit)

Yeah, that should work.

Thanks a lot.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: c++ 2d array

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge