|
Sorry, I wasn't clear....
in the part where I compare if (arg1 = a) it tells me expression must have a bool type.
|
|
|
|
|
jharn wrote: in the part where I compare if (arg1 = a) it tells me expression must have a bool type.
Right, you are confusing == with = .
"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
|
|
|
|
|
Hi, I tried arg1==a, now it says that no operator "==" matches these operands.
|
|
|
|
|
I think you need to use the 'compare' method. It returns 0 if they're equal, a positive int the string is greater, or a negative int if the string is less. Therefore:
if (arg1.compare(a) == 0)
|
|
|
|
|
Is that not an AND function? ==
I guess I am confused.....
|
|
|
|
|
jharn wrote: Is that not an AND function? ==
Uh, no. That is the equality operator.
The two AND operators are bitwise (& ) and logical (&& ).
"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
|
|
|
|
|
hahahaha......you are SO right, my apologies
|
|
|
|
|
Hi, I tried arg1==a, now it says that no operator "==" matches these operands.
|
|
|
|
|
You are trying to compare two strings. The == operator just compares simple scalar values, not arrays. I'm not a C++ / C# programmer, but if it was C, I'd suggest using the strcmp function - there must be a C++ / C# equivalent. If it was Java, you'd use the equals() method.
|
|
|
|
|
jsc42 wrote: You are trying to compare two strings. The == operator just compares simple scalar values, not arrays.
Neither arg1 nor a is an array. They are both string objects, which has this.
"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 apologise! I had forgotten that C++ allows operator overloading. 
|
|
|
|
|
You declare your arrays to have 16 elements, but only initialize 15, then access the 17:th element.
string vl_reg1[16] = { };
do{
a = vl_reg1[counter];
counter = counter + 1;
} while (counter <= 16)
It's a mess.
|
|
|
|
|
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
|
|
|
|
|