TransWikia.com

Flight destination check programm - C++

Stack Overflow Asked by MrFreeman16 on January 12, 2021

I am trying to solve the following question:

Write a program, which will get from the user

  1. the Number of flights (N)
  2. the destination of train (city)
  3. the train number for certain destination (number)
  4. the name of the wanted destination city, for which we are searching possible trains.
    The program should find and display all numbers of trains, which go to wanted destination. If there is no such train, the program must display "Unreachable city!".

Now the problem is that I wrote a code which finds such train number, but not numbers of all trains, it cannot display all the train numbers going to wanted destination point.

I.e. if I input following data:

3
Chicago I-789
Chicago J-159
Chicago A-465
Chicago

It shows me only last train number A-465, whereas the right answer would be: I-789 J-159 A-465

#include <iostream>
#include <string>
using namespace std;

class MyClass {  
    
  public:      
  
    string city;      
    string number;  
    
};

int main() {
    
    int N;
    cin >> N;
        
  MyClass myObj;  
  
 for (int i=0; i<N; i++){
     cin>>myObj.city;
     cin>>myObj.number;
 }
 string destination;
 cin >> destination;
     if(myObj.city==destination){
         cout << myObj.number;
     }
     else{
         cout << "Unreachable city!";
     }
  return 0;
}

3 Answers

On your comment:

C++ is much harder than python.

Programming languages are just tools. You use them to solve a problem. If you don't know how to use a tool, you can't solve the problem. Your computer is a tool, if you don't know how to operate it, you can't do your homework. That doesn't mean computers are difficult to use. Similarly, C++ is a tool, if you don't know it, it doesn't mean it's difficult.

Let's get to the problem.

The problem

The program should find and display all numbers of trains, which go to wanted destination. If there is no such train, the program must display "Unreachable city!".

Let's break it down

  • Reading the problem carefully, we can see that there is not just "one" train but multiple 'trains'.
  • We have to take input from the user (wanted destination)
  • Then we have to find "all" the "trains" that go that city.
  • If no train was found, we print "Unreachable city!"

The problem with your code

The problem with your code is that there is only "one train":

  MyClass myObj;  //one object only

You keep overwriting it's values every time you take input from the user.

Learning the tool

So, What can you do to fix this? In programming when we want to store multiple values of the same object we usually create an array. An array is just a collection of values of one type. Example:

int myarray[5]; //can store 5 "int" values
                //size is given inside the [] (square brackets)

Array indexes start from 0. We can store values in array like following:

cin >> myarray[0];  //take input from user and store it into the "first" place in our array
cin >> myarray[1]; //store in the "second" place
cin >> myarray[4]; //store in the "last" place
cin >> myarray[5]; //WRONG! Don't do this. It will result in errors and bugs!! (Undefined Behaviour)

You can also store values directly:

int myarray[5] = {1, 2, 3, 4, 5};
cout << myarray[3]; // prints "4"

That's all nice and fine however there is a small problem with arrays. We have to know the "size" of the array before we create it.

int N;
cin >> N;
int array[N]; //WRONG, even it works, this is wrong.

So, what should we do? We can't know the number of objects we want always. Worry not, because C++ provides us with a nice container: std::vector which can be used to solve this issue.

#include <vector> // you need this for vector
int N;
cin >> N;
std::vector <int> myvector(N); //vector of size N

//access the values as you would with the array
myvector[0] = 10;
myvector[5] = 9; //WRONG. 

Solving your problem

Note, that I will not give you the solution directly, but I will show you the way and give you the tools. It's your problem, it's your challenge, and if you try, it's pretty easy to solve the problem.

So we learned about vectors and arrays. Next, you may be wondering how to create vector for your type. Simple:

//create a vector, with size = N
vector <MyClass> Trains (N);

//take input from user
for (int i=0; i<N; i++){
  cin >> Trains[i].city;
  cin >> Trains[i].number;
} 

The last part, will be quite similar. You need a loop, then go over all the values in the vector to find the "destinations" you want.


Side note

You should start with naming your objects and variables in a way that it is easy and natural for you to think about your problem. For example:

class MyClass 

This doesn't tell anyone, anything about your class or what you want to do with it. What could be a better name for it? Looking at the problem, I suggest the name Train:

class Train {};

The problem also tells us that each train has a "destination city" and a "train number". We can refactor our Train class to contain the following:

class Train {  
  public:      
    string destination;      
    string number;  
};

Correct answer by Waqar on January 12, 2021

    #include <iostream>
#include <string>
using namespace std;
#include <vector> // you need this for vector

class Train {  
  public:      
    string city;      
    string number;  
};

int main(){
int N;
cin >> N;
string dest;
vector <Train> Trains (N);
int i;

//take input from user
for (i=0; i<N; i++){
  cin >> Trains[i].city;
  cin >> Trains[i].number;
} 
   cin >> dest;
for(i=0; i<Trains.size(); i++){
  if(Trains[i].city==dest){
     
    cout << Trains[i].number <<" ";
}
else{
    cout << "Unreachable city!";
}
}
}

Now it is always printing Unreachable city next to right results :(

Answered by MrFreeman16 on January 12, 2021

First, myObj is not a good name, let's change it to an empty list of destinations.

#include <vector>
...
vector <MyClass> destinations;

Next, push each new value into the vector. For this it would be better to have a constructor that sets the values. Constructing a destination with no values is pointless.

MyClass(string _c, string _n) : city(_c), number(_n) {};
...
string city, number;
cin >> city;
cin >> number;
destinations.pushback(MyClass(city, number));

Now you can write your loops to go through the vector looking for the data you need.

Answered by stark on January 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP