调试这个c ++程序

时间:2010-08-12 19:01:51

标签: c++

我需要帮助来调试这个程序。

// 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;

}

现在当继承的类mercedez试图通过v2-> getdataofvehicle();

来执行车辆的getdataofvehicle

getdataofvehicle函数基类的一部分不接受输入,只需显示“请输入汽车的价格:”并直接跳转到: COUT&LT;&LT; “请输入汽车的价格:”; CIN&GT;&GT;运输及工务局局长(14)&GT;&GT;检查;

任何人都可以调试一下并告诉我为什么会发生这种情况

非常感谢任何帮助

2 个答案:

答案 0 :(得分:3)

问题可能在这里:

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

要使其正常工作,您需要输入“Y&lt; Enter&gt;”使缓冲区冲洗。

但是代码只从输入流中读取“Y”。因此,流仍然包含&lt; Enter&gt;。字符。所以你可以输入第一辆车的数据,但是当你到达第二辆车时,std :: cin上还有数据(&lt; Enter&gt;),这是用std :: getline()读取的,它读取一个空行

要解决这个问题,请先阅读一行:

    cout <<"Vehicle has autostart function ? : (y/n)"; 

    std::string bools;
    std::getline(std::cin, bools);

    autostart  = ((bools == "y") || (bools == "Y"))? yes : no;

我给了你一个线索,你应该自己做一些工作(否则你什么都不学习)。

关于&lt; Enter&gt;的相同规则适用于此。

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

PS:不要在一行上放两个语句。

尝试这样的事情:

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

答案 1 :(得分:1)

v2 = new mercidez;  

应该是

v2 = new mercedez; 

首发。