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 986635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:21:05+00:00 2026-05-16T05:21:05+00:00

I need help to debug this program . // VirtualFN.cpp : Defines the entry

  • 0

I need help to debug this program .

// VirtualFN.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <iomanip>

using namespace std;
enum isautostart { no,yes };

class vehicle {
protected:
    static int noofvehicles;
    string vehiclename;
    long long int price;
    isautostart autostart;
public:
    vehicle():price(0),autostart(no),vehiclename("N/A")
    {
        noofvehicles++;
    }
    vehicle (vehicle& tempveh){
        vehiclename = tempveh.vehiclename;
        autostart = tempveh.autostart;
        price = tempveh.price;
    }
    virtual void getdataofvehicle(){
        string tempcarname;
        char check[15];
        cout<< "\nPlease enter the name of vehicle :"; 
        getline(cin,tempcarname);
        vehiclename = tempcarname;
        /*******************************************************/

        while(true){
        cout<< "Please enter the price of the car :"; cin>>setw(14)>>check;
        if(atoi(check)){
            price = atoi(check);
            break;}
        else{
            cout<< "you didn't enter the integer number , Try again"<<endl;
        }
        }
        /*******************************************************/
        char bools;
        cout <<"Vehicle has autostart function ? : (y/n)"; cin >> bools;
        if(bools == 'y'){
            autostart = yes;
        }
        else { autostart = no; }
    }
    virtual void displaycardata() {
        cout<< "\nName of vehicle is :" << vehiclename;
        cout<< "\nPrice of the vehicle is : $" <<price;
        if(autostart){
            cout<< "\nThis vehicle is autostart.";}
        else{
            cout<< "\nThis vehicle is not autostart.";}

    }
    static void displaynoofcars(){
        cout<<"\nNo of cars in warehouse are :" << noofvehicles;
    }
};

int vehicle::noofvehicles = 0;

class mercedez : public vehicle {
    string color;
public:
    mercedez(): vehicle() {
        color = "N/A";}
    void getdataofvehicle() {
        vehicle::getdataofvehicle();
        cout<<"\nPlease enter the color of the car :"; 
        cin >> color;
    }
    void displaycardata(){
        vehicle::displaycardata();
        cout<<"\nThe color of the car is:" << color;
    }
};



int main()
{
    vehicle *v1;
    vehicle *v2;
    mercedez a;

    v1 = new vehicle;
    vehicle::displaynoofcars();
    v2 = new mercidez;
    vehicle::displaynoofcars();
    v1->getdataofvehicle();
    v1->displaycardata();
    v2->getdataofvehicle();
    v2->displaycardata();

    getch();

    return 0;

}

now when inherited class mercedez try to execute getdataofvehicle of vehicle by v2->getdataofvehicle();

getdataofvehicle function part of base class doesn’t takes the input, just diaplays the “Please enter the price of the car :” and jumps directly to :
cout<< “Please enter the price of the car :”; cin>>setw(14)>>check;

Can anyone please debug this and tell me why is this happening

Any help is very appreciated

  • 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-05-16T05:21:05+00:00Added an answer on May 16, 2026 at 5:21 am

    The problem is probably here:

        char bools;
        cout <<"Vehicle has autostart function ? : (y/n)"; cin >> bools;
        if(bools == 'y'){
            autostart = yes;
        }
        else { autostart = no; }
    

    To get this to work you need to type “Y<Enter>” to make the buffer flush.

    But the code only reads the “Y” from the input stream. Thus the stream still contains the <Enter> character. So you can enter the data for the first car but when you get to the second car there is still data left on std::cin (the <Enter>) Which is read with std::getline() which reads an empty line.

    To fix this make it read a line:

        cout <<"Vehicle has autostart function ? : (y/n)"; 
    
        std::string bools;
        std::getline(std::cin, bools);
    
        autostart  = ((bools == "y") || (bools == "Y"))? yes : no;
    

    I gave you a clue you are supposed to do some work yourself (otherwise you don’t learn anything).

    Same rule about the <Enter> applies here.

    cout<< "Please enter the price of the car :"; cin>>setw(14)>>check;
    

    PS: Don’t put 2 statements on one line.

    Trying somthing like this:

    cout<< "Please enter the price of the car :";
    
    std::string  priceData;
    std::getline(std::cin, priceData);
    
    // Choice 1: (Best)
    price= boost::lexical_cast<long long int>(priceData);
    
    // Choice 2: (OK) Best if you don't have boost;
    std::stringstream priceStream(priceData);
    priceStream >> price;
    
    // Choice 3: Only if you must use atoi() use it as a last resort.
    price = atoi(priceData.c_str());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.