In: Computer Science
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE
Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
bus.h
===========
#pragma once
#include
using namespace std;
class Bus {
private:
string BusId; // bus ID
string Manufacturer; // manufacturer of the bus
int BusCapacity; // bus capacity
int Mileage; // mileage of bus
char Status; // current status of the bus
public:
Bus(string bus_id, string manufac, int cap, int
mileage, char status); // constructor
~Bus(); // destructor
// getter methods
string getBusId(); // return bus id
string getManufacturer(); // return manufacturer of
the bus
int getBusCapacity(); // return bus capacity
int getMileage(); // return bus mileage
char getStatus(); // return current status of
bus
// setter methods
void setStatus(char s); // set the given status to the
bus status
};
============
bus.cpp
============
#include "bus.h"
Bus::Bus(string bus_id, string manufac, int cap, int mileage,
char status) {
// initialize member variables
BusId = bus_id;
Manufacturer = manufac;
BusCapacity = cap;
Mileage = mileage;
Status = status;
}
Bus::~Bus() {
// delete bus object
}
string Bus::getBusId() {
return BusId;
}
string Bus::getManufacturer() {
return Manufacturer;
}
int Bus::getBusCapacity() {
return BusCapacity;
}
int Bus::getMileage() {
return Mileage;
}
char Bus::getStatus() {
return Status;
}
void Bus::setStatus(char s) {
Status = s;
}
================
functions.h
================
#pragma once
#include "bus.h"
#include
// print column header
void displayCH();
// print bus data
void printId(string id);
void printMfc(string mfc);
void printCap(int cap);
void printMileage(int mileage);
void printStatus(char s);
void printEndl();
=============
functions.cpp
=============
#include "functions.h"
void displayCH() {
cout <<
"================================================================="
<< endl;
cout << setw(10) << left << "Bus
ID";
cout << setw(20) << left << "Bus
Manufacturer";
cout << setw(10) << right << "Bus
Capacity";
cout << setw(10) << right <<
"Mileage";
cout << setw(10) << right <<
"Status" << endl;
cout <<
"================================================================="
<< endl;
}
void printId(string id) {
cout << setw(10) << left <<
id;
}
void printMfc(string mfc) {
cout << setw(20) << left <<
mfc;
}
void printCap(int cap) {
cout << setw(10) << right <<
cap;
}
void printMileage(int mileage) {
cout << setw(10) << right <<
mileage;
}
void printStatus(char s) {
cout << setw(10) << right << s
<< endl;
}
void printEndl() {
cout << "---end of list---" << endl;
}
===========
main.cpp
===========
#include "functions.h"
#include
#include
#include
int main() {
vector buses; // stores a list of buses created
// read input record
// create file stream object to read data
ifstream file("input.txt");
string line; // stores input record for a bus
while (!file.eof()) {
getline(file, line); // read input
line by line
// first 5 character in record are
bus id
string id = line.substr(0,
5);
// remove id from line
line = line.substr(5);
// last charater in record is
status
char status = line.at(line.length()
- 1);
// remove last charater from
line
line =
line.substr(0,line.length()-1);
// now last 7 digit are mileage of
bus
string miles =
line.substr(line.length() - 7);
// remove last 7 character from
line
line = line.substr(0, line.length()
- 7);
// convert string to int
int mileage = stoi(miles);
// now last 3 digit in the line are
bus capacity
int cap =
stoi(line.substr(line.length() - 3));
// remove last 3 character from
line
line = line.substr(0, line.length()
- 3);
// remainig data in line is
manufaturer
// create a bus object
Bus
b(id,line,cap,mileage,status);
// add bus to the list
buses.push_back(b);
}// end of while loop
// close file stream as done reading data
file.close();
// get user input
string input = ""; // stores user input
while (input != "X") {
// promt user for input
cout << "Enter transaction
code (D=display, L=list a bus, C=change, X=exit)" <<
endl;
getline(cin, input); // get input
from user
// read input
if (input.substr(0,1) == "D")
{
//display all
bus object
displayCH();
for (int i = 0;
i < buses.size(); i++) {
printId(buses.at(i).getBusId()); // print bus
id
printMfc(buses.at(i).getManufacturer()); //
print bus manufacturer
printCap(buses.at(i).getBusCapacity()); // print
bus capacity
printMileage(buses.at(i).getMileage()); // print
bus mileage
printStatus(buses.at(i).getStatus()); // print
current status of the bus
}
printEndl();
}
// check if input is exit
else if (input.substr(0, 1) == "X")
{
// break the
loop
break;
}
// check if input is list
else if (input.substr(0, 1) == "L")
{
// check for bus
id
string id =
input.substr(2);
// create a flag
to indicate if bus found
bool found =
false;
for (int i = 0;
i < buses.size(); i++) {
if (buses.at(i).getBusId().compare(id) == 0)
{
// print header
displayCH();
// print bus data
printId(buses.at(i).getBusId()); // print bus id
printMfc(buses.at(i).getManufacturer()); // print bus
manufacturer
printCap(buses.at(i).getBusCapacity()); // print bus capacity
printMileage(buses.at(i).getMileage()); // print bus mileage
printStatus(buses.at(i).getStatus()); // print current status of
the bus
printEndl(); // print end
line
// set flag to true
found = true;
}
}// checked all
buses data
if (!found)
{
// print error
cout << "Not found" << endl;
}
}
// check if input is change
else if (input.substr(0, 1) == "C")
{
// check for bus
id
string id =
input.substr(2,5);
// create a flag
to indicate if bus found
bool found =
false;
for (int i = 0;
i < buses.size(); i++) {
if (buses.at(i).getBusId().compare(id) == 0)
{
// get current status of
bus
char status =
input.at(8);
// change status of bus
buses.at(i).setStatus(status);
// print message of change
done
cout << "Change
successful" << endl;
// set flag to true
found = true;
}
}// checked all
buses data
if (!found)
{
// print error
cout << "Not found" << endl;
}
}
else {
// print error
in input
cout <<
"Invalid Input!" << endl;
}
}
// print goodbuy message
cout << "Thank you and have a nice day!"
<< endl;
return 0;
}
==========
input.txt
==========
A1234Gillig 0750001000a
B2345Flxible 1100011235a
C3456Daimler 0350002500a
D4567MAN 0900125000a
E5678General Mo1400150000a
F6789Gillig 0480002000r
G7890Gillig 0480003000m
H8901Flxible 1100012330m
I9012Flxible 1090015500a
J0123Daimler 0350002750r
Only need change in main part.
Input the data as per the output screen given.
#include "functions.cpp"
//#include "bus.cpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector <Bus>buses; // stores a list of buses created
int mileage;
int cap;
// read input record
// create file stream object to read data
ifstream file("input.txt");
string line; // stores input record for a bus
while (!file.eof()) {
getline(file, line); // read input line by line
// first 5 character in record are bus id
string id = line.substr(0, 5);
// remove id from line
line = line.substr(5);
// last charater in record is status
char status = line.at(line.length() - 1);
// remove last charater from line
line = line.substr(0,line.length()-1);
// now last 7 digit are mileage of bus
string miles = line.substr(line.length() - 7);
// remove last 7 character from line
line = line.substr(0, line.length() - 7);
// object from the class stringstream
stringstream convert(miles);
// convert string to int
convert>>mileage;
// now last 3 digit in the line are bus capacity
stringstream convertDigit(line.substr(line.length() - 3));
convertDigit>>cap;
//stoi();
// remove last 3 character from line
line = line.substr(0, line.length() - 3);
// remainig data in line is manufaturer
// create a bus object
Bus b(id,line,cap,mileage,status);
// add bus to the list
buses.push_back(b);
}// end of while loop
// close file stream as done reading data
file.close();
// get user input
string input = ""; // stores user input
while (input != "X") {
// promt user for input
cout << "Enter transaction code \n D = display \n L = list a
bus (Example: L A1234) \
\n C = change (Example: C A1234 a) \n X = exit" <<
endl;
getline(cin, input); // get input from user
// read input
if (input.substr(0,1) == "D") {
//display all bus object
displayCH();
for (int i = 0; i < buses.size(); i++) {
printId(buses.at(i).getBusId()); // print bus id
printMfc(buses.at(i).getManufacturer()); // print bus
manufacturer
printCap(buses.at(i).getBusCapacity()); // print bus capacity
printMileage(buses.at(i).getMileage()); // print bus mileage
printStatus(buses.at(i).getStatus()); // print current status of
the bus
}
printEndl();
}
// check if input is exit
else if (input.substr(0, 1) == "X") {
// break the loop
break;
}
// check if input is list
else if (input.substr(0, 1) == "L") {
// check for bus id
string id = input.substr(2);
// create a flag to indicate if bus found
bool found = false;
for (int i = 0; i < buses.size(); i++) {
if (buses.at(i).getBusId().compare(id) == 0) {
// print header
displayCH();
// print bus data
printId(buses.at(i).getBusId()); // print bus id
printMfc(buses.at(i).getManufacturer()); // print bus
manufacturer
printCap(buses.at(i).getBusCapacity()); // print bus capacity
printMileage(buses.at(i).getMileage()); // print bus mileage
printStatus(buses.at(i).getStatus()); // print current status of
the bus
printEndl(); // print end line
// set flag to true
found = true;
}
}// checked all buses data
if (!found) {
// print error
cout << "Not found" << endl;
}
}
// check if input is change
else if (input.substr(0, 1) == "C")
{
// check for bus id
string id = input.substr(2, 5);
// create a flag to indicate if bus found
bool found = false;
for (int i = 0; i < buses.size(); i++) {
if (buses.at(i).getBusId().compare(id) == 0) {
// get current status of bus
char status = input.at(8);
// change status of bus
buses.at(i).setStatus(status);
// print message of change done
cout << "Change successful" << endl;
// set flag to true
found = true;
}
}// checked all buses data
if (!found) {
// print error
cout << "Not found" << endl;
}
}
else {
// print error in input
cout << "Invalid Input!" << endl;
}
}
// print goodbuy message
cout << "Thank you and have a nice day!" << endl;
return 0;
}
Sample Output:
Enter transaction code
D = display
L = list a bus (Example: L A1234)
C = change (Example: C A1234 a)
X = exit
D
=================================================================
Bus ID Bus Manufacturer Bus Capacity Mileage Status
=================================================================
A1234 Gillig 75 1000 a
B2345 Flxible 110 11235 a
C3456 Daimler 35 2500 a
D4567 MAN 90 125000 a
E5678 General Mo 140 150000 a
F6789 Gillig 48 2000 r
G7890 Gillig 48 3000 m
H8901 Flxible 110 12330 m
I9012 Flxible 109 15500 a
J0123 Daimler 35 2750 r
---end of list---
Enter transaction code
D = display
L = list a bus (Example: L A1234)
C = change (Example: C A1234 a)
X = exit
L D4567
=================================================================
Bus ID Bus Manufacturer Bus Capacity Mileage Status
=================================================================
D4567 MAN 90 125000 a
---end of list---
Enter transaction code
D = display
L = list a bus (Example: L A1234)
C = change (Example: C A1234 a)
X = exit
C J0123 m
Change successful
Enter transaction code
D = display
L = list a bus (Example: L A1234)
C = change (Example: C A1234 a)
X = exit
D
=================================================================
Bus ID Bus Manufacturer Bus Capacity Mileage Status
=================================================================
A1234 Gillig 75 1000 a
B2345 Flxible 110 11235 a
C3456 Daimler 35 2500 a
D4567 MAN 90 125000 a
E5678 General Mo 140 150000 a
F6789 Gillig 48 2000 r
G7890 Gillig 48 3000 m
H8901 Flxible 110 12330 m
I9012 Flxible 109 15500 a
J0123 Daimler 35 2750 m
---end of list---
Enter transaction code
D = display
L = list a bus (Example: L A1234)
C = change (Example: C A1234 a)
X = exit
X
Thank you and have a nice day!