|
OK, I see, sorry to bother, I know it usually is not much fun helping a newbie, but I really do appreciate your guidance.
|
|
|
|
|
First off, your code looks like it is supposed to be C++, I'm not an expert on this language. I'm going to assume it is something else because I don't see how this would compile.
I agree with the assessment that the code is a mess no matter what language.
The reason why you aren't blowing up with string vl_reg1[16] = {...} is because the variable is first defined with 16 values in an array and then the {} acts like you said
vl_reg1 = new string[15]
, followed by a loop that fills in 15 values into the new array. Like I said, I'm not an expert. That's what it looks like it is doing. As far as I know C++ doesn't have string as a field. The "new" is probably a mistake on my part because that works great in C#, but entails a huge amount of memory management headaches with C++. (Unless the .Net version of C++ fixes that problem as well.) Anyway in C# you can dynamically change the size of an array if you wish. In C# I would probably say string[] vl_reg1 = {...}
I'm assuming your language supports ubounds(). From your error messages, maybe your language doesn't support "=="?
Change your first while to
}while (counter <= vl_reg1.ubounds(0));
In fact, change your whole do loop to:
do{
a = vl_reg1[counter];
tp = counter++;
}while (counter <= vl_reg1.ubounds(0) && arg1 != a);
You don't have any error checking. Immediately after that execute
if (arg1 != a) return -1.;
You have return vl_reg3[bg][tp];
I don't understand how that would even compile. I know what you are trying to do, but this WON'T do it! (At least in any of the languages I know.) vl_reg3 is a string type single array. You are returning a double value from the routine. You can't change types. All of a sudden you are returning a double array setup from a single array string type. No, it won't automagically determine the string field vl_reg3[bg] needs to be converted to a double type array field and pass back that array's number from the tp location. (Which makes no sense either, but that's what it looks like you are trying to do.
Forget the second loop it is total nonsense. vl_reg2 should be a pointer to the array values you want to use, so keep the numbering scheme you've put into it. These numbers should be a 1 to 1 correllation to the positions of the first argument found in vl_reg1.
Set up a new double array. (call it, say "ret_vals")
Instead of the second array, do some more error checking
if (arg2 < 0) return -2.;// This could be the first line of the code if you want.
if (tp > vl_reg1.ubounds(0)) return -3.;
// You passed a valid array value in the first argument but the second isn't defined yet.
b = vl_reg1[tp];
// There isn't a do loop that will do this, write a line for each array you want to use.
if (b == 12) ret_vals = vl_reg12;
// reassigns the ret_vals array to point to the array of values you want to use.
if (ret_vals == null) return -4.;
// you just screwed up the if tests you were supposed to write
if (arg2 > ret_vals.ubounds(0)) return -5.;
// you just passed too big an argument for the return location in the array values you want to use.
return ret_vals[arg2];
|
|
|
|
|
All, thank you very much for your help and your constructive criticism. I do realize I am a complete newbie and I hope I can improve my programming skills. I do know that the last line of the code would not work (vl_reg3[bg][tp]).....it was written ion visual basic and I was in the process of converting it to C++.
I have since found out that the function in C++ is not receiving the text string I am trying to send form a VBA Declare function, so I guess I need to figure that out first.
All, I know I am not a good programmer, but I ma very persistent so I will keep trying to learn and I hope all of you are not offended by my less than intelligent coding and questions.
|
|
|
|
|
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.
Function HEI_F2(ByVal typ As Variant, bwg As Integer) As Double 'saturation pressure inHg from Temp
Dim vl_reg1 As Variant, vl_reg2 As Variant, vl_reg3 As Variant, vl_reg12 As Variant
Dim vl_reg14 As Variant, vl_reg16 As Variant, vl_reg18 As Variant, vl_reg20 As Variant
Dim vl_reg22 As Variant, vl_reg23 As Variant, vl_reg24 As Variant, vl_reg25 As Variant
Dim tp As Integer
vl_reg1 = Array("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)")
vl_reg2 = Array(12, 14, 16, 18, 20, 22, 23, 24, 25)
vl_reg12 = Array(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)
vl_reg14 = Array(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)
vl_reg16 = Array(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)
vl_reg18 = Array(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)
vl_reg20 = Array(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)
vl_reg22 = Array(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)
vl_reg23 = Array(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)
vl_reg24 = Array(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)
vl_reg25 = Array(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)
vl_reg3 = Array(vl_reg12, vl_reg14, vl_reg16, vl_reg18, vl_reg20, vl_reg22, vl_reg23, vl_reg24, vl_reg25)
counter = 0
Do While counter <= UBound(vl_reg1)
If typ = vl_reg1(counter) Then
tp = counter
Exit Do
End If
counter = counter + 1
Loop
counter = 0
Do While counter <= UBound(vl_reg2)
If bwg = vl_reg2(counter) Then
bg = counter
Exit Do
End If
counter = counter + 1
Loop
HEI_F2 = (vl_reg3(bg)(tp))
End Function
|
|
|
|
|
Going from vba to a strongly typed language explains a lot. The main complaint I have with your original vba version is that you expect the caller to be reasonable and accurate. I find it best to assume the caller is the exact opposite until proven otherwise. That is best even if the caller is yourself. Set bg and tp to -1 before starting the loop and check to make sure both are >= 0 before setting the double value.
You can set up a
double[][] vl_reg3 = {vl_reg12, vl_reg14, vl_reg16, vl_reg18, vl_reg20, vl_reg22, vl_reg23, vl_reg24, vl_reg25};
and use the same index formats in the new language as you did in the old vba code.
Don't appologize for being stubborn, that's a normal trait for a programmer. You have to be in order to be beaten down over and over again by the code you write and getting up and trying again. Just dial back the stubbornness when meeting other humans and listen. (That's something I have to watch out for.)
|
|
|
|
|
KP, thanks for the help and the advice. I am persistent and stubborn, but when it comes to listening to the people on the list who know better I am all ears. You are right about the error checking for caller mistakes, I really should do more of that.
I will try your multi-array as you suggested. I am still having a bit of trouble passing a string from VBA Declare statement to the C++ function correctly and checking if the string matches existing strings already in the C++ function. I think I am getting hung up on the ASCII/Unicode difference.
I will keep toiling away. Again, thank you very much.
|
|
|
|
|
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
|
|
|
|
|