Arrays: Passing Index of String Array to Index of Int Array for Output only

This is a discussion on Arrays: Passing Index of String Array to Index of Int Array for Output only within the C++ Programming forums, part of the General Programming Boards category; Hello, I am looking to pass the int values of an array to the indices of a string array "behind ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Arrays: Passing Index of String Array to Index of Int Array for Output only

    Hello, I am looking to pass the int values of an array to the indices of a string array "behind the scenes." I want the string arrays to be ouput with their strings, however I want to do math on the indices the strings represent.
    In other words I would like to combine or blend them together so that I have 4 arrays: 2 of them are int arrays, 1 of the int arrays is for doing math on; and 2 of them are string arrays, both of them are for display to the screen purposes only.
    If I try to loop through the int arrays and assign the string array elements to their corresponding indices, it won't compile, and I get the error message:
    "Cannot convert 'std::string' to 'int' in assignment."
    If I try to loop through the string arrays and assign them the int array values to their corresponding indices, it compiles, however I get the ASCII representation of the int values, rather than the numbers.
    Here is the code for looping through the int arrays:
    Code:
    #include <iostream>	
    
    using namespace std;
    	
    int main()			
    {	
        
        int typeArray[4] = {55,66,77,88};
        int valArray[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        
        string types[4] = {"Manny", "Moe", "Jack", "John" };
        string values[13] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
                              "Eleven","Twelve","Thirteen"};
                              
        for (int i = 0; i < sizeof(typeArray)/sizeof(int); i++)
          {
            for (int j = 0; j < sizeof(valArray)/sizeof(int); j++)
            {
                    
                typeArray[i] = types[0];
                valArray[j] = values[0];
                }
          }
    
          system("Pause");
          return 0; 
    }
    AGAIN, IT WON'T COMPILE, AND I GET THE ERROR MESSAGE NOTED ABOVE.
    Here is the code for looping through the string arrays and the output:
    Code:
    #include <iostream>	
    
    using namespace std;
    	
    int main()			
    {	
        
        int typeArray[4] = {55,66,77,88};
        int valArray[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        
        string types[4] = {"Manny", "Moe", "Jack", "John" };
        string values[13] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
                              "Eleven","Twelve","Thirteen"};
    
    
    
        
            for (int i = 0; i < sizeof(types)/sizeof(int); i++)
             {
            for (int j = 0; j < sizeof(values)/sizeof(int); j++)
              {
                    
                types[i] = typeArray[0];
                values[j] = valArray[0];
                
                cout << "This is types array: " << types[i] << "  This is values array: " << values[j] << endl;
              }
           }
    
          system("Pause");
          return 0;
    }
    ------------------------
    HERE IS THE OUTPUT SHOWING ASCII CONVERSIONS OF THE INTS:
    This is types array: 7 This is values array: ☺
    --------------------

    Please advise, thanks!

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Hi! It looks to me as if you're just rearranging indexes in an array. Would it be possible to just use the indexes that are auto incremented..?
    Code:
    std::string strnumbers[20] = {"zero", "one", "two", etc, ...};
    Last edited by überfuzz; 04-01-2012 at 04:12 PM.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Quote Originally Posted by überfuzz View Post
    Hi! It looks to me as if you're just rearranging indexes in an array. Would it be possible to just use the indexes that are auto incremented..?
    Code:
    std::string strnumbers[20] = {"zero", "one", "two", etc, ...};
    Hi uberfuzz,
    Thank you for the suggestion. It might work in another program.
    This program requires integer values in each index for the int array. I am going to do math on them, so they need to be ints.
    I realize there is a "conflict" between strings and ints, however, this is similar to a deck of cards where the "Ace" is a string that is equal to the integer value of "-1".
    Also, I already have a 2-Dimensional vector of both int arrays, so I'll be using a vector for the program which eliminates other options like maps. Let me know what you think, thanks!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "Cannot convert 'std::string' to 'int' in assignment."
    Of course you can't do that, for the same reason you can't do this:

    Code:
    int four = "quattro";
    überfuzz has a decent idea for resolving this, here's a similar one:

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main (void) {
    	const string numbers[] = { "one", "two", "three", "four", "five" };
    	map<string,int> lookup;
    
    	for (int i; i < 5; i++)
    		lookup.insert(pair<string,int>(numbers[i], i+1));
    
    	cout << lookup["one"] << endl;
    	cout << lookup["three"] << endl;
    	cout << lookup["five"] << endl;
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello MK27, thank you for the map code. I agree with the idea of "why make it harder" and using a MAP data structure that can take both a string and an int (I'm correct with this yes?).....In this particular case, my goal is to use a vector because it needs to RESIZE itself automatically. I already have a 2-dimensional int vector that is populated with 2 separate int arrays. So, numerically, it works.
    I also have 2 string arrays that maybe I need to put in another 2-dimensional vector and somehow bind the indices together.
    My code for the vector and arrays is:
    Code:
    #include <iostream>	
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    const int typeSize = 4; 
    const int valSize = 14; 
    
    			
    int main()			
    {	
         // arrays to be loaded into 2-dimensional int vector  
        int typeArray[typeSize] = {55,66,77,88};
        int valArray[valSize] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13}; // array is 1 element longer to begin populating vector in column 1
    
       // arrays to be output to screen for User to see 
       string types[4] = {"Manny", "Moe", "Jack", "John" };
       string values[13] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
                                   "Eleven","Twelve","Thirteen"};
    
    
       // load both int arrays into 2-dimensional vector
        for(size_t i = 0; i < myVector.size(); i++)
             { 
               myVector[i][0] = typeArray[i]; 
             }
    
        // output vector to the screen
            for (size_t i = 0; i < myVector.size(); i++)
             {       
               for (size_t j = 0; j < myVector[i].size(); j++)
                {          
                   cout << myVector[i][j] << ' ';
                }       
                cout << '\n';    
            }
    --------------------
    OUTPUT:
    55 1 2 3 4 5 6 7 8 9 10 11 12 13
    66 1 2 3 4 5 6 7 8 9 10 11 12 13
    77 1 2 3 4 5 6 7 8 9 10 11 12 13
    88 1 2 3 4 5 6 7 8 9 10 11 12 13

    Press any key to continue . . .
    ---------------------------------
    The question is: How do I bind the indices of the int arrays into the indices of the string arrays so that the strings display to the user and the ints are "behind the scenes" ready to be used for mathematical calculations? Two vector maybe? Let me know what you think, thanks!
    Last edited by codechick; 04-01-2012 at 09:55 PM.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Could you explain what you're going to do with the number/strings? I said something about math. A tree map should be able to do the things you explained in the first post... MK27 example is a solid way of mapping int to strings. I seem to remember doing a similar thing back in school when I had an assignment to do a symbolic calculator.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Eh, I wrote I said soomething when it should be you said something... (I couldn't fins the edit button.)

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by codechick View Post
    Hello MK27, thank you for the map code. I agree with the idea of "why make it harder" and using a MAP data structure that can take both a string and an int (I'm correct with this yes?).....In this particular case, my goal is to use a vector because it needs to RESIZE itself automatically.
    Maps are also dynamically resizable.

    As to what is the best approach, I'm kind of confused about what you are trying to accomplish. :/ Maybe you should try and explain further what the purpose and end goal are.

    The question is: How do I bind the indices of the int arrays into the indices of the string arrays so that the strings display to the user and the ints are "behind the scenes" ready to be used for mathematical calculations?
    I'd say that depends on how they are going to be used. You mention "mathematical calculations"...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello uberfuzz and MK27!...I apologize for the late response, there were plumbing problems here, etc....etc....; p

    Anyway, back to the fun, yes amigo(a)s...What my goal is, is to make a deck of cards. I omitted stating this originally because I've seen so much debate in various forums as to what's "the best way to do it." So, in answer to your questions, the math I am looking to do is on the card's values. I want to code the cards with ints for both the suits and the values. Why? So, that if I want to use this program later for poker where suits apply to the game, I can re-use the code. As for the values, Ace - King, definitely those are ints.

    MK27, you say "maps are also dynamically resizable." Is this the same as a vector? The whole point of the vector is to be sure that no card is dealt twice until either the game is re-started, or the deck is completely dealt.

    uberfuzz, you talked about a tree map, I don't think I'm there yet...; )....however, I aspire to it one day soon.....the symbolic calculator sounds mondo groovy.
    As for spelling and editing, I try to follow Mr. Job's advice, "stay hungry AND foolish."

    Now that I've "confessed," please advise, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-12-2011, 04:07 AM
  2. Accessing an array index with a string?
    By homer_3 in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2009, 02:34 PM
  3. postfix in array index
    By CaptainBalrog in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2006, 08:39 AM
  4. index array
    By kurz7 in forum C Programming
    Replies: 9
    Last Post: 04-24-2003, 09:57 AM
  5. About Array Index
    By Kokila in forum C Programming
    Replies: 3
    Last Post: 11-08-2001, 12:29 PM

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