473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I've got the Vector (input parsing) blues

Hello all,

It's my fisrt post here and I am feeling a little stupid here, so go easy..
:) (Oh, and I've spent _hours_ searching...)

I am desperately trying to read in an ASCII "stereolithography" file (*.STL)
into my program. This has the following syntax...

Begin STL Snippet
**********
facet normal -0.00000904 -0.00002272 -1.00000000
outer loop
vertex -2.71325651 0.49965868 -0.00399721
vertex -1.14940957 0.00000000 -0.00400000
vertex -5.03708494 0.00000000 -0.00396485
endloop
endfacet
*********
End STL Snippet

I am putting the various numbers into a facet (an array of vectors) -
myArray[numOfFacets][4]; - from my vector class. Slots 0,1 & 2 are taken by
the vector components, and 3 by the normal, with numOfFAcets being
(drumroll...) the number of facets.

I have two problems..

1) I want to dynamically size the vector array, as the number of facets will
be different for different input files. HOWEVER, I read through the file and
I then 'numOfFacets++;' for each occurence of 'facet'. But, I get...

cfdcasdr1_1.cpp(54) : error C2057: expected constant expression
cfdcasdr1_1.cpp(54) : error C2466: cannot allocate an array of constant size
0
cfdcasdr1_1.cpp(54) : error C2133: 'myArray' : unknown size

.... upon compiling. (VC++ 6.0) Is there a way around this?

Also,

2) I have a 'while' loop to read the numbers in, doing a strtod() on them.
The problem is, I am relying on the program seeing the 'vertex' and 'normal'
part of the file to tell the program that the next (three) inputs are going
to be the vector's x, y and z coordinates.

unfortunately the...

if (buffer == "vertex")
{
}

....thing won't work with strings (AFAICT). How can I use the 'if' statement
to this end? I could to a strtod() - STRing TO Double - on everything, but
if it can't do it, it returns 0 - which the vector components can _also_ be,
so this is no help! :/

I would greatly appreciate any help you could give me.

Alexander Livingstone
--
alex _dot_ livingstone *at* btinternet _dot_ com
Jul 19 '05 #1
9 3181

"{AGUT2}=IWIK=" <al********************@ambtinternet.com> wrote in message
news:bi**********@hercules.btinternet.com...
Hello all,

It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...)

I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax...

Begin STL Snippet
**********
facet normal -0.00000904 -0.00002272 -1.00000000
outer loop
vertex -2.71325651 0.49965868 -0.00399721
vertex -1.14940957 0.00000000 -0.00400000
vertex -5.03708494 0.00000000 -0.00396485
endloop
endfacet
*********
End STL Snippet

I am putting the various numbers into a facet (an array of vectors) -
myArray[numOfFacets][4]; - from my vector class. Slots 0,1 & 2 are taken by the vector components, and 3 by the normal, with numOfFAcets being
(drumroll...) the number of facets.

I have two problems..

1) I want to dynamically size the vector array, as the number of facets will be different for different input files. HOWEVER, I read through the file and I then 'numOfFacets++;' for each occurence of 'facet'. But, I get...

cfdcasdr1_1.cpp(54) : error C2057: expected constant expression
cfdcasdr1_1.cpp(54) : error C2466: cannot allocate an array of constant size 0
cfdcasdr1_1.cpp(54) : error C2133: 'myArray' : unknown size

... upon compiling. (VC++ 6.0) Is there a way around this?

I'm sure there is, but you need to post code, not tiny snippetts of it, but
proper compilable code. Line 54 would be a help, as would the complete
definitions of any variables or types on line 54. And how about what you are
calling 'your vector class'.

Also,

2) I have a 'while' loop to read the numbers in, doing a strtod() on them.
The problem is, I am relying on the program seeing the 'vertex' and 'normal' part of the file to tell the program that the next (three) inputs are going to be the vector's x, y and z coordinates.

unfortunately the...

if (buffer == "vertex")
{
}

...thing won't work with strings (AFAICT).
Again the problem is the incomplete code. What is the type of buffer? What
is its value? Did you know there are two types of strings in C++? Which one
are you using?

