|
unsigned int* link[200];
float* delms[200];
float* sds[200];
float* delmih[200];
float* sdih[200];
for (int i=0;i<200;i++)
{
link[i]=new unsigned int[4];
delms[i]=new float[4];
sds[i]=new float[4];
delmih[i]=new float[4];
sdih[i]=new float[4];
}
......................................codes initialized the above arrays with values
dlay_tme(delms,sds,delmih,sdih,nlinks,ndih,link,time,time_var);
//
//
delete [] *link;
delete [] *delms;
delete [] *sds;
delete [] *delmih;
delete [] *sdih;
I got the following error message, How to pass 2D dynamic array to the function ?? Please correct.
Thanks
blst_vi_procDlg.cpp
c:\blast_vib_proc_model_from_d\blst_vi_procdlg.cpp(522) : error C2664: 'void __thiscall CBlast_vib_procDlg::dlay_tme(float [][4],float [][4],float [][4],float [][4],unsigned int [],unsigned int [],unsigned int [][4],float [],float [])' : cannot
convert parameter 1 from 'float *[200]' to 'float [][4]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
feature_display.cpp
Error executing cl.exe.
blast_vib_proc.exe - 1 error(s), 0 warning(s)
|
|
|
|
|
As an example you can allocate an array of array of ints, pass the array to a function which deletes it like this:
void initit()
{
int* stuff[100];
for (int i = 0; i < 100; i++)
{
stuff[i] = new int[5];
}
deleteit(stuff);
}
void deleteit(int* stuff[])
{
for (int i = 0; i < 100; i++)
delete [] stuff[i];
}
Haven't tested this thouroughly, but it should work. Please correct me if I'm wrong.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
|
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files.
For example:
In version.h
------------
#ifndef VERSION
#define VERSION
#include <string>
const std::string version("alpha_0-22");
#endif
In main.cpp
------------
#include <cstdio>
#include "version.h"
extern void fun();
int main()
{
printf("address of version in main=%p\n", &version);
fun();
getchar();
}
In fun.cpp
------------
#include <cstdio>
#include "version.h"
void fun()
{
printf("address of version in fun=%p\n", &version);
}
The output of the program will be:
address of version in main=00431960
address of version in fun=00431984
It seems that two copies of version string had been created if version.h is included in different cpp file scope. Now I am worry if version.h file is included in thousand of cpp files, will thousand of version string object be created?!
My alternative workaround on this is, I will let version.h declare the version string and version.cpp define the version string.
In version.h
------------
#ifndef VERSION
#define VERSION
#include <string>
extern const std::string version;
#endif
In version.cpp
--------------
#include "version.h"
const std::string version("alpha_0-22");
Again, here is my output:
address of version in main=00431960
address of version in fun=00431960
It seems that the const string just be constructed one time only.
I am not sure whether this is the correct workaround? Or my concern on multiple creation of const object is not an issues?
Please refer to http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7 on why I am using const instead of const.
Thank you for your feedback.
yccheok
|
|
|
|
|
Place:
#pragma once
at the very beginning of the header file. This instructs the compiler to only compile the header file once.
|
|
|
|
|
Hem... dun think that is the root cause of this problem. since #ifndef...#define pair had already served the purpose for #pragma once...
|
|
|
|
|
#ifndef, #define and #endif combination works to prevent redundant inclusion in the same file.
This will not work if you are including them in different .cpp files. Since in that .cpp file the macro that you are checking is not defined. Hence it creates another std::string version var.
What you have to do is create a global variable by declaring it in a header file which will be included everywhere (for example stdafx.h in VC6) like wise
extern std::string version; and then defining it in any one and only one of the .cpp files.
Now you can use the variable by including the header file where you wish to use the variable.
Do read the faqs[^] too.
Nibu thomas
Software Developer
|
|
|
|
|
anybody knw how to stop the output of the program...i mean stop the loop of this program..the program dont stop..even i tried in many way..
<code>#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
time_t tmStart, tmEnd;
void DoStuff()
{
tmStart = time(0);
ifstream fin("data.txt",ios::in);
ofstream File("MyFile.txt",ios::out);
char str[14];
fin.getline(str,sizeof(str),'\n');
string line(str);
cout << str<<endl;
int i=0;
while(File.eof !=0)
{
random_shuffle(line.begin(), line.end());
File << "Randomised: " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " "<< endl;
// File << "Single Row: " << s << " " << s << " " << s <<" " << s << endl;
File << " "<< endl;
++i;
tmEnd = time(0);
cout << tmEnd-tmStart << " seconds " << endl;
}
File.close ();
fin.close ();
}
int main(int argc, char* argv[])
{
DoStuff();
cout << "finished"<<endl;
return 0;
}
</code>
|
|
|
|
|
A couple of things. Firstly eof is a function. It should be called like this: while(!File.eof()) - Does this code compile?!?. Secondly the fact that you're checking "File" for an end of file condition doesn't make logical sense: it's the output file, the input file is the one you should be checking.
Steve
|
|
|
|
|
which input file is the one is should be checking.This code compile and its continueing looping...how to check the input file.
|
|
|
|
|
First of all, the condition that you are applying is wrong. Because, eof( ) is a function that returns true if current position of the file pointer is end of file. It is used when you are getting input from file. When you are copying something to file, you can not use this condition because as you copy something to file, the end of file pointer moves immediate after last charecter in file. As you are getting input from data.txt, you should modify your loop like this,
while (!fin.eof())<br />
{<br />
do something;<br />
} if you still have problem, let us know.
Good luck.
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
i tried that allready..but now it dont wanna loop..onli the input from the file appears..and then..there is no output in the output file "MyFile.txt"..my code is down here...
<code>#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
time_t tmStart, tmEnd;
void DoStuff()
{
tmStart = time(0);
ifstream fin("data.txt",ios::in);
ofstream File("MyFile.txt",ios::out);
char str[14];
fin.getline(str,sizeof(str),'\n');
string line(str);
cout << str<<endl;
int i=0;
while(!fin.eof() !=0)
{
random_shuffle(line.begin(), line.end());
File << "Randomised: " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " "<< endl;
// File << "Single Row: " << s << " " << s << " " << s <<" " << s << endl;
File << " "<< endl;
++i;
tmEnd = time(0);
cout << tmEnd-tmStart << " seconds " << endl;
}
File.close ();
fin.close ();
}
int main(int argc, char* argv[])
{
DoStuff();
cout << "finished"<<endl;
return 0;
}
</code>
|
|
|
|
|
You are not using my code...
I did not put !=0.
Also send us your file format of data.txt
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
now i using but still the same...
and then my data.txt is 0123456789123...
|
|
|
|
|
Oh man! check your logic. You get input from file in an array. Since you have got all the data from there, file pointer has reached to end of the file. Now when you go in loop, condition is true. So nothing will be printed in output file.
So if you want to get correct output of your code, let us know what exectly you want from your code to do in while loop.
Good luck.
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
does your file contains 14 charectors only or more...
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
|
How will the "fin" file ever reach the end when you're only reading from it once! Surely you need to move the fin.getline(str,sizeof(str),'\n'); bit into the while loop.
Steve
|
|
|
|
|
In fact you should get input from data.txt within your loop. As you are not getting input in loop, your file pointer in data.txt in not moving. So there will be infinite loop.
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
yeah..how can i stop the loop once i got all the possible shuffling of numbers.
|
|
|
|
|
so did you get your problem solved?
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
yeah thank u..it continues long way more...bcause i ve to end up this with GUI interface....just took a challenge to learn C++ in 2 weeks...till ca MFC part...n i m learning alot..thankz..for who r kind hearted to help!
|
|
|
|
|
makaveli_07 wrote: just took a challenge to learn C++ in 2 weeks
2 weeks... It's a joke, isn't it ?
And, if you really want to learn something, it is not by doing cut and paste without trying to understand that you will learn something. Really, if you want to learn the language, follow my advice: buy yourself a good book and start from the begining (and do the things yourself and THINK by yourself). It's not because you made a program (with the help of people here) that you can program.
Learning a programming language is like learning a new language and also a new way of thinking (if it's the first language that you learn). What will you think if I tell you that I want to learn Japanese in two weeks.
|
|
|
|
|
now everythin is okie but...onli one line of digit output in the file..i need all the possible randomized shuffling..
<code>#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
time_t tmStart, tmEnd;
void DoStuff()
{
tmStart = time(0);
ifstream fin("data.txt",ios::in);
ofstream File("MyFile.txt",ios::out);
char str[14];
// fin.getline(str,sizeof(str),'\n');
// string line(str);
//cout << str<<endl;
int i=0;
// while(fin.eof() == NULL )
while(true)
{
if(fin.getline(str,sizeof(str),'\n'))
{
string line(str);
random_shuffle(line.begin(), line.end());
File << "Randomised: " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " : " << line << endl;
File << " "<< endl;
// File << "Single Row: " << s << " " << s << " " << s <<" " << s << endl;
File << " "<< endl;
++i;
//continue;
tmEnd = time(0);
cout << tmEnd-tmStart << " seconds " << endl;
}else break;
}
File.close ();
fin.close ();
}
int main(int argc, char* argv[])
{
DoStuff();
return 0;
}
</code>
|
|
|
|
|
I've been very patient and even defended you for asking so many questions in the face of other CPians suggesting that you're not even trying and that in fact all you're really trying to do is get other people to do your work for you. Was I wrong? Look how quickly you ask another question after the never ending loop problem was solved for you. It seems impossible that you’ve given the problem any serious thought.
Steve
|
|
|
|