2016-08-15 10 views
0

私は大きな問題があります... プログラムを実行して "New Gun"、次に "Handgun"に行きます。 モデルを入力する必要があるときに問題が発生します。 たとえば、「Desert Eagle」と入力すると、テキストファイルに「Eagle」のみが出力されます。その奇妙な、私はそれを解決することはできません。C++は文字列から一行全体を読む

コード:

#include <fstream> 
#include <iostream> 
#include <windows.h> 
#include <string> 
#include <algorithm> 
#include <sstream> 
using namespace std; 

void setcolor(unsigned short color) 
{ 
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); 
SetConsoleTextAttribute(hcon,color); 
} 

int main(){ 

system("chcp 1251 > nul"); 
system("title GunnZone"); 


string gunModel; 
int gunManufactureYear; 
int itemNumber; 
double gunWeight; 
double price; 

ofstream myfileHandguns("Handguns.txt", ios::app); 
ofstream myfileRifles("Rifles.txt", ios::app); 
ofstream myfileShotguns("Shotguns.txt", ios::app); 
ofstream myfileSnipers("Snipers.txt", ios::app); 
ofstream myfileGranades("Granades.txt", ios::app); 
ofstream myfileExplosives("Explosives.txt", ios::app); 


int choice; 
cout << "1.New Gun" << endl; 
cout << "2.List Guns" << endl; 
cout << "3.Exit" << endl << endl; 
cout << "Enter your choice: "; 
cin >> choice; 

if(choice == 1){ 

    system("cls"); 

    int gunTypeChoice; 
    cout << "1.Handgun" << endl; 
    cout << "2.Rifle" << endl; 
    cout << "3.Shotgun" << endl; 
    cout << "4.Sniper" << endl; 
    cout << "5.Granade" << endl; 
    cout << "6.Explosives" << endl << endl; 
    cout << "Enter your choice: "; 
    cin >> gunTypeChoice; 

    if(gunTypeChoice == 1){ 

     system("cls"); 

     cout << "Model: "; 
     cin >> gunModel; 
     getline(cin, gunModel); 
     cout << endl << endl; 

     cout << "Year of Manufacture: "; 
     cin >> gunManufactureYear; 
     cout << endl << endl; 

     cout << "Weight: "; 
     cin >> gunWeight; 
     cout << endl << endl; 

     cout << "Item Number: "; 
     cin >> itemNumber; 
     cout << endl << endl; 

     cout << "Price: "; 
     cin >> price; 


     myfileHandguns << "Model: " << gunModel << "\n\n"; 
     myfileHandguns << "Year of Manufacture: " << gunManufactureYear << "\n\n"; 
     myfileHandguns << "Weight: " << gunWeight << " g" << "\n\n"; 
     myfileHandguns << "Item Number: " << itemNumber << "\n\n"; 
     myfileHandguns << "Price: " << price << "$" << "\n\n"; 
     myfileHandguns.close(); 
    } 

} 

system("pause > nul"); 
return 0; 

}

+3

CIN >> gunModel'ので参照してください\nをスキップクリアするclearignoreを使用します"鷲"。 'cin >> gunModel;を削除してください。 – DimChtz

+0

cinを削除し、getlineを追加しますか? うん、私はしようとしましたが、何が起こるかを見てみましょう... 私は銃を追加するために行くときに "モデル"を逃して、ちょうど製造年に始まります... –

+0

あなたの質問を編集してDimChtzの提案を実装してください。あなたは1行を削除するだけです。 –

答えて

2

示唆されるように>>オペレータを削除。 `取得;` "砂漠" を取得し、その後、 `getlineの(CIN、gunModel);エラーが改行文字

cout << "Model: "; 
//cin >> gunModel; 
std::cin.clear(); 
std::cin.ignore(0xffff, '\n'); 
std::getline(cin, gunModel); 
cout << "Testing... you entered " << gunModel << endl; 
cout << endl << endl; 

もがWhy would we call cin.clear() and cin.ignore() after reading input?

+0

本当に本当にありがとう! 今すぐ入手します。 –

+0

チェックマークをクリックすると、回答を受け入れることができます –

関連する問題