0

I have a big problem... Just run the program and go to "New Gun", then "Handgun". The problem comes when you have to enter the model. For example, if i enter "Desert Eagle", in the text file it outputs only "Eagle". Its strange, and i cant solve it.

code:

#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
    Because `cin >> gunModel;` gets the "Desert" and then `getline(cin, gunModel);` gets "Eagle". Try to remove `cin >> gunModel;`. – DimChtz Aug 15 '16 at 16:18
  • Remove the cin, and add the getline? Yup, i tried but take a look what happens... When i go to add a gun it misses "Model: " and just starts at the manufacturing year... –  Aug 15 '16 at 16:25
  • Edit your question and implement DimChtz suggestion. You just have to remove one line. – Barmak Shemirani Aug 15 '16 at 16:32
  • Remove the cin, and add the getline? Yup, i tried but take a look what happens... When i go to add a gun it misses "Model: " and just starts at the manufacturing year...Im telling it again –  Aug 15 '16 at 16:34
  • Just try the code and you will see –  Aug 15 '16 at 16:35
  • @KristianPanov After you remove `cin >> gunModel;` see: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – NathanOliver Aug 15 '16 at 16:38

1 Answers1

2

Remove >> operator as suggested. Use clear and ignore to clear errors skip new line characters \n

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;

See also Why would we call cin.clear() and cin.ignore() after reading input?

Community
  • 1
  • 1
Barmak Shemirani
  • 26,741
  • 4
  • 33
  • 61