-
May 1st, 2007, 11:13 AM
#1
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?
-
May 1st, 2007, 11:18 AM
#2
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
-
May 1st, 2007, 12:41 PM
#3
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;
}
-
May 1st, 2007, 12:50 PM
#4
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
-
May 2nd, 2007, 03:57 PM
#5
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?
-
May 2nd, 2007, 09:29 PM
#6
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.
-
May 3rd, 2007, 04:08 AM
#7
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
-
May 3rd, 2007, 09:36 AM
#8
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.
-
May 5th, 2007, 02:27 AM
#9
Re: Loop To Create Variables
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
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?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 5th, 2007, 06:17 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|