Question

In: Computer Science

(C++) I'm getting a few errors that prevents the program from running what is causing this?...

(C++) I'm getting a few errors that prevents the program from running what is causing this?

"

routes3.cpp:202:9: warning: add explicit braces to avoid dangling else

[-Wdangling-else]

else {

^

"

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstring>
using namespace std;

class Leg{
const char* const startCity;
const char* const endCity;
const int dist;

public:
Leg(const char* const, const char* const, int);
Leg& operator= (const Leg&);
int getDist() const {return dist;}
void output(ostream&) const;
friend class Route;
friend class ShortestRoute;
};


class Route{
friend class ShortestRoute;
vector<const Leg*> legs;
const int dist;

public:
Route(const Leg&);
Route(const Route&, const Leg&);
Route& operator=(const Route&);
int getDistance() const {return dist;}
void output(ostream&) const;
friend bool operator<(const Route&, const Route&);

};


class ShortestRoute
{
static const int legSize;
static const Leg legs[];

public:
static const Route anyRoute(const char* const, const char* const);
static const Route shortestRoute(const char* const, const char* const);
};

const Leg ShortestRoute::legs[] =
{
Leg("Denver", "Kansas City", 395),

Leg("Philadelphia", "New York", 96),

Leg("Oklahoma City", "Kansas City", 347),

Leg("San Jose", "Las Vegas", 382),

Leg("San Fransisco", "San Jose", 42),

Leg("San Jose", "Los Angeles", 306),

Leg("Albuquerque", "Dallas", 646),

Leg("Pittsburgh", "New York", 371),

Leg("Los Angeles", "Phoenix", 372),

Leg("Phoenix", "Albuquerque", 421),

Leg("Las Vegas", "Albuquerque", 573),

Leg("Sacramento", "Reno", 138),

Leg("Memphis", "Atlanta", 383),

Leg("Albuquerque", "Kansas City", 617),

Leg("Albuquerque", "Oklahoma City", 541),

Leg("Oklahoma City", "St Louis", 498),

Leg("Cincinnati", "Columbus", 102),

Leg("Cincinnati", "Cleveland", 217),

Leg("Oklahoma City", "Memphis", 467),

Leg("Dallas", "Memphis", 451),

Leg("Richmond", "Washington", 109),

Leg("Chicago", "Indianapolis", 183),

Leg("Indianapolis", "Cincinnati", 115),

Leg("Los Angeles", "Las Vegas", 263),

Leg("Los Angeles", "Albuquerque", 789),

Leg("Louisville", "Cincinnati", 100),

Leg("St Louis", "Indianapolis", 243),

Leg("Washington", "Baltimore", 39),

Leg("Baltimore", "Philadelphia", 105),

Leg("Las Vegas", "Denver", 754),

Leg("Las Vegas", "Salt Lake City", 420),

Leg("Reno", "Salt Lake City", 518),

Leg("Salt Lake City", "Denver", 519),

Leg("Kansas City", "Chicago", 510),

Leg("Memphis", "Louisville", 383),

Leg("San Fransisco", "Sacramento", 90),

Leg("Memphis", "Nashville", 210),

Leg("Kansas City", "St Louis", 249),

Leg("Nashville", "Charlotte", 409),

Leg("Atlanta", "Charlotte", 226),

Leg("Detroit", "Cleveland", 169),

Leg("Cleveland", "Pittsburgh", 134),

Leg("Louisville", "Richmond", 563),

Leg("Pittsburgh", "Washington", 241),

Leg("Pittsburgh", "Philadelphia", 304),

Leg("Chicago", "Detroit", 283),

Leg("Columbus", "Pittsburgh", 142),

Leg("Charlotte", "Richmond", 291),

//SF to NY SuperHighway
Leg("San Fransisco", "New York", 21000)
};


int main(){

cout << endl << "Programmer: \n";
cout << "Course: \n";

const Route route1 = ShortestRoute::anyRoute("San Francisco", "New York City");
route1.output(cout);

const Route route2 = ShortestRoute::shortestRoute("San Francisco", "New York City");
route2.output(cout);

return 0;
}

const Route ShortestRoute::anyRoute(const char* const start, const char* const end) {
for (int i = 0; i < ShortestRoute::legSize; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0) {
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start,ShortestRoute::legs[i].startCity),legs[i]);
return x;
}
}
}
throw "Can't find a possible route!";
}