I would guess that the answer is

if (strcmp(buffer, "vertex") == 0)
{
}

but since my crystal ball is at the menders I'm not really sure.
How can I use the 'if' statement
to this end? I could to a strtod() - STRing TO Double - on everything, but
if it can't do it, it returns 0 - which the vector components can _also_ be, so this is no help! :/

I would greatly appreciate any help you could give me.

Alexander Livingstone


Please post proper code, this sounds easy enough.

john
Jul 19 '05 #2
In article <bi**********@hercules.btinternet.com>,
{AGUT2}=IWIK= <al********************@ambtinternet.com> wrote:

Begin STL Snippet
**********
facet normal -0.00000904 -0.00002272 -1.00000000
outer loop
vertex -2.71325651 0.49965868 -0.00399721
vertex -1.14940957 0.00000000 -0.00400000
vertex -5.03708494 0.00000000 -0.00396485
endloop
endfacet
*********
End STL Snippet

I am putting the various numbers into a facet (an array of vectors) -
I take it you're using "vector" to mean simply one of the triplets of
numbers that appear in the snippet above? If so, you should be aware that
to a C++ programmer, "vector" means a data structure somewhat like an
array, but more powerful, that is included in the C++ standard library.
This might cause some miscommunication.
myArray[numOfFacets][4]; - from my vector class. Slots 0,1 & 2 are taken by
the vector components, and 3 by the normal, with numOfFAcets being
(drumroll...) the number of facets.
OK, so in C++ terms, you're trying to use a two-dimsnsional array, or more
precisely an array of arrays.
1) I want to dynamically size the vector array, as the number of facets will
be different for different input files. HOWEVER, I read through the file and
I then 'numOfFacets++;' for each occurence of 'facet'. But, I get...

cfdcasdr1_1.cpp(54) : error C2057: expected constant expression
cfdcasdr1_1.cpp(54) : error C2466: cannot allocate an array of constant size
0
cfdcasdr1_1.cpp(54) : error C2133: 'myArray' : unknown size
This sounds like you're trying to use a variable for one dimension of the
array, to establish the size dynamically at run time when you declare it.
You can't do this directly. You have to allocate the memory explicitly
using 'new', keep a pointer to it, and then deallocate the memory when
you're finished. This is tricky for two-dimensional arrays.

However, the aforementioned C++ vectors *can* be sized (and even resized)
dynamically at run time. They handle their own memory allocation. So
instead of an array of arrays, you might try a vector of vectors:

#include <vector>

using namespace std; // if you don't have this already

vector<vector<double> > myArray (numOfFacets, vector<double>(4));

This declares a vector (named myArray) which contains numOfFacets vectors,
each of which in turn contains four doubles. numOfFacets can be a
variable determined at run time, and you can access each item in myArray
using ordinary subscript notation: myArray[facetNum][k].
2) I have a 'while' loop to read the numbers in, doing a strtod() on them.
The problem is, I am relying on the program seeing the 'vertex' and 'normal'
part of the file to tell the program that the next (three) inputs are going
to be the vector's x, y and z coordinates.

unfortunately the...

if (buffer == "vertex")
{
}

...thing won't work with strings (AFAICT).


I take it you're using arrays of char as C-style strings? Using the ==
operator actually compares pointers to the locations of the strings in
memory, which will give you equality only if the two strings are actually
the same string (i.e. in the same memory location). To compare the
*contents* of two C-style strings, you need to use the strcmp() function.

You *can*, however, use == to compare two C++-style strings:

#include <string>

using namespace std; // if you don't already have this somewhere

string buffer;
// read into buffer from your data, then
if (buffer == "vector")
{
// do something
}

It looks like you're learning C++ from a book that teaches the older
C-style constructs before teaching the newer C++-style constructs (if
indeed it does use the C++ style stuff at all). You might try looking for
a more modern book. Many people here (including me) will recommend Koenig
and Moo's "Accelerated C++" if you already have some programming
experience.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #3
John,

Thanks very much for the assistance! I'm composing this on a train on my
elderly notbook, but will be back with my box soon, so will try the
programs with the strcmp() thing when I get back. In the mean time, I've
included the code of my program so far....

