开发者

Debug this c++ program

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 diapla开发者_开发技巧ys 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


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());


v2 = new mercidez;  

should be

v2 = new mercedez; 

for starters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