Choosing a variable based on user text input.

This is a discussion on Choosing a variable based on user text input. within the C++ Programming forums, part of the General Programming Boards category; I have an assignment to write a simple interpreter for a made-up programming language, the code for which will be ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Choosing a variable based on user text input.

    I have an assignment to write a simple interpreter for a made-up programming language, the code for which will be passed in a text file; for example,

    INC(A)

    would perform:

    A++;

    (the variable A is predefined). I know how to do most of it, but one part has stumped me: how do I tell C++ that it has to incriment the variable A? I could do it using an if or a case statement like:
    Code:
    void increment(char varName) {
        if(varName ==  'A') {
            A++;
        }
        if(varName ==  'B') {
            B++;
        }
    }
    but that gets ugly fast, considering there are five "functions" and ten variables. Any alternative ideas? I'm not looking for someone to do the work for me, but a hint on how to get C++ to pass the correct variable based on text input would be awesome. Thanks in advance.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well you could do a function for it like this:
    Code:
    int increment(int iVar)
    {
    return iVar++;
    }
    Then for whatever variable you need to increment you could pass it like this:
    Code:
    A=increment(A);
    //or
    B=increment(B);
    Is that what you're looking for, or did I totally miss the point?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Not quite. Here's what I mean.

    let's say A and B are global variables and accessible from anywhere in the program.

    Code:
    int main() {
        char buffer = new char;
        A = 0;
        B = 0;
        cin >> buffer;
        increment(?);
    }
    Now, based on whether A or B was entered into buffer, I have to pass the variable A or the variable B to the incriment method. Is there any way to do it other than an if statement?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You could use an associative array, which contains name-value pairs, like this:

    "apples" : 5
    "bananas": 8
    "pears": 11

    where the strings are the index values. It works something like this:

    string varName = "apple";
    myArray[varName]++; // 6

    The value associated with the string "apple" is retrieved from the array.

    To create an associative array in C++ requires using the Standard Template Library(STL). A <map> is what you want. Here is an example:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
    		
    	map<string, int> myAssocArray;
    
    	myAssocArray["A"] = 10;
    	myAssocArray["number"] = 30;
    	myAssocArray["strawberry"] = 2;
    
    	myAssocArray["A"]++;
    
    	cout<<myAssocArray["A"]<<endl;
    
    	return 0;	
    }
    You have to be careful using the operator[] with maps: it will insert the index into the array if it doesn't already exist, and in this case give it a default int value of 0.
    Last edited by 7stud; 10-31-2005 at 10:28 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    Thanks a lot. I modified it, and it seems to work:

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
    	string info = "A";
    		
    	map<string, int> myAssocArray;
    
    	myAssocArray["A"] = 10;
    	myAssocArray["number"] = 30;
    	myAssocArray["strawberry"] = 2;
    
    	myAssocArray["A"]++;
    
    	cout<<myAssocArray["A"]<<endl;
    
    	cout<<myAssocArray[info]<<endl;
    
    	return 0;	
    }
    The one problem is that it throws a ton of warnings (not errors, though) when compiled in Visual C++ 6.0 (I attatched a text file with them). Is there any way to disable warning display? It's making it hard to find the errors.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I was going to say something about that but I forgot. If I remember correctly, those warnings are caused because VC++6 can't handle long variable names and the STL uses variable names that are too long. Someone once directed me to some add on that stiffles the warnings, but it doesn't work completely.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    See here for a fix(look under the heading Standard Template Library):

    http://www.msoe.edu/eecs/cese/resour...vcmfc/msvc.htm

  8. #8
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    msoe id go there, but its 8 grand a quarter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  2. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 05:21 PM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21