Click here to Skip to main content
15,892,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: String array comparison Pin
KP Lee1-Feb-11 2:22
KP Lee1-Feb-11 2:22 
GeneralRe: String array comparison Pin
jharn1-Feb-11 4:39
jharn1-Feb-11 4:39 
GeneralRe: String array comparison Pin
jharn1-Feb-11 5:04
jharn1-Feb-11 5:04 
GeneralRe: String array comparison Pin
KP Lee1-Feb-11 10:18
KP Lee1-Feb-11 10:18 
GeneralRe: String array comparison Pin
jharn1-Feb-11 10:40
jharn1-Feb-11 10:40 
GeneralRe: String array comparison Pin
jharn1-Feb-11 10:46
jharn1-Feb-11 10:46 
GeneralRe: String array comparison Pin
KP Lee1-Feb-11 14:00
KP Lee1-Feb-11 14:00 
AnswerRe: String array comparison [modified] Pin
Alain Rist2-Feb-11 7:07
Alain Rist2-Feb-11 7:07 
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 Smile | :) . 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; // saturation pressure in Hg from Names at various temperatures
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; // 1.02
		cout << HEI_F2("Cold-Rolled Low Carbon Steel", 22) << endl; // 0.98
		cout << HEI_F2("Aluminum Bronze", 18) << endl; // 0.98
		cout << HEI_F2("Aluminum Bronze", 30) << endl; // throws Invalid temperature
	}
	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

GeneralRe: String array comparison Pin
jharn2-Feb-11 14:45
jharn2-Feb-11 14:45 
GeneralRe: String array comparison Pin
Alain Rist2-Feb-11 20:30
Alain Rist2-Feb-11 20:30 
GeneralRe: String array comparison Pin
Alain Rist2-Feb-11 22:54
Alain Rist2-Feb-11 22:54 
GeneralRe: String array comparison Pin
jharn3-Feb-11 5:46
jharn3-Feb-11 5:46 
GeneralRe: String array comparison Pin
Alain Rist3-Feb-11 9:39
Alain Rist3-Feb-11 9:39 
AnswerRe: String array comparison Pin
Stuart Rubin1-Feb-11 3:22
Stuart Rubin1-Feb-11 3:22 
AnswerRe: String array comparison Pin
thomas.michaud1-Feb-11 5:57
thomas.michaud1-Feb-11 5:57 
AnswerRe: String array comparison Pin
MarvinMartian1-Feb-11 13:47
MarvinMartian1-Feb-11 13:47 
QuestionReadFile() Problem Pin
goldenrose931-Jan-11 4:12
goldenrose931-Jan-11 4:12 
AnswerRe: ReadFile() Problem Pin
David Crow31-Jan-11 4:18
David Crow31-Jan-11 4:18 
GeneralRe: ReadFile() Problem Pin
goldenrose931-Jan-11 4:32
goldenrose931-Jan-11 4:32 
AnswerRe: ReadFile() Problem Pin
David Crow31-Jan-11 4:44
David Crow31-Jan-11 4:44 
GeneralRe: ReadFile() Problem Pin
goldenrose931-Jan-11 4:51
goldenrose931-Jan-11 4:51 
AnswerRe: ReadFile() Problem Pin
David Crow31-Jan-11 4:55
David Crow31-Jan-11 4:55 
AnswerRe: ReadFile() Problem Pin
Cedric Moonen31-Jan-11 4:47
Cedric Moonen31-Jan-11 4:47 
GeneralRe: ReadFile() Problem Pin
goldenrose931-Jan-11 6:01
goldenrose931-Jan-11 6:01 
GeneralRe: ReadFile() Problem Pin
goldenrose931-Jan-11 18:43
goldenrose931-Jan-11 18:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.