|
Oh, also I typed into my .cpp file as you wrote:
double[][] vl_reg3 = {vl_reg12, vl_reg14, vl_reg16, vl_reg18, vl_reg20, vl_reg22, vl_reg23, vl_reg24, vl_reg25};
At first set of brackets: Error: expected an identifier
At second set of brackets: Error: an array may not have elements of this type
At text vl_reg3: Error: expectd a ';'
for some reason it does not like it that way. I am not sure why.
|
|
|
|
|
Sorry, thought that would work. Doesn't work in C# either. I don't use double array setups too often. (I do use multidimentional arrays -> [n, m]) You can declare double[][], but I don't know how to instantiate it.
|
|
|
|
|
Hi,
jharn wrote: This is the VBA code I was trying to translate from, it is a bit cleaner since I am a bit more familiar with it. Just posting so you can see what I was trying to do.
Indeed C++ and VBA are totally different beasts . The following should do the same, plus parameter validation, in modern C++ (requires std::tr1 coming with VC2010, VC2008 or gcc 4.5):
#include <string>
using std::string;
#include <array>
using std::array;
#include <stdexcept>
using std::invalid_argument;
typedef array<const string, 15> Names;
Names names =
{
"70-30 Cu-Ni", "90-10 Cu-Ni", "Admiralty Metal", "Aluminum Brass",
"Aluminum Bronze", "Arsenical Copper", "Cold-Rolled Low Carbon Steel", "Copper Iron 194 (Olin 194)",
"Titanium Grades 1 & 2", "304 SS", "316/317 SS", "N08367 (AL6XN)",
"S43035 (TP439)", "S44660 (Sea-Cure)", "S44735 (AL29-4C)"
};
typedef array<const double, 15> SaturationPressure;
const SaturationPressure vl_reg12 = {0.71, 0.8, 0.93, 0.92, 0.89, 0.98, 0.81, 1, 0.64, 0.54, 0.53, 0.48, 0.63, 0.58, 0.58};
const SaturationPressure vl_reg14 = {0.78, 0.85, 0.96, 0.95, 0.93, 1, 0.86, 1.01, 0.71, 0.62, 0.61, 0.56, 0.71, 0.66, 0.66};
const SaturationPressure vl_reg16 = {0.83, 0.89, 0.98, 0.97, 0.96, 1.01, 0.9, 1.02, 0.77, 0.69, 0.67, 0.63, 0.77, 0.72, 0.72};
const SaturationPressure vl_reg18 = {0.88, 0.93, 1, 0.99, 0.98, 1.02, 0.94, 1.03, 0.83, 0.75, 0.74, 0.7, 0.82, 0.79, 0.79};
const SaturationPressure vl_reg20 = {0.92, 0.96, 1.01, 1.01, 1, 1.03, 0.97, 1.03, 0.89, 0.82, 0.81, 0.78, 0.88, 0.85, 0.85};
const SaturationPressure vl_reg22 = {0.95, 0.98, 1.02, 1.02, 1.01, 1.03, 0.98, 1.04, 0.91, 0.86, 0.85, 0.82, 0.91, 0.89, 0.89};
const SaturationPressure vl_reg23 = {0.96, 0.99, 1.02, 1.02, 1.01, 1.04, 0.99, 1.04, 0.93, 0.88, 0.87, 0.84, 0.92, 0.9, 0.9};
const SaturationPressure vl_reg24 = {0.97, 0.99, 1.03, 1.02, 1.02, 1.04, 1, 1.04, 0.94, 0.9, 0.89, 0.86, 0.94, 0.92, 0.92};
const SaturationPressure vl_reg25 = {0.97, 1, 1.03, 1.03, 1.02, 1.04, 1, 1.04, 0.95, 0.91, 0.9, 0.88, 0.95, 0.93, 0.93};
typedef array<const SaturationPressure, 9> Pressures;
Pressures pressures = {
vl_reg12, vl_reg14, vl_reg16, vl_reg18, vl_reg20, vl_reg22, vl_reg23, vl_reg24, vl_reg25};
typedef array<int, 9> Temperatures;
Temperatures temp = {12, 14, 16, 18, 20, 22, 23, 24, 25};
double HEI_F2(const string& name, int temperature)
{
size_t iName = distance(names.begin(), find(names.begin(), names.end(), name));
if (iName == names.size())
throw invalid_argument("Invalid name");
size_t iTemp = distance(temp.begin(), find(temp.begin(), temp.end(), temperature));
if (iTemp == temp.size())
throw invalid_argument("Invalid temperature");
return pressures[iTemp][iName];
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
try
{
cout << HEI_F2("Admiralty Metal", 22) << endl;
cout << HEI_F2("Cold-Rolled Low Carbon Steel", 22) << endl;
cout << HEI_F2("Aluminum Bronze", 18) << endl;
cout << HEI_F2("Aluminum Bronze", 30) << endl;
}
catch (invalid_argument& err)
{
cout << err.what() << endl;
}
return 0;
} cheers,
AR
Code edited to correctly match OP's intent.
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Thursday, February 3, 2011 4:43 AM
|
|
|
|
|
All I can say is:
1. Wow, thanks....!!!!
2. I have so much to learn about C++, I do have a book from when I took a course in college, guess I better drag it back out and start studying.
3. You guys on the list are great, I look forward so much to learning from all of you.
Sincerely,
Joe
|
|
|
|
|
|
I edited my code[^] which misinterpreted your input. Now simpler
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
Thanks so much for your time and attention. I should note that I mislabeled my code. This is actually the tube material
"70-30 Cu-Ni", "90-10 Cu-Ni", "Admiralty Metal", "Aluminum Brass",
"Aluminum Bronze", "Arsenical Copper", "Cold-Rolled Low Carbon Steel", "Copper Iron 194 (Olin 194)",
"Titanium Grades 1 & 2", "304 SS", "316/317 SS", "N08367 (AL6XN)",
"S43035 (TP439)", "S44660 (Sea-Cure)", "S44735 (AL29-4C)"
versus the tube gauge {12, 14, 16, 18, 20, 22, 23, 24, 25};
Which when matched up in x, y matrix returns the HEI gauge correction factor.
On another note, I am tying to pass the tube gauge (int) and the tube material (string) from a VBA function:
Declare Function TSx Lib "C:/cnd_perf.dll" (arg1 As Integer, arg2 As String) As Double
and return the factor found in the array values you showed in your code (should be a double or a long) back to Excel, (and maybe later in another function return the tube material and gauge also.)
Problem is I can pass the VBA string to C++, but have had no success in checking it against the tube material values as shown above so I can get the correct lookup value between the material and the gauge.
I have tried char (with strcmp), BSTR, and std::string, but I am still not quite there
|
|
|
|
|
Hi Joe,
jharn wrote: I mislabeled my code. This is actually the tube material
"70-30 Cu-Ni", "90-10 Cu-Ni", "Admiralty Metal", "Aluminum Brass",
"Aluminum Bronze", "Arsenical Copper", "Cold-Rolled Low Carbon Steel", "Copper Iron 194 (Olin 194)",
"Titanium Grades 1 & 2", "304 SS", "316/317 SS", "N08367 (AL6XN)",
"S43035 (TP439)", "S44660 (Sea-Cure)", "S44735 (AL29-4C)"
versus the tube gauge {12, 14, 16, 18, 20, 22, 23, 24, 25};
Which when matched up in x, y matrix returns the HEI gauge correction factor.
Change the names to relevant ones
jharn wrote: Problem is I can pass the VBA string to C++, but have had no success in checking it against the tube material values as shown above
This is a really different issue. I suppose VBA uses Unicode, so what is transmitted to your dll is a BSTR or a wchar_t* . First investigate around that. If you don't solve post in the Questions and Answers area with VBA C++ dll tags;
cheers,
AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
There have been many rookie errors pointed out by others, so I won't repeat them. There are a few general things that will help with your code:
1. Compile the code with the strictest warnings setting. The compiler will likely tell you about a lot of bugs, or potential bugs, lurking in the code. Do not use the code until there are no warnings.
2. Use a static code analyzer like Lint. It would have caught problems like your assign/compare bug. It will also offer many suggestions for cleaner code.
See http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis[^]
When you can compile code with no warnings, and get through Lint without much trouble, you are much less likely to have a real problem with your code. You'll be able to concentrate on the real meat of the code.
Stuart
|
|
|
|
|
Silly question -- in whatever language your working in, is there no find command?
If not, write one....you're doing the same action 3+ times. Take an value and an array and search through the array looking for the element. Return -1 if not found or the index value if it is found.
|
|
|
|
|
This code is not C. It could be C++ or Java. A string in C IS a character ARRAY and is not declared as string but as char[string length+1].
Try:
if (arg1.equal(a))
{
tp = counter;
}
|
|
|
|
|
Content of a.txt
Hello How are you
Welcome
Function used to read the File
void ReadBytes(__inout BYTE *pBuffer, DWORD dwSize)
{
DWORD dwRead;
if (hFile != INVALID_HANDLE_VALUE)
ReadFile(hFile,pBuffer,dwSize,&dwRead,NULL);
}
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[26];
mObj.ReadBytes(hSrc,26);
std::cout<<hSrc;
return 0;
}
The output of the above function gives.
Hello How are you
Welcomeýýýý««««««««îþ
Why this extra characters at the end? please help
Some Day I Will Prove MySelf :: GOLD
modified on Tuesday, February 1, 2011 12:44 AM
|
|
|
|
|
goldenrose9 wrote: Why this extra characters at the end?
Because cout will output characters until it reaches a \0 .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
i had checked this with file mapping. In file mapping when i use cout to display the hSrc value it gives the correct output.
then why it is displaying extra characters when i am using ReadFile method. Is there any problem in my code. please help
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
goldenrose9 wrote: ...then why it is displaying extra characters when i am using ReadFile method.
It has nothing to do with ReadFile() .
goldenrose9 wrote: Is there any problem in my code.
Yes, hSrc is not terminated properly.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
then how to solve this.
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
You forgot to terminate the buffer with the null-termination character:
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[27];
mObj.ReadBytes(hSrc,26);
hSrc[26] = '\0';
std::cout<<hSrc;
return 0;
}
|
|
|
|
|
thanx
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
Cedric Moonen wrote: BYTE* hSrc = new BYTE[27];
here you have initialized the byte array with +1 of the required value that is 26 and null terminated the byte array at 26.
A small doubt. do i have to allocated the byte array +1
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
Yes, you have to take the termination character into account (it still counts as one char and should be allocated too). Remember that if I allocate an array of 27 characters, the last one is the character at index 26 (since the array is zero-based).
|
|
|
|
|
got it.
sir, now the program is working fine. but when i pass a bmp or exe file to this function only few bytes from the starting is read into the buffer.
Zip Archive
PK
EXE
MZ
BMP File
BM:
am i missing something again.please help..
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
Are you trying to print those files to the console ?
This won't work, since they contain binary information which can perfectly be the character with ASCII code 0 (which will be considered as the end of the string). What are you trying to do here ?
|
|
|
|
|
Hi,
Using C++, std::fstream, Windows.
I have an algorithm that processes large file (actually this is the series of algorithms, but lets consider only one, cause others having the same problems).
In general this algorithm reads chunks of data from one file and write to the other file with during some realignment (For reference, I'm doing realignment of the 3d volume data). I'm reading chunk size is 512 bytes, writing chunk size is 16 kb.
Usually this algorithm finishes in 1 minute and 50 seconds. But I noticed that sometimes (rarely) it finishes in 24 seconds! Processing the same file, the same execution path. I've started to search for the reasons of why this slowdown is happening and how can I control that.
1) I have tried increasing the coalescing of the accesses to disk
2) I have considered the fragmentation problem (the file that I write is wrote in small chunks, therefore it's fragmented, about 600 fragments). When I resolved fragmentation problem (so, it's guaranteed that file is not fragmented) - I didnt got anything, still this chaotic access speed.
3) I have investigated the probability, that Windows flashes my memory buffers to HDD. No, that's not the case.
4) I have found that if I do all this operation on the other physical disk (not the one with OS) - I get this slow down more rarely, and algorithm usually finishes in 40 seconds (but anyway, speed is of HDD access is chaotic).
Frequently during the same execution, access speed is rising or falling down, may be few times.
This looks like my accesses are going out of tact with some internal HDD or OS operations, don't know.
Anyone, have some experience or idea?
Thanks.
|
|
|
|
|
If you read and write data from the same disk from different files, the read-head will have to move to a different place on the disk, which is slow. If your input and output file are on different drives, this problem is solved. If you read and write alternatively (to the same physical disk) in your program each time, this slow down is maximum.
Note that some variation in disk speed can't be prevented in a multitasking OS where other programs might be using the disk too.
If your memory permits, you could consider reading the entire input file to memory at once at the beginning of your algorithm and then processing it and writing the results.
modified 13-Sep-18 21:01pm.
|
|
|
|
|