簡體   English   中英

調試這個c ++程序

[英]Debug this c++ 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;

}

現在當繼承的類mercedez嘗試通過v2-> getdataofvehicle()執行車輛的getdataofvehicle時;

getdataofvehicle函數基類的一部分不接受輸入,只需顯示“請輸入汽車的價格:”並直接跳轉到:cout <<“請輸入汽車的價格:”; CIN >>運輸及工務局局長(14)>>檢查;

任何人都可以調試這個並告訴我為什么會發生這種情況

非常感謝任何幫助

問題可能出在這里:

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

要使其工作,您需要鍵入“Y <Enter>”以使緩沖區刷新。

但代碼只從輸入流中讀取“Y”。 因此,流仍包含<Enter>字符。 所以你可以輸入第一輛車的數據但是當你到達第二輛車時,仍然有數據留在std :: cin(<Enter>),這是用std :: getline()讀取的,它讀取一個空行。

要解決此問題,請閱讀以下內容:

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

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

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

我給了你一個線索,你應該自己做一些工作(否則你什么都學不到)。

關於<Enter>的相同規則適用於此處。

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());
v2 = new mercidez;  

應該

v2 = new mercedez; 

對於初學者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM