在过去的 8 个小时里,我一直在搞乱这个程序,我不知道该往哪里走。我很抱歉我的代码是外语的,我尽力用英语评论它。 最后我设法调试了它。但我几乎没有遇到什么问题。 1)如何打印前 5 行(txt 的前 5 行失败)然后再打印前 5 行?下面列出了我相当可怜的尝试。示例: 一开始你有纪念品,教父。你打印下五个 教父 II , 肖申克的救赎, Leon, 苦乐参半的生活,Se7en.Now previous 5:Memento, Godfather,Godfather II, Shawshank Redemption, Leon 2) 如何在程序中使用字符而不是数字来制作菜单? 3)我真的很傻吗?请诚实回答。 我现在休息一下,睡一觉,早上再看回复,希望不要违反规定。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void sisend(); /*input*/
string valik(); /*choice*/
void kogukuvand(); /* the whole display of the database */
void valjumine(); /* program quit*/
void sisendvoivaljumine(); /*quiting with output to file or not*/
void viisjuurde(); /* input further 5 lines from the file */
void viismaha(); /* input previous 5 lines from the file*/
int main()
{
system ("title Filmide andmebaas"); /*Movie database*/
system ("cls");
int valik;
cout << "Vajuta klahv 'j',kui tahad naha jargmist 5 kirjet."<< endl; /* j to get extra 5*/
cout << "Vajuta klahv 'e',kui tahad naha eelmist 5 kirjet."<< endl; /* e for previous 5 lines */
cout << "Vajuta klahv 'v',kui tahad lopetada programmi taitumist."<< endl; /* v for the output */
cin >> valik;
switch (valik) { /*choice*/
case 1:
viisjuurde();
break;
case 2:
viismaha();
break;
case 3:
sisendvoivaljumine();
break;
}
}
void viisjuurde()
{
ifstream filmike("andmebaas.txt");
int n, sum = 5;
while (filmike >> n) {
sum += n;
string film;
string vaadatud;
int aasta;
long int hinne;
system ("CLS");
cout << "Kogu filmide andmebaas"<<endl;
cout << "Film" << " " << "Aasta" << " " <<"Hinne"<< " " << "Vaadatud" << endl;
cout << "------------------------------" << endl;
while (filmike>> film >> aasta >> hinne >> vaadatud)
{
cout << film << " " << aasta << " " << hinne << " " << vaadatud << endl;
}
system ("pause");
cin.get();
main();
}
}
void viismaha()
{
ifstream filmike("andmebaas.txt");
int n, sum = -5;
while (filmike >> n) {
sum += n;
}
string film;
string vaadatud;
int aasta;
long int hinne;
system ("CLS");
cout << "Kogu filmide andmebaas"<<endl;
cout << "Film" << " " << "Aasta" << " " <<"Hinne"<< " " << "Vaadatud" << endl;
cout << "------------------------------" << endl;
while (filmike>> film >> aasta >> hinne >> vaadatud)
{
cout << film << " " << aasta << " " << hinne << " " << vaadatud << endl;
}
system ("pause");
cin.get();
main();
}
void sisend() /* user input */
{
string nimi;
int aasta;
long int hinne;
string vaadatud;
ofstream lisafilm("andmebaas.txt", ios::app);
cout <<"Sisestage filmi nimi." << endl; /*Enter film's name */
cin >> nimi;
string film;
cout << "Sisestage filmi ilmumisaasta." << endl; /* Enter film's year */
cin >> aasta;
cout << "Sisestage filmi hinne." << endl; /* Enter film's rating */
cin >> hinne;
cout << "Kas olete filmi juba vaadanud?" << endl; /* Has the user seen the movie already*/
cout << "Vastake 'Jah' voi 'Ei'" << endl;
cin >> vaadatud;
cout << film << " " << aasta << " " << hinne << " " << vaadatud << endl;
lisafilm<< nimi << " " << aasta << " " << hinne<< " " << vaadatud << endl;
lisafilm.close();
main();
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
void valjumine() /*quiting without output */
{
system("CLS");
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
void kogukuvand() /* the whole movie database*/
{
ifstream filmike("andmebaas.txt");
string film;
string vaadatud;
int aasta;
long int hinne;
system ("CLS");
cout << "Kogu filmide andmebaas"<<endl;
cout << "Film" << " " << "Aasta" << " " <<"Hinne"<< " " << "Vaadatud" << endl;
cout << "------------------------------" << endl;
while (filmike>> film >> aasta >> hinne >> vaadatud)
{
cout << film << " " << aasta << " " << hinne << " " << vaadatud << endl;
}
system ("pause");
cin.get();
main();
}
void sisendvoivaljumine()
{
int valik2;
system ("CLS");
cout << "Vajuta klahv 'j',kui tahad salvestada kirje ja v2ljuda programmist." << endl; /* output new movie to file and quit*/
cout << "Vajuta klahv 'e',kui ei taha salvestada kirje ja v2ljuda programmist." << endl; /*just quit, without any output to file*/
cout << "Vajuta klahv 'k',kui tahad naha kogu filmide andmebaasi." << endl; /* display the whole movie database*/
cin >> valik2;
switch (valik2)
{
case 1:
sisendvoivaljumine();
break;
sisend();
break;
case 2:
valjumine();
break;
case 3:
kogukuvand();
break;
}
}
最佳答案
一些提示:
根据通用约定缩进和格式化您的代码。它使您(以及其他所有人)更容易阅读和调试。
您正在递归调用 main()。不要那样做。请改用围绕菜单切换的循环。
从文件中读取行的一个很好的故障保护方法是使用 getline()。结合 istringstream 以从行中读取单个值:
ifstream infile("somefile.txt"); string line; while ( getline(infile, line) ) // read infile until eof { istringstream iss(line); // create stream from line data string a, b, c, d; iss >> a >> b >> c >> d; // read just as from cin cout << a << "," << b << "," << c << "," << d << endl; }
如果您读取整数,您需要验证读取是否成功(除非您确定文件在您读取的位置包含数字):
int n; if (iss >> n) { // read OK } else { // read failed infile.clear(); string junk; infile >> junk; cout << "ERROR: bad data was:" << junk << endl; }
要在菜单中使用字符,您可以使用 char 数据类型。这是个好主意,因为字符输入不会失败:
char choice; cin >> choice; switch (choice) { case '1': { // do something break; } case 'A': { // do something break; } }
请注意,当您选择该选项时,
sisendvoivaljumine
会递归调用自身。由于可以无限次地选择菜单,因此最好使用循环来重复菜单。
您想阅读以上主题。检查http://www.cplusplus.com . 调试时,请确保您确切知道程序在每一点执行的是哪几行。添加清晰的调试输出以帮助您。
关于c++ - C++中的数据库问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468341/