Is it possible to assign different variables to each vector value ?
I have this function here. I don't have much code written out yet I am just building it out.
In my main I am going to ask the user how many pills they take. If they say 5 that gets passed through here and the vector is now size 5.
Ideally I want to be able to assig a Pill1 Pill2 etc... so I can ask dosage times so I can then output a calendar.
Is that something that is possible ? Am I on the right track ?
1 2 3 4 5 6 7 8 9 10
void Pill::definePills(int p)
{
//load vector with number of pills user takes
vector<int> pillDef(p);
for (int p : pillDef)
{
cout << pillDef[p]<< " ";
}
cout << "\n";
}
Yes as long as the value that you assign is an "int" it works. If you want something different it will not work.
When you define the vector: vector<int> pillDef(p); the vector has a starting size of "p" very similar to defining a C style array.
Something you might want to consider is to create a struct the has all the information needed for each pill then create a vector of those structs. Then you would not have to have several vectors just to keep track of all the information about a pill.
Without more to go on it is hard to guess at what you are doing.
That one has issues:
* The loop declares local variable 'p' that masks function's argument 'p'. While it might be ok, it can lead to misunderstandings.
* The loop has range-based syntax. The 'p' gets value of an element on each iteration. It is like you had written:
1 2 3 4
for ( int e = 0; e < pillDef.size(); ++e )
{
cout << pillDef[ pillDef[e] ] << " ";
}
With ranged for you should write:
1 2 3 4
for ( int t : pillDef )
{
cout << t << " ";
}
If you want to assign values to elements with ranged for, then use reference:
1 2 3 4
for ( int& t : pillDef )
{
t = 42;
}
What does one integer element in the vector pillDef represent? You said "dosage time". The integer is a time? Answers "When?"
Each element of vector has index. They are 0-based, so for vector of p elements they are 0 .. p-1
The index can be used as implicit number of a pill:
1 2 3 4
for ( int e = 0; e < pillDef.size(); ++e )
{
cout << "Take Pill" << (e+1) << " at " << pillDef[e] << '\n';
}