CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2006
    Posts
    25

    Loop To Create Variables

    How would I go about creating a loop that creates new variables based on how many I wish?

    For example...

    User declares that there is 3 categories in a Jeopardy game.
    Each category starts with 0 questions, which is displayed beside each categorie.
    Everytime the user adds a new question to a categorie, the number is increased by 1

    So on and so forth...

    Before:

    1. Stuff (0)
    2. More Stuff (0)
    3. Stuff with Stuff (0)

    After Adding One question:
    1. Stuff (1)
    2. More Stuff (0)
    3. Stuff with Stuff (0)

    Im currently programming a Jeopardy type game and would like this addition to it, I know I have to create seperate variables for each categorie so each of the numbers are different...but I have no idea how to go about doing this. Any suggestions?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Loop To Create Variables

    Variables are declared when you write (actually compile) the program. There is no such thing as "dynamic variable creation".

    On the other hand, you can create a collection (declared at compile time), and dynamically put different data into it. Take a look as std::map for examples.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Oct 1999
    Location
    ks
    Posts
    523

    Re: Loop To Create Variables

    coul you not do the following if all vars are the same type you can declare a pointer - eg:

    Code:
    int *ptr_to_int = new[27] int;
    
    for (i=0;i<27;i++){
       *(ptr_to_int + i) = 7*i + 3;
    }

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Loop To Create Variables

    Jim,

    Why would you use a raw array with all the potential for erros, instead of a std::vector, as I suggested previously??
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    May 2006
    Posts
    25

    Re: Loop To Create Variables

    I took a look at both std::vector and std::map and honestly...I dont know where to start! Could you possibly point out specific member functions so I can look into those and start experimenting?

  6. #6
    Join Date
    May 2007
    Posts
    13

    Re: Loop To Create Variables

    You should probably create a class that defines a category, then create and add them to some sort of collection. Here is an example using a vector. It is trivial to have main() create the Category objects using a loop.
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    class Category {
    public:
      bool addQuestion(const std::string &s) {
        if (s.empty())
          return false;
        mQuestions.push_back(s);
        return true;
      }
    
      void displayQuestions() {
        std::vector<std::string>::const_iterator iter;
        for (iter = mQuestions.begin(); iter != mQuestions.end(); ++iter){
          std::cout << *iter << std::endl;
        }
      }
      int getNumberOfQuestions() { return mQuestions.size(); }
    private:
      std::vector<std::string> mQuestions;
    };
    
    int main(){
     std::vector <Category> categories;
    
     Category presidents;
     Category animals;
     std::string question;
     std::cout << "Enter a question: " << std::endl;
     std::getline(std::cin, question);
     presidents.addQuestion(question);
     std::cout << "Enter a question: " << std::endl;
     std::getline(std::cin, question);
     presidents.addQuestion(question);
     categories.push_back(presidents);
     std::cout << "Enter a question: " << std::endl;
     std::getline(std::cin, question);
     animals.addQuestion(question);
     categories.push_back(animals);
    
     std::vector <Category>::iterator iter;
     for (iter = categories.begin(); iter != categories.end(); ++iter){
       std::cout << "Number of questions: " << iter->getNumberOfQuestions() << std::endl;
       iter->displayQuestions();
     }
    
      return 0;
    }
    Last edited by crei; May 5th, 2007 at 03:11 PM.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Loop To Create Variables

    Crei,

    A good bost, but it would be alot more readable if you went back and edited it to use code tags.

    ps: I would have sent you this privately but you have not enabled private messages in your profile....

    Did you read the FAQ's??????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Feb 2007
    Posts
    186

    Re: Loop To Create Variables

    I know how he feels c++ is not very friendly with its containers. They have iterators and templates all over the place and the syntax is very ugly.

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Loop To Create Variables

    Quote Originally Posted by dragnot15
    I took a look at both std::vector and std::map and honestly...I dont know where to start! Could you possibly point out specific member functions so I can look into those and start experimenting?
    Look at msdn for references. They provide nice examples for each of the members that can be very helpful when starting with the STL. Look here - http://msdn2.microsoft.com/en-us/lib...07(VS.80).aspx
    Quote Originally Posted by abcdefgqwerty
    I know how he feels c++ is not very friendly with its containers. They have iterators and templates all over the place and the syntax is very ugly.
    And you would want to work in C++ without knowledge of these topics? And ugliness of syntax - that is just a matter of personal perception and taste. Moreover, they look ugly compared to what actually?

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Loop To Create Variables

    Moreover, they look ugly compared to what actually?
    To me, they [STL iterators] look much "prettier" than nearly ever custom implementation I have seen that attempts to accomplish the same goals...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured