Re: pass a vector to a void function?
"Default User" <defaultuserbr@yahoo.com> wrote in message
news:63baq3F269165U1@mid.individual.net...
John Brawley wrote:
I've tried everything I could find on the web; my code should be
right; the vector itself works right, but I can't get the filewriter
to write anything into the file it creates....
[code snipered]
The program compiles, runs, I can see the values changing in the
vector as they're supposed to, but the filewriter just creates an
empty file.... What'm I doing wrong?
Please most COMPLETE minimal code that demonstrates the problem. As
shown, you have no headers included, and most of your variables are
undeclared.
Problem:
I posted what wasn't working... The filewriter function was complete enough,
the problem stated. HOWever:
I shortened everything to minimum, and this works:
#include <vector>
#include <fstream>
#include <ostream>
#include <cstdlib>
using namespace std;
vector<double> vec;
void writfil(const vector<double> &vec) {
ofstream it8vfil;
it8vfil.open ("it8test.3d");
if (it8vfil.is_open()) {
for (int k=0;k<33;k++) {
double a=vec[0+k];
double b=vec[1+k];
double c=vec[2+k];
it8vfil << a << " " << b << " " << c << "\n"; }
}
it8vfil.close();
}
main() {
for (int i=0;i<33;i++) {
double val=rand()%1000*0.001;
vec.push_back(val);
}
writfil(vec);
return 0;
}
<snips>>
Get rid of the platform-specific stuff like kbhit() until you have a
working portable version.
I have one, using array[]s where I'm trying to use vectors.
Don't use goto. You are not experienced enough for that. When you are
experienced enough, you won't want to.
Brian
There's no choice, really.
Search archives on my name, find out why....
(There's only one goto, which jumps out of the switch() statement and
terminates the program. Since it's never supposed to stop running until the
user demands so, the goto makes sense here (and perhaps *only* here, since
otherwise you are quite right).
NOW I have the problem that the densified example above works perfectly: the
file has content, but the main program's _apparently_identical_ filewriter
and function call, those relevant pieces extracted and put into my first
message, doesn't.
Posting the entire program would be both badndwidth, and embarassing...
If no one can figure out what was wrong with the code in my first (well,
second...) message, when it's identical in structure, function and purpose
to this *working* piece, well, I don't know where else to turn....
Thanks.
--
Peace
JB
jb@tetrahedraverse.com
Web: http://tetrahedraverse.com