How to use input string, to call variable name in c++ - variables

so I just have what seems like a rudimentary problem.
But for some reason I can't wrap my mind around it.
So given the code:
int Red = 1;
int Blue = 2;
int Green = 3;
main (){
cout << "Enter your keyword";
cin >> str1;
If the user inputs the word Red, i want it to return the value of the variable "Red", without using an if statement for every variable (I have 200+ variables).
To be more specific:
If the user inputs Red, I want my program to read the first item in my array. To do that I need red to represent a number value.
Thanks in advance for any help.

Updated: Question misunderstood.
You cannot ref to a variable based on a string value.
But you can setup a list of structure who contain a combination of key and value. You should look at map

Related

Comparator and Arraylist - sorting words

I am working on a scrabble assignment. We have to assign values to each word and then put them in order of point value. We will read the words from a text file. So I'm thinking to create an ArrayList to store each word. My question is, how do I read each character and assign a value to it? I believe I will use a for loop. I'm just confused when I start working with char... I think that's what this will be.
Once I figure out how to give a score to each word, do I then use the comparator interface to sort them?
EDIT: I realized maybe I didn't make this clear. Each letter is given a value from the Scrabble game. So I have to read each letter of each word and assign a value that will add up to a total score for the word. Like the word QUIT would be 10 + 1 + 1 + 1 = 13.
What you could do is to add all words as keys in a hash table, initiating them all with a value of 0. Then you loop through the keys and use a for loop to check the value of each character of each word, adding the word’s sum value as the value for that word’s key in the hash table.
The for loop for each word would look something like this:
int sumValue = 0;
for(int i =0; i < word.length(); i++)}
sumValue += value(word.charAt(i))
}
The return value() is just my way to represent that you return the value of the specific character you are at. Of course this depends on which character it is. Maybe it would be wise to keep another hash table with each character associated with a scrabble value that you access in the loop (like A:1, X:8 etc).

How do you write Lua code for an input request and then outputs a result from a lookup

I would like some assistance on writing some Lua code in which if a user inputs a color for a car the code will be able to search a lookup table to see if that color is included in the lookup table as a keyword and will be able to output a score which defines the result for example 1 = accept, 2 = decline deepening on what color car they have typed at the beginning.
Thank you
This is a rather trivial program in lua. because lua allows for simple creation of associative arrays(a.k.a. hash tables) you can quickly make a lookup table.
local carColors = {
purple = "1"
}
From there you index the table with your user input and return your 1 or 2
local userInput = io.read():lower() --Make sure to set the user input to all lowercase.
print(carColors[userInput] or "2") -- if nil return 2
I used print rather than io.output.
the or here lets the code handle when the user gives a bad color name when carColors[userInput] is nil the 2 will be printed.

How to use Get command in Monkey talk?

Does anybody know how to use the Get command in monkey talk?
In monkey talk guide only the command is written but no syntax is present.
Here is an example for you,
var x = app.label("label1").get("x","value")
Above line will get the value of label1 and store it in variable x. You can change the second parameter "value" to ".text" or ".size" depending on your needs
since the question is not marked Answered and For the future reference i am answering this.
‘GET’ ACTION RETRIEVES COMPONENT PROPERTY VALUES
Syntax:
ComponenType MonkeyId Get varName propName
Works like an assignment statement (varName= propName)
The default property is “value”
// Get label value currently displayed
Label lastname Get name
// Enter name into input field
Input query EnterText ${name}
// Get the first table item
Table countries Get country items1
// Enter into input field
Input * EnterText ${country}
you can refer this document here, thanqs
I will try to explain using example. Suppose there is a ListView(Table) and you want to scroll till its last item.
//Define a variable
Vars * Define row
//Store size of list in variable row using Get. Check use of "Get" here
Table * Get row size %thinktime=20000
//Now stored variable can be used as per need. here I am using for scrolling
Table * ScrollToRow ${row} %thinktime=2000
Hope it helps you !!

How can I get the name of a node?

Is there a way to get the name of a node? For example:
Fruits:
- apple
- orange
- pear
and in C++:
YAML::Node fruit = parser["Fruits"];
cout << fruit.name() << endl; // should print "Fruits"
is there something like YAML::Node::name()? I don't see anything in Node.h that fits the bill.
If there isn't, any suggestions on a simple way to modify the code to record this information?
Thanks!
What you're really looking for is the key associated with a value in a map. You're right, there's no link back from a value to its key, but you can store one, when you're navigating the node in the first place.
If all of your keys are string keys, then whenever you pass a value to some function, just pass along the string key as well:
// Instead of:
doSomething(node[key]);
// Call:
doSomething(key, node[key]);
Fruits:
- apple
- orange
- pear
This is one key-value-pair
"Fruits" is the key
" -apple
-orange
-pear " is the value
YAML::Node fruit = parser["Fruits"];
This command use one key to query the value
So the node "fruit" just keep information about value.

Store an NSString as a fixed length integer?

having a bit of trouble finding a solution to this.
I want to take a large ordered text file of words and create - in the same order - a text file of fixed length numeric values.
For example:
Input File Output File
AAA -> 00000001
AAH -> 00002718
AAZ -> 71827651
Initially it seemed a hash function would do the trick. However they are one way. Also perhaps they are a bit "heavyweight" for this. After all, I don't need any cryptography. Plus, it's a reference file. It will never change.
Any compression is a bonus not essential. That said, I don't want the file to get any bigger than it already is. Which is why I don't just want to write out the words as text but with fixed lengths.
So, bottom line; input is a NSString of variable length, output is an integer of fixed length. And, I must be able to take the integer and figure out the string.
Any help much appreciated!
Thanks!
xj
Well, this would be a bit of a brute force method, but here's my guess.
Start by making a custom function to convert one letter of text to an integer less than 100. (I'm not sure if such a function already exists, if so then great!) You might need to just go to stuff like "if ([input isEqual: #"a"]){ return 1;}
Then, run that function on each letter of text, and get the final integer by combining the previous results.
For example:
int myVal1 = [intConverter firstLetter];
int myVal2 = [intConverter secondLetter];
int myVal3 = [intConverter thirdLetter];
int finalValue =100^3 + 100^2*myVal1 + 100*myVal2 + myVal3;
Then, finalValue would be of the form 1(myVal1)(myVal2)(myVal3), which is what I think you're looking for.
To get back the original string, simply use the mod (%) and division functions to get the individual values back, then run the intConverter function backwards. (This would probably mean writing a new function that basically runs those if statements in reverse, but oh well.)
I hope this helps.