const Route ShortestRoute::shortestRoute(const char* const start, const char* const end){
set<Route> s;
for (int i = 0; i < ShortestRoute::legSize; i++) {
for (int j = 0; j < ShortestRoute::legSize; j++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0)
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start, ShortestRoute::legs[i].startCity), legs[i]);
s.insert(x);
}
}
}
return *(s.begin());
}

bool operator<(const Route& a, const Route& b)
{
return a.dist < b.dist;
}


Leg& Leg::operator=(const Leg& copy){
Leg& host = *this;
if(this != &copy){
const_cast<const char*&>(host.startCity) = copy.startCity;
const_cast<const char*&>(host.endCity) = copy.endCity;
const_cast<int&>(host.dist) = copy.dist;
}
return host;
}


void Leg::output(ostream& os) const{
os << startCity << " to " << endCity << " is " << dist << " miles" << endl;
}

Route::Route(const Leg& leg) : dist(leg.getDist()){
legs.push_back(&leg);
}


Route::Route(const Route& route, const Leg& leg)
: legs(route.legs), dist(route.dist+leg.dist){
if (strcmp(route.legs.back()->endCity, leg.startCity) != 0)
throw "Mismatch";
legs.push_back(&leg);
}


void Route::output(ostream& os) const{
string output = "Route: ";
string temp = " to ";
int totalDist = 0;
for(int i = 0; i < legs.size(); i++){
output += legs[i]->startCity;
if(i != legs.size()-1) {
output += temp;
}
else{
output += (temp + legs[i]->endCity);
}
totalDist += legs[i]->getDist();
}
output += (" "+to_string(totalDist)+" miles\n");
os << output;
}

Route& Route::operator=(const Route& copy){
Route& host = *this;
if(this != &copy){
const_cast<int&>(host.dist) = copy.getDistance();
host.legs = copy.legs;
}
return host;
}

Solutions

Expert Solution

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstring>
using namespace std;

class Leg{
    const char* const startCity;
    const char* const endCity;
    const int dist;

    public:
    Leg(const char*, const char*, int);
    Leg& operator= (const Leg&);
    int getDist() const {return dist;}
    void output(ostream&) const;
    friend class Route;
    friend class ShortestRoute;
};

Leg::Leg(const char* s, const char* e, int d): startCity(s), endCity(e), dist(d) {
  
}


class Route{
friend class ShortestRoute;
vector<const Leg*> legs;
const int dist;

public:
Route(const Leg&);
Route(const Route&, const Leg&);
Route& operator=(const Route&);
int getDistance() const {return dist;}
void output(ostream&) const;
friend bool operator<(const Route&, const Route&);

};


class ShortestRoute
{
static const int legSize;
static const Leg legs[];

public:
static const Route anyRoute(const char* const, const char* const);
static const Route shortestRoute(const char* const, const char* const);
};

