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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <cstdlib>
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<lenght;i++)
        PizzaNameLenght[i] = str[i].length();

    for (int k=0;k<lenght-1;k++){
        for(int j=0;j<lenght-1;j++){
            i=0;
            if (ascending){
                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);
            }
            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<length-1;j++)
            for(int i=0;i<length-1;i++)
                    if (array[i] > 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<length-1;j++)
            for(int i=0;i<length-1;i++)
                    if (array[i] < 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;
                    }
    }

}


void ShowBasket(string* array,float* prices,int Order[],int length){

    int cnt=0;

for (int i=0;i<length;i++){
    if (Order[i] > 0){
        cnt++;
    }
}
if (cnt>0){
         bool comma;
  float total=0;
  int counter=0;
cout << "Tavs grozins: ";
for (int i=0;i<length;i++){
    if (Order[i]>0){
            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<strlenght;i++){
        cout << Name[i];
        for(int j=0;j<15-Name[i].length();j++)
            cout << " ";
        cout << Price[i] << endl;
    }

}

int AskPizzaName(string* Name,float* Price,int Order[], int length){
    string answer;
    do{
    cout << "\nKadu picu jus velaties pasutit? - ";
    cin >> answer;
    for(int i=0;i<length;i++)
        if (answer == Name[i])
            return i;
    system("cls");
    ShowBasket(Name,Price,Order,length);
    PrintPizza(Name,Price,length);
    cout << "\nTadas picas nav.";
    }
    while (1<2);
}


int main(){
    string PizzaName[P]={"Turistu", "Parmas", "Vezuva", "Peperoni", "Chili","Havaju","Garda","Studentu","Rietumu","Vegetariesu" };
    float PizzaPrice[P]={12.32, 17.91, 12.31, 17.63, 16.64, 13.76, 16.26, 15.49, 12.99, 13.69};
    int PizzaOrder[P]={0},tmp,tmp2;
    bool SortByName,Ascending,Finish;

    SortByName = AskBoolQuestion("Izveleties picas pec Nosaukuma(1) vai Cenas(2):");
    Ascending = AskBoolQuestion("Augosa(1) vai dilstosa(2) seciba:");

    if (SortByName)
        SortingStringArray(PizzaName,PizzaPrice,P,Ascending);
    else
        SortingFloatArray(PizzaPrice,PizzaName,P,Ascending);

     do{
    system("cls");
    ShowBasket(PizzaName,PizzaPrice,PizzaOrder,P);
    PrintPizza(PizzaName,PizzaPrice,P);
    tmp = AskPizzaName(PizzaName,PizzaPrice,PizzaOrder,P);
    system("cls");
    ShowBasket(PizzaName,PizzaPrice,PizzaOrder,P);
    PrintPizza(PizzaName,PizzaPrice,P);
    cout << "\nCik " <<  PizzaName[tmp] << " picas jus velaties pasutit? - ";
    cin >> 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?
1
2
3
4
5
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:

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


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