2010-08-12 55 views
1

我需要幫助來調試此程序。調試此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; 

} 
現在

當繼承的類梅塞德斯嘗試通過V2-執行車輛的getdataofvehicle> getdataofvehicle();基類的

getdataofvehicle功能部分並不需要輸入時,僅diaplays「請輸入汽車的價格」,並直接跳轉到: COUT < <「請輸入汽車的價格:」; CIN >>運輸及工務局局長(14)>>檢查;

誰能請調試這一點,並告訴我這是爲什麼發生的

任何幫助是非常讚賞

+2

'長的長整型價格;'哇'那些可以mercidez' O_O是 – Dmitry 2010-08-12 19:11:31

+1

@Carl Norum時貴:這不是我的功課,我正在學習C++,使我可以和maya api一起工作(我是一名學習gfx藝術家) @Dmitry:是的,他們對我來說至少是大聲笑 – M3taSpl0it 2010-08-12 19:24:04

+0

小建議:使用C++ caml-case風格命名事物。 getDataOfVehicle比getdataofvehicle更易於閱讀。 – 2010-08-12 19:51:26

回答

3

這個問題可能是在這裏:

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

爲了得到這個工作,你需要鍵入「Y <輸入>」,使緩衝區刷新。

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

爲了解決這個問題,使其讀取一行:

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

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

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

我給你,你應該做一些工作你自己(否則你沒有學到任何東西)的線索。

關於<的相同規則輸入>適用於此處。注意:不要把2條語句放在一行上。

這樣的嘗試財產以後:

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

如果你在任何事物和三元操作符之前用好的std ::'s提交它,你會讓他陷入作弊的困境;)聰明。 – samandmoore 2010-08-12 19:25:57

+0

這個事情有所幫助,但現在它沒有在「車輛具有自動啓動功能?:(y/n)「,直接跳轉到下一條語句:( – M3taSpl0it 2010-08-12 19:33:10

+0

@ user411102:已更新的答案 – 2010-08-12 19:46:29

1
v2 = new mercidez; 

應該

v2 = new mercedez; 

對於初學者。

+4

或者'mercedes' .. – Potatoswatter 2010-08-12 20:59:42