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.
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;
}
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?? :confused: :confused:
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?
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;
}
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?????? :confused: :confused:
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.
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?
Re: Loop To Create Variables
Quote:
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...