I am trying to create a set of tasks that will loop according to the number of times the user has specified with different sets of variables.
However, I am wondering if there is a way to automatically define "n" sets of variables, without having to pre-define it at the start.
For this simple example,
1) Start
2) No. of land farmer has: nOofland = 5 (cin from user)
3) Lenght of land 1 (m): lengthofland1 = 10 (cin from user)
4) Width of land 1 (m) widthofland1 = 15 (cin from user)
5) nOofland--
6) if nOofLand != 0, loop back to step 3 and asks for details for next plot of land // and at this step I would like to automatically create variables "lengthofland2" and "widthofland2", "lengthofland3" and "widthofland3" and so on... until 5 sets of data is entered.
Could someone please provide the best solution please?
#include <iostream>
#include <vector>
struct Land
{
int length, width;
Land(int l_, int w_)
{
length = l_;
width = w_;
}
};
void main()
{
int numLands, temp_length, temp_width; //variables to store user inputs
std::vector<Land> LandList; //list of Lands
std::cout<<"Enter Number of Lands: ";
std::cin>>numLands; //get number of lands
for(int i=0;i<numLands;i++) //loop according to how many lands you have
{
std::cout<<"Enter Length of Land "<<(i+1)<<": ";
std::cin>>temp_length; //get length
std::cout<<"Enter Width of Land "<<(i+1)<<": ";
std::cin>>temp_width; //get width
LandList.push_back(Land(temp_length, temp_width)); //push back the current land
}
std::cout<<"\nNumber of Lands: "<<LandList.size()<<std::endl;
for(int i=0;i<LandList.size();i++)
std::cout<<"Land "<<(i+1)<<" : "<<LandList.at(i).length<<" "<<LandList.at(i).width<<std::endl;
system("pause");
}
But quick question out of this topic, is there a command for C++ to read in a matrix of values? is it arrays?
"Arrays" isn't a command, but they can be used for this purpose if they are inside of a for loop. Something like this is very useful:
1 2 3 4
for(int i = 0; i < sizeof(matrix); ++i) /* matrix in this example is a place holder for what ever you use*/
{
MyArray[i] = Next_Item_In_Matrix; /*More Place Holders*/
}
Moschops is right about creating variables with new names, afterall how would you even address them inside of your code if you don't know what they are called or what they hold? This is because after the program builds and translates you C++ code, those names don't exist anymore, they are replaced with addresses that have less then helpful hexadecimal labels. So why would your compiler even want to create them at run time?
Vectors are REALLY cool IMO, they are worth any minor pains you may go through while learning them.
Ah... Not quite. You wrote "What you didnt define will not be built at the output." This still isn't technically correct and I'm only being this pedantic because I know how hard it is to "unlearn" stuff in this language.
A better way to put it is that once the program is built, the names that you gave to the variables don't exist anymore.
firedraco's code will only work on arrays whose size is known at compile time. The above function also only works on arrays whose size is known, but it throws an error when the size is not known, whereas firedraco's does not (meaning that you can't rely on it).
@chrisname: The actual arraysize function is this:
1 2 3 4 5
template <typename T, unsigned S>
unsigned arraysize(const T (&array)[S]) // the parenthesis here aren't for show
{
return S; // no need for sizeof nonsense
}