const Leg ShortestRoute::legs[] =
{
Leg("Denver", "Kansas City", 395),

Leg("Philadelphia", "New York", 96),

Leg("Oklahoma City", "Kansas City", 347),

Leg("San Jose", "Las Vegas", 382),

Leg("San Fransisco", "San Jose", 42),

Leg("San Jose", "Los Angeles", 306),

Leg("Albuquerque", "Dallas", 646),

Leg("Pittsburgh", "New York", 371),

Leg("Los Angeles", "Phoenix", 372),

Leg("Phoenix", "Albuquerque", 421),

Leg("Las Vegas", "Albuquerque", 573),

Leg("Sacramento", "Reno", 138),

Leg("Memphis", "Atlanta", 383),

Leg("Albuquerque", "Kansas City", 617),

Leg("Albuquerque", "Oklahoma City", 541),

Leg("Oklahoma City", "St Louis", 498),

Leg("Cincinnati", "Columbus", 102),

Leg("Cincinnati", "Cleveland", 217),

Leg("Oklahoma City", "Memphis", 467),

Leg("Dallas", "Memphis", 451),

Leg("Richmond", "Washington", 109),

Leg("Chicago", "Indianapolis", 183),

Leg("Indianapolis", "Cincinnati", 115),

Leg("Los Angeles", "Las Vegas", 263),

Leg("Los Angeles", "Albuquerque", 789),

Leg("Louisville", "Cincinnati", 100),

Leg("St Louis", "Indianapolis", 243),

Leg("Washington", "Baltimore", 39),

Leg("Baltimore", "Philadelphia", 105),

Leg("Las Vegas", "Denver", 754),

Leg("Las Vegas", "Salt Lake City", 420),

Leg("Reno", "Salt Lake City", 518),

Leg("Salt Lake City", "Denver", 519),

Leg("Kansas City", "Chicago", 510),

Leg("Memphis", "Louisville", 383),

Leg("San Fransisco", "Sacramento", 90),

Leg("Memphis", "Nashville", 210),

Leg("Kansas City", "St Louis", 249),

Leg("Nashville", "Charlotte", 409),

Leg("Atlanta", "Charlotte", 226),

Leg("Detroit", "Cleveland", 169),

Leg("Cleveland", "Pittsburgh", 134),

Leg("Louisville", "Richmond", 563),

Leg("Pittsburgh", "Washington", 241),

Leg("Pittsburgh", "Philadelphia", 304),

Leg("Chicago", "Detroit", 283),

Leg("Columbus", "Pittsburgh", 142),

Leg("Charlotte", "Richmond", 291),

//SF to NY SuperHighway
Leg("San Fransisco", "New York", 21000)
};


int main(){

cout << endl << "Programmer: \n";
cout << "Course: \n";

const Route route1 = ShortestRoute::anyRoute("San Francisco", "New York City");
route1.output(cout);

const Route route2 = ShortestRoute::shortestRoute("San Francisco", "New York City");
route2.output(cout);

return 0;
}

const Route ShortestRoute::anyRoute(const char* const start, const char* const end) {
int SIZE = sizeof(ShortestRoute::legs)/sizeof(ShortestRoute::legs[0]);

for (int i = 0; i < SIZE; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0) {
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start,ShortestRoute::legs[i].startCity),legs[i]);
return x;
}
}
}
throw "Can't find a possible route!";
}

const Route ShortestRoute::shortestRoute(const char* const start, const char* const end){
int SIZE = sizeof(ShortestRoute::legs)/sizeof(ShortestRoute::legs[0]);
set<Route> s;
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE; i++) {
if (strcmp(ShortestRoute::legs[i].endCity, end) == 0) {
if (strcmp(ShortestRoute::legs[i].startCity, start) == 0) {
Route r(legs[i]);
return r;
}
else {
Route x(ShortestRoute::anyRoute(start, ShortestRoute::legs[i].startCity), legs[i]);
s.insert(x);
}
}
}
}
return *(s.begin());
}

bool operator<(const Route& a, const Route& b)
{
return a.dist < b.dist;
}


Leg& Leg::operator=(const Leg& copy){
Leg& host = *this;
if(this != ©){
const_cast<const char*&>(host.startCity) = copy.startCity;
const_cast<const char*&>(host.endCity) = copy.endCity;
const_cast<int&>(host.dist) = copy.dist;
}
return host;
}


void Leg::output(ostream& os) const{
os << startCity << " to " << endCity << " is " << dist << " miles" << endl;
}

Route::Route(const Leg& leg) : dist(leg.getDist()){
legs.push_back(&leg);
}


Route::Route(const Route& route, const Leg& leg)
: legs(route.legs), dist(route.dist+leg.dist){
if (strcmp(route.legs.back()->endCity, leg.startCity) != 0)
throw "Mismatch";
legs.push_back(&leg);
}


void Route::output(ostream& os) const{
string output = "Route: ";
string temp = " to ";
int totalDist = 0;
for(unsigned int i = 0; i < legs.size(); i++){
output += legs[i]->startCity;
if(i != legs.size()-1) {
output += temp;
}
else{
output += (temp + legs[i]->endCity);
}
totalDist += legs[i]->getDist();
}
output += (" "+to_string(totalDist)+" miles\n");
os << output;
}

