0

I am learning c++ by myself. I just try to do a library program. But something is wrong. after the user select number from menu, program turns off. Here is my code:

Here is my menu.cpp:

using namespace std;
class Menu{
    public:
        int menuchoice();
};

int Menu::menuchoice()
{
    int choice;
    cout<<"1 - Kitaplarim"<<endl;
    cout<<"2 - Kitap Ekle"<<endl;
    cout<<"3 - About coder"<<endl;
    cout<<"4 - Exit"<<endl;
    cout<<"Bir secenek secin";
    cin>>choice;
    return choice;
}

Here is my main.cpp

#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<string>
#include "menu.cpp"
using namespace std;


class Kitap{
    public:
        string Ad;
        void takeinfobook();

};

void Kitap::takeinfobook(){

    cout<<"Kitabin adi...:";
    std::getline(std::cin, Ad);
    ofstream savefile("savebook.txt");
    savefile<<Ad;
    cout<<Ad;

}
main(){
    Menu menu;
    int choice = menu.menuchoice();
    if(choice==2)
    {
        Kitap book;
        book.takeinfobook();// After this line program must take me a book name and write to file. But it doesnt. Program turns off..
    }
    else
    {
        cout<<"hodor";
        }
}
Ada Karcı
  • 39
  • 7

1 Answers1

0

The issue you are seeing is answered here: Need help with getline()

void Kitap::takeinfobook(){

    cout << "Kitabin adi...:";
    ws(cin);  // <---       Add this to make getline block.
    std::getline(std::cin, Ad);
    ofstream savefile("savebook.txt");
    savefile << Ad;
    cout << Ad;

}

Also, as an aside, never include a cpp into another cpp file!! You need to use a header; you should create a file called 'menu.h', containing:

#pragma once // or other include guard depending on your setup
class Menu{
public:
    int menuchoice();
};
Community
  • 1
  • 1
VoidStar
  • 5,241
  • 1
  • 31
  • 45