#include "stdafx.h"
#include "stdlib.h"
#include "math.h"
#include "iostream.h"
#include "iomanip.h"
#include "fstream.h"
#include "vectorr1_1.h"
#include "string.h"
#include "string"

// I'm sure I don't need all these, but I'll strip them out later. :)

int main()
{
char fileName[255];
cout << "Enter the name of the input *.STL file (including extension): ";
cin >> fileName;
ifstream inf(fileName);
char buffer[128];
char * ehWhy;
int facetTot=1;
int facetNum=0;
int normFlag=0;
int vertFlag=0;
int compFlag=0;
int solidFlag=0;
char vcheck[128] = "\nvertex";
char ncheck[128] = "\nnormal";

while(!inf.getline(buffer, 128 ,' ').eof())
{
// What I have done here is put the carriage return from
// the line before in the buffer check. Do I need to do this?
// If so is this the right way? or should I use that ignore
// doodad that I have seen in reference to strings
if(buffer == "\nfacet")
{
facetTot++;
}
}
const int facetTot1 = facetTot;

// This was 'line 54' - I've stripped some stuff to uncomplicate it.
// The compiler doesn't like it. Even with facetTot set to 1 (I'll
// fix the exact facet numbering later).
vector mySurface[facetTot1][4];
while(!inf.getline(buffer, 128, ' ').eof())
{
if (buffer == "\nnormal")
{
cout << ".";
normFlag=1;
compFlag=1;
} else if (buffer == "\nvertex" )
{
vertFlag++;
} else if (normFlag != 0)
{
// This is one thing I don't understand. TBH I'm not really sure
// about pointers in general, but am particularly curious as to
// why this pointer is necessary.
mySurface[facetNum][4].setComp(compFlag, strtod(buffer,&ehWhy));
compFlag++;
if (compFlag == 4)
{
compFlag=0;
normFlag=0;
}
} else if (vertFlag != 0)
{
mySurface[facetNum][vertFlag].setComp(compFlag, strtod(buffer,&ehWhy));
compFlag++;
if (compFlag == 4)
{
compFlag=0;
if (vertFlag == 4)
{
vertFlag=0;
facetNum++;
}
}
}
}
return 0;
}
******* End of main code file. ***********

******* Begin Vector.h *********
#include "iostream.h"
#include "math.h"

