Problem with some arrays.

Hi, I'm a beginner in C++(obviously :P), anyways, I'm having troubles with an specific program that I was asked to do. I know I'm not supposed to ask about homework in this forum, but I have already tried all the possibilities and to no avail. I just need a little push so I can figure out what I should do (any tip or hint). Well to the case:

I'm supposed to make an elevator simulator, but it's unlike the ones that are on the internet. The program is as follows:

Make an elevator simulator that takes the of amount of floors (input by the user), takes any number of people (again input by the user), the floors where they are and the floors they want to go. The elevator is supposed to take those people to their destination the most efficient way possible. For example:

no. of floors =4
no. of people =3
person one is in 1 floor and wants to go to the fourth floor
person two is in 3 floor and wants to go to the second floor
person three is in 2 floor and wants to go to the third
So the elevator goes like this:
elevator is in floor 1, takes person 1 and then goes to the 2 floor, takes person 3, goes to floor 3, person 3 gets off and person 2 gets in, then it goes to 4 floor, person 1 gets off and then the elevator goes to floor 1 and person 2 gets off.
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
#include<iostream>
#include <conio.h>

using namespace std;
int pisos,no_personas,usuario,piso_ascensor,user;// in order, floors,no.people,usuario(user in spanish),floor_elevator,user
int main(){
    cout << "           Bienvenido al sistema gestor de ascensores" << endl;//welcome to the program
    cout << endl;
    cout << endl;
    cout << "Cuantos pisos tiene el edificio? ";//how many floors does the building has
    cin >> pisos;
    cout << "Cuantas personas lo van a abordar(peso maximo 360kg [4 personas])? ";//how many people going to use it
    cin >> no_personas;
    int piso_original[usuario];//original_floor
    int piso_deseado[usuario];//desired_floor
    
    for(usuario =0; usuario < no_personas; usuario++){
                cout << "En que piso se encuentra el usuario #" << (usuario+1) << "? ";//in which floor is the user
                cin >> piso_original[usuario];
               cin.clear();
                }             
    for(usuario =0; usuario < no_personas; usuario++){
                cout << "A que piso se dirije el usuario #" << (usuario+1) << "? ";// where is user # he headed
                cin >> piso_deseado[usuario];
                cin.clear();
                }  
    piso_ascensor=1;            
    while(piso_ascensor<=pisos){
     
          for(user =0; user < no_personas; user++){
                           if (piso_ascensor==piso_original[user]){
                           cout << "Piso#" << piso_ascensor << endl;
                           cout << "Entra el usuario #" << (user+1) << endl;//user # enters
                           
                           }else{}
                                 }
                                 piso_ascensor++;
                                 }                 
   while(piso_ascensor>0){
                          for(user = 0; user < no_personas; user++){
                           if (piso_ascensor==piso_deseado[user]){
                           cout << "Piso#" << piso_ascensor << endl;
                           cout << "Sale el usuario #" << (user+1) << endl;//user # gets out
                           }else{}
                                 }
                                 piso_ascensor--; 
                                 }
getch();
return 0;
}                                 

I'm having trouble making the elevator take the most efficient route possible, any tips?
Sorry for my grammar. I would appreciate any help.
Last edited on
don't looked in all your code, but already have a question.
you use int piso_original[usuario];//original_floor
what is value of variable usuario? As I don't see it.
Well, since I don't know how many people the user is going to input, I thought I could assign the value of [usuario] later in the for loop. I don't know if that's correct, but it's the only way I see to make the array piso_original[usuario] take as many inputs as the user desires. Correct me if I'm wrong.
If you're going to declare an array with bounds, I'm fairly sure they can't be variable. It has to be a variable declared initially or a number. You can use std::strings or std::vectors if you want resizable ones
Could you please explain me in more detail? I haven't used std::vectors yet. Thanks.
You can't dynamically size an array that has been statically defined. The compiler will need to know the size of the array at compile time.

You could dynamically size the array using pointers but, as NerdTastic suggested, your best option would be to use vectors: http://www.cplusplus.com/reference/stl/vector/.
Last edited on
Ok, thanks, I will do that, but regarding the algorithm to make the elevator route the most efficient possible, any suggestions?
Topic archived. No new replies allowed.