### Make Costume variable.

Is there a way to create a costume variable. I want 3 dimensions array, with first dimension string, 2nd float, 3rd int. Could you tell me if is it possible?
Not exactly. You can't make an array which would consist of three times, but you can use std containers to achieve whatever you want, or make a class especially for that.
Thanks for your input.
with std containers you mean int,string,float?

what is a class? :)

I mesed up something there. I was wondering if I can create for example

myvariable variable[3][3];

variable [0-2][0] - string
variable [0-2][1] - float
variable [0-2][2] - int

ofcourse I can create:
int variable1[3];
float variable2[3];
string variable3[3];

but the problem is that, when I need to for example sort int variable, float and string must be changed as int would be and I end up with so many variables for sorting function. :/

what is closest way I can get to this.
Last edited on
How are you learning C++? If you are learning from tutorial/book, then (if it's worth reading) it will cover classes, polymorphism, etc. So don't worry - if you don't know it yet, it doesn't matter, you'll learn it soon :)
But answering question, class is a way of representing some object and its properties. It's useful, but take your time.

Std containers are also another thing. std is standard namespace, which holds many different things. I used it more like a short version of "standard". These things are there so you don't have to "reinvent the wheel". For example, you don't have to create sqrt function to calculate square root of a number - C++ already has it.

Now to explain everything that you don't know would be too long(basically I would make a C++ tutorial in here :)), so instead, I'll simply answer your question.

An array isn't a type. Integer is a type. Also, an array of integer is a type too - but see the difference; array OF something makes a type. And you can't make multi-type arrays.
Array is a container. It contains some things - in (nearly) the same way, as box contains things. You can put things to box. Then, you can look at them, switch them, and so on. Same goes with array. List may be another example of container - shopping list contains some words(strings).

Anyway, if you can make these change in one 2d array, then I guess you can also make it work with 3 arrays. You just have to figure out how.
And if I were you, I'd go with 3-arrays solution. It's something that you can understand, and it will work. You will practice your knowledge, and learn how to solve some problems. If you start using some more advanced methods, you may make more errors, and not solve problem at all.

Anyway, good luck with your project. Cheers!
hehe, Im learning as student in class I've got this homework where people have to order pizzas. They can choose how they will be sorted by price or name and ascending order or vica versa. Then they write in name of pizza and how many pizzas they want and I also need to show a basket(order list, count, total price). You got the idea, and I have 5 functions and each one need to get names, prices and how many pizzas are ordered. I was just wondering if I can make things more simpler. It looks very messy at least for me.

We are not allowed to use any built in functions(for example sorting), but we can use sqrt etc.

You will see that I tried some new stuff, some worked some didnt :D, but gonna fix. Questions are in latvian.

1st question - sorted by name(nosaukums) price(cena)
2nd question - ascending(augosa) descending(dilstosha)
3rd question - Which pizza you want to order?
4th question - How manu you want to order?
5th question - If you want to order more pizzas?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` ``````#include #include using namespace std; const int P = 10; void SortingStringArray(string* str,float* intig,int lenght, bool ascending){ int PizzaNameLenght[lenght],i; string tmp; float tmp2; for(int i=0;i(int)str[j+1].at(i)){ tmp = str[j]; str[j]=str[j+1]; str[j+1]=tmp; tmp2 = intig[j]; intig[j]=intig[j+1]; intig[j+1]=tmp2; } i++; }while((int)str[j].at(i-1) == (int)str[j+1].at(i-1) && PizzaNameLenght[0] > i && PizzaNameLenght[0] > i); } else{ do{ if ((int)str[j].at(i)<(int)str[j+1].at(i)){ tmp = str[j]; str[j]=str[j+1]; str[j+1]=tmp; tmp2 = intig[j]; intig[j]=intig[j+1]; intig[j+1]=tmp2; } i++; }while((int)str[j].at(i-1) == (int)str[j+1].at(i-1) && PizzaNameLenght[0] > i && PizzaNameLenght[0] > i); } } } } void SortingFloatArray(float* array,string* str, int length, bool ascending){ string tmp2; float tmp; if (ascending){ //karto augosa seciba for(int j=0;j array[i+1]){ tmp2 = str[i]; str[i]=str[i+1]; str[i+1]=tmp2; tmp = array[i]; array[i]=array[i+1]; array[i+1]=tmp; } } else{ //karto dilstosa seciba for(int j=0;j 0){ cnt++; } } if (cnt>0){ bool comma; float total=0; int counter=0; cout << "Tavs grozins: "; for (int i=0;i0){ counter++; cout << array[i] << "(" << Order[i] << ")"; total = total + Order[i]*prices[i]; if (counter==cnt) cout << ".\n"; else cout << ", "; } } cout << "Kopa ir " << total << " Euro.\n\n"; } } bool AskBoolQuestion(string question){ string answer; do{ cout << question; cin >> answer; system("cls"); if ((int)answer.at(0) == 49 && answer.length() == 1 ) return true; if ((int)answer.at(0) == 50 && answer.length() == 1 ) return false; else cout << "Tadas iespejas nav ludzu izveleties velreiz.\n"; } while (1<2); } void PrintPizza(string* Name,float* Price,int strlenght){ for(int i=0;i> answer; for(int i=0;i> tmp2; PizzaOrder[tmp]=PizzaOrder[tmp]+tmp2; Finish = AskBoolQuestion("Vai velaties pasutit vel citas picas(ne-1,ja-2)? - "); } while(!Finish); ShowBasket(PizzaName,PizzaPrice,PizzaOrder,P); return 0; }``````
Perhaps you are looking for something like this?
 ``12345`` ``````struct Pizza { std::string name; double price; int order; }``````

You could then make an array of Pizzas like so:
`Pizza pizzas[3];`
Each of the array elements is a Pizza object, so you can access the differently typed fields:
`Pizza[0].name` is the name of the first pizza in the array, and so on.
hey thanks. When I tried to implement it in I coudnt figure more efficient way yo give values, only by giving it individually like:

 ``12`` ``````Pizza[0].name="blah"; Pizza[1].name="meh";``````

This is how its done? Coudnt find anything better.
Topic archived. No new replies allowed.