class vector
{
protected: // The 'protected' part ensures that 'xcoord' etc. are only
accessible from withing this specific class.

double xcoord, ycoord, zcoord;

public:

// Default constructor: when a vector is specified without arguments, it's
default co-cordinates are set to '0'
vector()
{
xcoord=0;
ycoord=0;
zcoord=0;
}

// Cartesian constructor: when argumants are supplied, they are set as the
co-ordinates.
vector(double x, double y, double z)
{
xcoord=x;
ycoord=y;
zcoord=z;
}

// Prints out to the screen.
void print()
{
cout << "(" << xcoord << "," << ycoord << "," << zcoord << ")";
}

// Access methods for the co-ordinates

double getx()
{
return xcoord;
}

double gety()
{
return ycoord;
}

double getz()
{
return zcoord;
}

double getComp(int compNum)
{
if (compNum == 1)
{
return xcoord;
} else if (compNum == 2)
{
return ycoord;
} else if (compNum == 3)
{
return zcoord;
} else
{
cout << "\n\n Error! Error! Vector getComp\n\n";
}
}

double lenOf()
{
double lengthVal=0;
lengthVal=(xcoord*xcoord);
lengthVal+=(ycoord*ycoord);
lengthVal+=(zcoord*zcoord);
return lengthVal;
}

void setx( double value)
{
xcoord=value;
}

void sety( double value)
{
ycoord=value;
}

void setz( double value)
{
zcoord=value;
}

void setComp(int compNum, double value)
{
if (compNum == 1)
{
xcoord=value;
} else if (compNum == 2)
{
ycoord=value;
} else if (compNum == 3)
{
zcoord=value;
} else
{
cout << "\n\n Error! Error! Vector setComp\n\n";
}
}

// Overload + - Addition
vector operator+(vector v_old)
{
vector v_sum(v_old.getx()+xcoord, v_old.gety()+ycoord, v_old.getz()
+zcoord);
return v_sum;
}

// Overload - - Subtraction
vector operator-(vector v_old)
{
vector v_diff(xcoord-v_old.getx(), ycoord-v_old.gety(), zcoord-
v_old.getz());
return v_diff;
}

// Overload * - Scalar multiplication
vector operator*(double scalar)
{
vector v_scalar(xcoord*scalar, ycoord*scalar, zcoord*scalar);
return v_scalar;
}

// Overload / - Scalar division
vector operator/(double scalar)
{
vector v_scalar(xcoord/scalar, ycoord/scalar, zcoord/scalar);
return v_scalar;
}

// Overload += = Self-assigned Addition
vector operator+=(vector v_old)
{
xcoord+=v_old.getx();
ycoord+=v_old.gety();
zcoord+=v_old.getz();
}

// Overload -= Self-assigned Subtraction
vector operator-=(vector v_old)
{
xcoord-=v_old.getx();
ycoord-=v_old.gety();
zcoord-=v_old.getz();
}

// Overload *= Self-assigned Multiplication
vector operator*=(double scalar)
{
xcoord*=scalar;
ycoord*=scalar;
zcoord*=scalar;
}

// Overload /= Self-assigned Multiplication
vector operator/=(double scalar)
{
xcoord/=scalar;
ycoord/=scalar;
zcoord/=scalar;
}

// Overload * - Dot product
double operator*(vector v_old)
{
double dotProd((xcoord*v_old.getx())+(ycoord*v_old.gety() )
+(zcoord*v_old.getz()));
return dotProd;
}

// Overload ^ - Cross product
vector operator^(vector v_old)
{
vector v_cross((ycoord*v_old.getz())-(zcoord*v_old.gety()),
(zcoord*v_old.getx())-(xcoord*v_old.getz()),
(xcoord*v_old.gety())-(ycoord*v_old.getx()));
return v_cross;
}
}; //EVIL SEMI-COLON MUST STAY TO AVOID DAYS OF WONDERING WHAT IS WRONG
WITH YOUR HEADER!!!

Copyright © Alexander Livingstone, 2003

******* End Vector.h *****

Yes, I know it's newbie programming, and probably not worthy of a
copyright, but I'm proud of it ;)

Thank you for the help.

Yours,

Alexander.
--
alex _dot_ livingstone *at* btinternet _dot_ com
Jul 19 '05 #4
> I take it you're using "vector" to mean simply one of the triplets of
numbers that appear in the snippet above? If so, you should be aware
that to a C++ programmer, "vector" means a data structure somewhat like
an array, but more powerful, that is included in the C++ standard
library. This might cause some miscommunication.
Eek! Ok, cheers. My vector calss isn't QUITE so impressive as the one in
the old include directory, however...
However, the aforementioned C++ vectors *can* be sized (and even resized)
dynamically at run time. They handle their own memory allocation. So
instead of an array of arrays, you might try a vector of vectors:

#include <vector>
using namespace std; // if you don't have this already
vector<vector<double> > myArray (numOfFacets, vector<double>(4)); You *can*, however, use == to compare two C++-style strings: It looks like you're learning C++ from a book that teaches the older
C-style constructs before teaching the newer C++-style constructs (if
indeed it does use the C++ style stuff at all). You might try looking
for a more modern book. Many people here (including me) will recommend
Koenig and Moo's "Accelerated C++" if you already have some programming
experience.


Thank you for the advice. I have a '97 version of '21 days' that's
yellowing already.. :)

It looks like some frightening syntax changes, but I'll give it a go...

