Question

In: Computer Science

Hello I have this error in the code, I do not know how to fix it....

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

Solutions

Expert Solution

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!


Related Solutions

I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
It shows me that 1 error exists in this code but I didn't know how to...
It shows me that 1 error exists in this code but I didn't know how to fix this error so if you can help I will appreciate it. Language C++. Code: #include <iostream> #include <string> #include <iterator> #include <fstream> #include <sstream> #include <cstdlib> #include <set> using namespace std; class Book { private: string BookId; string BookISBN; string Publisher; int PublisherYear; double Price; int Quantity; string Author; public: string SetBookId(); string SetBookISBN(); string SetPublisher(); int SetPublisherYear(); double SetPrice(); int SetQuantity(); string...
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
hello everyone, i have this assignment to do and i dunno how to structure it as...
hello everyone, i have this assignment to do and i dunno how to structure it as i didnt have enough informations about the layout and the structure. please provide me with the structure starting from the introduction, excutive summary,...,.....,......,.......,conclusion and references. i need to fill up the paragraphs but i dunno what to layout. Assume your group comprises the Sydney City Council. In an effort to reduce inner city congestion, there is already a project to establish light rail. But...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
Hello, i would like to know about what do i need to know post surgical nursing...
Hello, i would like to know about what do i need to know post surgical nursing intervention and teaching patient.  Cataract,BPH, Glucoma. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT