Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:08:56+00:00 2026-06-11T23:08:56+00:00

please have a look at the following code #include <iostream> #include <iomanip> #include <vector>

  • 0

please have a look at the following code

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int static carNumber = 1; //Counts the car number
static int vectorLocation = 0; // used to get the vector location
double total=0; // total amount of charges

vector<double>hoursVector; //tracks each car's parkes hour
vector<double>chargeVector; //tracks each charge
vector<int>carVector; //tracks each car number


double calculateCharge(double numberOfHours);
void printData();
void insertIntoVector(double hours, double charges);

int main()
{
    cout << "Start entering data. -1 to exit \n\n " << endl;
    double numberOfHours=0;

    while(true)
    {
        cout << "Enter Number Of Hours"<< endl;
        cin >> numberOfHours;

        if(numberOfHours==-1)
        {
            break;
        }
        else
        {
            total = total + calculateCharge(numberOfHours);
        }
    }

    printData();

}



//This code will calculate the charge values

double calculateCharge(double numberOfHours)
{

    double charge = 0;
    double extraHours = 0;
    double extraCharge = 0;



    if(numberOfHours<=3)
    {
        charge = 2;
        insertIntoVector( numberOfHours, charge);
    }
    else if(numberOfHours>3 && numberOfHours<24)
    {
        extraHours = numberOfHours-3;
        extraCharge = extraHours * 0.5;

        charge = 2+extraCharge;

        insertIntoVector( numberOfHours, charge);
    }
    else if(numberOfHours==24)
    {
        charge = 10;

        insertIntoVector( numberOfHours, charge);
    }
    else if(numberOfHours>24)
    {
        charge = 0;

        insertIntoVector( numberOfHours, charge);
    }

    return charge;


}


//This code is used to enter data into vectors
void insertIntoVector(double hours, double charges)
{
    hoursVector[vectorLocation] = hours;
    chargeVector[vectorLocation] = charges;
    carVector[vectorLocation] = carNumber++;

    vectorLocation++;
    carNumber++;
}


//This method is used to print data
void printData()
{
    cout << "Car"<< setw(6)<< "Hours" << setw(6) << "Charge" << endl;

    for(size_t i=0;i<hoursVector.size();i++)
    {
        cout << carVector[i] << setprecision(2) << fixed << setw(6) << hoursVector[i] << setw(6) << chargeVector[i] << endl;
    }
}

In here, after giving 1 data inside the while loop, the program terminates giving the error

RUN FAILED (exit value 1, total time: 5s)

I can’t understand why. I am new to C++ and learning it by my self. Please help me to correct this code and run it without an issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:08:57+00:00Added an answer on June 11, 2026 at 11:08 pm
    hoursVector[vectorLocation] = hours;
    chargeVector[vectorLocation] = charges;
    carVector[vectorLocation] = carNumber++;
    vectorLocation++;
    

    It is invalid way to insert into vector. You should do something like this:

    hoursVector.push_back( hours );
    chargeVector.push_back( charges );
    carVector.push_back( carNumber++ );
    

    The std::vector itself holds info about memory block he allocated; size of block; size of data currently stored and so on. When needed, it is extending memory block to allow you to push new values. So when you use indexes you simply run out of allocated memory block (in this case). It is not valid way to to add values into vector anyway, so you must replace with matching methods. See link to get help about std::vector’s methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int
Please have a look at the following code. Main.cpp #include <iostream> #include Maximum.h using
Please have a look at the following code #include <iostream> #include <iomanip> #include <cmath>
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Have a look at the following code snippet... File1.h void somefunc(int); File1.c #include File1.h
I have the following code: #include <iostream> #include <string> void main() { std::string str;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.