2017-10-30 12 views
1

私のプログラムの内容はこの質問には関係ありませんが、情報を保存するためにクラスやオブジェクトを使用しています。特定のコード行は実行されません

#include <iostream> 
using namespace std; 

class Car 
{ 
private: 
    int carNumber; 
    bool loaded; 
    string reportingMark, kind, destination; 
public: 
    void setUp(string reportingMark, int carNumber, string kind, bool 
loaded, string destination); 
    void output(); 
}; 

void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination); 

/* ******************** main ******************** 
*/ 
int main() 
{ 
    Car *ptr; 

    string reportingMark, kind, destination; 
    int carNumber; 
    bool loaded; 

    Car c = *new Car(); 
    ptr = &c; 
    input(reportingMark, carNumber, kind, loaded, destination); 
    ptr->setUp(reportingMark, carNumber, kind, loaded, destination); 
    ptr->output(); 
    return 0; 
} 
/* ******************** Member Functions ******************** 
*/ 

/* ******************** setUp ******************** 
Takes 5 parameters by value: reporting mark, car number, kind, loaded, 
and destination 
Inputs these paramaters into the Car class' variables 
*/ 
void Car::setUp(string rm, int cn, string k, bool l, string d) 
{ 
    reportingMark = rm, carNumber = cn, kind = k, loaded = l, 
destination = d; 
} 

/* ******************** output ******************** 
Prints the reporting mark, car number, kind, loaded, and destination 
in a neat format 
*/ 
void Car::output() 
{ 
    cout << "\n\nReporting Mark: " << reportingMark << "\nCar Number: " 
<< carNumber << "\nKind: " << kind << "\nCar is loaded: " << loaded << 
"\Destination: " << destination << endl; 
} 
/* ******************** Global Functions ******************** 
*/ 

/* ******************** input ******************** 
Takes the reporting mark, car number, kind, loaded, and destination as 
reference parameters 
Asks the user to input these values 
*/ 
void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination) 
{ 
    do 
    { 
     cout << "What is the Reporting Mark? (Between 2-4 characters) "; 
     getline(cin, reportingMark); 
    if(reportingMark.length() < 2 || reportingMark.length() > 4) cout << "invalid input, try again\n"; 
} while(reportingMark.length() < 2 || reportingMark.length() > 4); 
cout << "What is the Car Number? "; 
cin >> carNumber; 
int carNumResponse; 

do 
{ 
    cout << "What is the Kind? (Choose '1' for business, '2' for maintenance, or '3' for other) "; 
    cin >> carNumResponse; 
    if(carNumResponse < 1 || carNumResponse > 3) cout << "invalid input, try again\n"; 
} while(carNumResponse < 1 || carNumResponse > 3); 
if(carNumResponse == 1) kind = "business"; 
if(carNumResponse == 2) kind = "maintenance"; 
if(carNumResponse == 3) kind = "other"; 

int loadedResponse; 

do 
{ 
    cout << "Is the car loaded? (Type '1' if true and '2' if false) "; 
    cin >> loadedResponse; 
    if(loadedResponse < 1 || loadedResponse > 2) cout << "invalid input, try again\n"; 
} while(loadedResponse < 1 || loadedResponse > 2); 
if(loadedResponse == 1) loaded = true; 
if(loadedResponse == 2) loaded = false; 

cout << "What is the Destination? "; 
getline(cin, destination); 
} 

を私のプログラムの最後の2行:

cout << "What is the Destination? "; 
getline(cin, destination); 

は私にいくつかの理由のために何も入力させませんが、以下のように私のコードです。 getlineを間違って使用していますか?これはプログラムの他の部分でも同じように使用されます。誰にも助けてくれてありがとう!

+0

何を意味するかを "実行しない" のでしょうか?あなたは画面に書き込まれていますか? getlineは失敗しますか?両方とも失敗するのですか?エラーが出ていますか? – xyious

+0

私はそれを編集しました。基本的には、「目的地は何ですか?」というメッセージが表示されます。それは私に応答を入力させません。 –

+0

なぜ動的に変数を割り当てていますか? C++!= Javaでは、クラスのインスタンスを割り当てるために動的メモリを使用する必要はありません。 –

答えて

0

ここでの問題は、getline操作を実行する前にcin>>があることです。 cin>>は、ストリームからすべての文字を抽出しますが、改行文字のままにしておきます。

getlineの場合、デフォルトの区切り文字は改行文字です。したがって、ストリームに残った改行文字を受け取るので、入力をスキップします。

解決策:cin.ignore()を実行する必要があります。これはストリームをフラッシュします。つまり、改行文字は削除されます。 getline()機能を使用する前にcin.ignore()を実行することをおすすめします。

属性:ポストから ヘルプ:How to use cin.getline in blocking mode

関連する問題