Alexander.
--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #5
{AGUT2} {H}-IWIK <al********************@ambtinternet.com> writes:
I take it you're using "vector" to mean simply one of the triplets
of numbers that appear in the snippet above? If so, you should be
aware that to a C++ programmer, "vector" means a data structure
somewhat like an array, but more powerful, that is included in the
C++ standard library. This might cause some miscommunication.
Eek! Ok, cheers. My vector calss isn't QUITE so impressive as the one
in the old include directory, however...
However, the aforementioned C++ vectors *can* be sized (and even
resized) dynamically at run time. They handle their own memory
allocation. So instead of an array of arrays, you might try a
vector of vectors:

#include <vector>
using namespace std; // if you don't have this already
vector<vector<double> > myArray (numOfFacets, vector<double>(4));

You *can*, however, use == to compare two C++-style strings:

It looks like you're learning C++ from a book that teaches the older
C-style constructs before teaching the newer C++-style constructs (if
indeed it does use the C++ style stuff at all). You might try
looking for a more modern book. Many people here (including me)
will recommend Koenig and Moo's "Accelerated C++" if you already
have some programming experience.


Thank you for the advice. I have a '97 version of '21 days' that's


'21 days'? Scariest thing one might see in the title of a C++
book. See the book reviews at www.accu.org, and look up yours to
see what experienced programmers think of it.
yellowing already.. :)

It looks like some frightening syntax changes, but I'll give it a
go...


But look on the bright side. All those troubles you're having with
arrays that can't be resized, just go away, because vector<double>
can be resized. And so on. Really, the standard library makes your
life a lot easier.
Jul 19 '05 #6
In article <op**************@mercury.nildram.net>,
{AGUT2} {H}-IWIK <sp@am> wrote:

Thank you for the advice. I have a '97 version of '21 days' that's
yellowing already.. :)


1997 is rather old in C++ terms. That's just before the C++ standard came
along and introduced nifty stuff like vectors and strings, along with
changing the names of the standard headers and introducing the concept of
"namespaces" that you have to deal with somehow if you're using the new
headers (even if only in Band-aid type fashion via 'using namespace
std;').

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #7

"{AGUT2} {H}-IWIK" <al********************@ambtinternet.com> wrote in
message news:op**************@mercury.nildram.net...
John,

Thanks very much for the assistance! I'm composing this on a train on my
elderly notbook, but will be back with my box soon, so will try the
programs with the strcmp() thing when I get back. In the mean time, I've
included the code of my program so far....

[snip]
const int facetTot1 = facetTot;

// This was 'line 54' - I've stripped some stuff to uncomplicate it.
// The compiler doesn't like it. Even with facetTot set to 1 (I'll
// fix the exact facet numbering later).
vector mySurface[facetTot1][4];


You've obviously heard that array bounds must be constant, that's why you
say const int facetTot1 ..., but actually its worse than that. Array bounds
must be compile time constants, that is (more or less) the compiler must be
able to work out what the value is when it is compiling. Obviously this is
not the case here since facetTot is computed when the program is running.

You need dynamic memory allocation, are you familiar with the new operator?

typedef vector surface[4];

surface* mySurface = new surface[facetTot];

That says a surface is an array of four vectors, and then dynamically
allocates facetTot surfaces and assigns then to the pointer mySurface. The
typedef statement should go somewhere near the top of your code, after you
include the vector header but before main.

Somewhere later in your code (probably at the end of main) you should say
that you no longer need mySurface with

delete[] mySurface;

That releases the dynamically allocated memory.

Because arrays and pointers are very similar beasties you should be able to
use the pointer mySurface in exactly the same way as the array mySurface and
so you should need to change the rest of your code.

john
Jul 19 '05 #8
> so you should need to change the rest of your code.


typo - so you shouldn't need to change the rest of your code.

john
Jul 19 '05 #9
Thank you all, you've been a wonderful help. This is why I love the
internet :) I shall stay subscribed and when I have a little skill, will
endeavour to put something back.

Yours,

Alexander

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
7
by: D. Susman | last post by:
Hi, In the code snippet below, I am trying to read the contents of a file into a vector. However I can't get it compiled. I am using Sunstudio which compiles C++ using cc. Here is the code:...
8
by: ejack | last post by:
Hello: This is my first time here so I hope I am doing this correctly. I am trying to push a vector object into a vector (I think). Here is my code (header file first) but it's only part of the...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.