Route& Route::operator=(const Route& copy){
Route& host = *this;
if(this != ©){
const_cast<int&>(host.dist) = copy.getDistance();
host.legs = copy.legs;
}
return host;
}

**************************************************

The first warning is because eevry if statement must have a pair of curly braces after it.. If you avoid it, it becomes confusing.. Hence the error.. I have corrected it.. The other error says: "Leg::Leg(char const*, char const*, int)" is not defined, which means, you have declared a constructor for Leg class in your class declaration, but never provided its definition in file...

For you reference,

void output(ostream&) const; => this is a declaration in Leg class..

void Leg::output(ostream& os) const{ => This is he definition of method of Leg class, which is later after declaration

===========================================================

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that ItemToPurchase("Bottled Water", "Deer Park, 12 oz.", 1, 10) correctly initializes item compilation failed 2) Tests default constructor and accessors for ShoppingCart Compilation failed 3)Tests ShoppingCart("John Doe", "February 1, 2016") correctly initializes cart Compilation failed 4) Tests that getNumItemsInCart() returns 6 (ShoppingCart) compilation failed 5) Test that getCostOfCart() returns 9 (ShoppingCart) compilation failed Complete program: itemtopurchase.java public class ItemToPurchase { // instance variables private String...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues as far as displaying programmers only portion and the average salary of all the employees. Please make any changes or comments ! Employee.h file: #ifndef EMPLOYEE_H #define EMPLOYEE_H #include using namespace std; enum Role {programmer, manager, director}; struct Employee { std::string firstName; std::string lastName; int SSN; std::string department; double salary; Role role; }; #endif #ifndef NODE_H #define NODE_H Node.h file: #include "Employee.h" typedef Employee...
I working on this program in C++ and I keep getting 20 errors of the same...
I working on this program in C++ and I keep getting 20 errors of the same type again.cpp:36:11: error: use of undeclared identifier 'Polynomial' int main() { // create a list of polinomials vector<Polynomial> polynomials; // welcome message cout << "Welcome to Polynomial Calculator" << endl; int option = 0; while (option != 6) { // display menu displayMenu(); // get user input; cin >> option; if (option == 1) { cout << "Enter a polynomial :" << endl; string...
I'm getting a conversion issue with this program. When I ran this program using the Windows...
I'm getting a conversion issue with this program. When I ran this program using the Windows command prompt it compiles and runs without issues. Although, when I run it using the zyBooks compiler it keeps giving me these conversion errors in the following lines: main.c: In function ‘main’: main.c:53:24: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] averageCpi += cpi[i] * instructionCount[i] * Million; main.c:57:29: error: conversion to ‘float’ from ‘long int’ may alter its value...
q7.1 Fix the errors in the code (in C) //This program should read a string from...
q7.1 Fix the errors in the code (in C) //This program should read a string from the user and print it using a character pointer //The program is setup to use pointer offset notation to get each character of the string #include <stdio.h> #include <string.h> int main(void){ char s[1]; scanf(" %c", s); char *cPtr = s[1]; int i=0; while(1){ printf("%c", cPtr+i); i++; } printf("\n"); }
c++ I am getting errors that say "expected a declaration". can you explain what it means...
c++ I am getting errors that say "expected a declaration". can you explain what it means by this and how I need to fix it?   #include <iostream> using namespace std; //functions void setToZero(int board[8][8]); void displayBoard(int oboard[8][8]); void movingFunction(); int main() {    // chess board 8x8    int board[8][8];    // all of the possible moves of the knight in 2 arrays    int colMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };    int rowMove[8]...
Explain each in a few sentences: a. What is the purpose / goal of running a...
Explain each in a few sentences: a. What is the purpose / goal of running a regression? (2 points) b. What are the regressand, regressor, dependent variable and independent variable in the regression context? (1 point
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
MIPS Program I'm trying to write a program that will take in two numbers from the...
MIPS Program I'm trying to write a program that will take in two numbers from the user and output the sum at the end. However, I keep getting very wrong answers. For example, I add 2 and 2 and get 268501000. Help would be appreciated. .data #starts data use userIn1:    .word 4 #sets aside space for input    userIn2:    .word 4 #sets aside space for input total:    .word 4 #sets space aside for input    request:   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT