我自己在学习c ++。我只是尝试做一个图书馆计划。但有些事情是错的。用户从菜单中选择号码后,程序将关闭。这是我的代码:
这是我的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;
}
这是我的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";
}
}
答案 0 :(得分:0)
您看到的问题在此处得到解答: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;
}
另外,顺便说一句,永远不要将cpp包含在另一个cpp文件中!!你需要使用标题;你应该创建一个名为&#39; menu.h&#39;的文件,其中包含:
#pragma once // or other include guard depending on your setup
class Menu{
public:
int menuchoice();
};