привет, ребята, я пишу код, и мой файл выводит несколько слов дважды? helpМой файл выводит на экран дважды C++?
это методы класса. Я звоню в основной программе. список создания в порядке, но отображение - это проблема с проблемой.
Он отображает несколько элементов дважды и в основном элементы последней записи.
void Books ::createList()
{
ofstream outfile;
outfile.open("bookshop.txt",ios::out);
cout << "enter the size of list" << endl;
cin >> n;
if (n <= 0)
{
cout << "invalid entry or list empty" << endl;
}///end if.
else
{
i = 1 ;
if (i > n)
tmp1Ptr->next = NULL;
else
cout << "enter value for the list" << endl;
for(i=1; i<=n ; i++)
{
cin.ignore();
cout << "enter the author" << endl;
getline(cin , authr);
cout <<endl << "enter the title" << endl;
getline(cin , titl);
cout << endl << "enter the publisher" << endl;
getline(cin , publishr);
cout << endl<< "enter the price" << endl;
cin >> pric;
cout << endl << "enter the stock position" << endl;
cin >> stoc;
tmp2Ptr = new Book;
tmp2Ptr->author = authr ;
tmp2Ptr->title = titl ;
tmp2Ptr->publisher = publishr ;
tmp2Ptr->price = pric ;
tmp2Ptr->stock = stoc ;
outfile<<tmp2Ptr->author << endl;
outfile<<tmp2Ptr->title <<endl;
outfile<<tmp2Ptr->publisher <<endl;
outfile<<tmp2Ptr->price <<endl;
outfile<<tmp2Ptr->stock <<endl;
if (i == 1)
{
headPtr = tmp2Ptr;
tmp1Ptr = headPtr ;
tmp1Ptr->prev = NULL;
}///end inner if
else
{
tmp1Ptr->next = tmp2Ptr;
tmp2Ptr->prev = tmp1Ptr;
tmp1Ptr = tmp2Ptr;
}///end inner else.
}///end for
tmp1Ptr->next = NULL;
tmp1Ptr = NULL;
tmp2Ptr = NULL;
cout << endl;
}
outfile.close();
}///end createList
void Books ::displayDetails()
{
ifstream infile;
infile.open("bookshop.txt", ios::in);
cout << "contents of list are:" << endl;
while (infile.good())
{
string priz;
string stok;
getline(infile,authr1);
getline(infile,titl1);
getline(infile,publishr1);
getline(infile,priz);
getline(infile,stok);
cout<< authr1 << endl;
cout<< titl1 << endl;
cout<< publishr1 << endl;
cout << priz << endl << stok << endl;
}
infile.close();
}
Прежде чем обращаться за помощью, пожалуйста, уменьшите свой код до минимума и удалите устаревшие пробелы и т. Д. Это побуждает людей помочь вам. –
@joachin pile borg. работа getline работала, и если я хочу искать из файла название и автора, как это сделать и выложить полную информацию об определенном заголовке на экране – hardeep