意外返回主函数和排序字符串函数
Unexpectedly Returning the Main Function and Sorting Strings Function
我不知道为什么,但这个程序的行为很奇怪,并返回我用2或3倍的主函数插入的所有内容,我不知道问题是什么。除此之外,我不知道如何使用排序选项与字符串。我想写一个函数来对书名进行排序。我需要将这个函数(排序函数)添加到main函数中。任何帮助都将非常感激。这是一个程序,它获取书名,作者,译者,ISBN和主题,并搜索或报告它们。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Library
{
string Book_Name;
string Author;
string Translator;
string ISBN;
string Subject;
struct Library *fl, *bl;
}*start, *cur, *p;
void insert()
{
p = new struct Library;
p->fl = NULL;
p->bl = cur;
cur->fl = p;
cur = p;
cout << "Enter the specified informations for Books" << endl;
cout << endl;
cout << "The Name of the Book " << endl;
getline(cin, p->Book_Name);
cin.ignore();
cout << "Author" << endl;
getline(cin, p->Author);
cin.ignore();
cout << "The Name of the Translator " << endl;
getline(cin, p->Translator);
cin.ignore();
cout << "International Standard Book Number (ISBN) " << endl;
getline(cin, p->ISBN);
cin.ignore();
cout << "Enter the Subject of the Book " << endl;
getline(cin, p->Subject);
cin.ignore();
}
void report_number_1()
{
cout << "The list of all Books in Library are as below" << endl;
for (p = start->fl; p != NULL; p = p->fl)
{
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void delete_number_1()
{
struct Library *ap, *bp;
char is[15];
int sw = 0;
cout << "Enter ISBN" << endl;
gets_s(is);
for (p = start->fl; p != NULL&&!sw; p = p->fl)
{
if (p->ISBN == is)
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void report_number_2()
{
string title;
int sw = 0;
cout << "Enter Book's Title " << endl;
getline(cin,title);
for (p = start->fl; p != NULL; p = p->fl)
{
if (p->Subject == title)
{
sw = 1;
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
}
void delete_number_2()
{
struct Library *ap, *bp;
string name;
int sw = 0;
cout << "Enter Author's Name or the Translator's Name so that search begins and delete" << endl;
getline(cin,name);
for (p = start->fl; p != NULL; p->fl = p)
{
if ((p->Author == name) || (p->Translator == name))
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
delete(p);
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
void main()
{
char ch;
start = new struct Library;
start->fl = NULL;
start->bl = NULL;
cur = start;
do
{
cout << "Enter I/i for Insert " << endl;
cout << "Enter R/r for Report that is Sorted by Name of the Book " << endl;
cout << "Enter S/s for Search by ISBN and delete the Specific Book " << endl;
cout << "Enter U/u for search " << endl;
cout << "Enter W/w to delete the Specific Book" << endl;
cout << "Enter X/x for Terminating the Program " << endl;
cin >> ch;
switch (ch)
{
case 'I':
case 'i':
insert();
break;
case'R':
case'r':
report_number_1();
case 'S':
case 's':
delete_number_1();
break;
case 'U':
case 'u':
report_number_2();
break;
case 'W':
case 'w':
delete_number_2();
break;
}
} while (ch != 'X' && ch != 'x');
}
我假设在line
for (p = start->fl; p != NULL; p->fl = p)
在delete_number_2()中for循环的最后一部分应该是
p = p->fl
相关文章:
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 用于合并排序的合并函数
- 如何计算此排序函数的时间复杂度?
- 什么是自定义比较器以及如何在 C++ 的排序函数中使用它?
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 自定义排序函数中的堆溢出
- 分段 排序函数实现中的错误
- 使用模板化分配器和对向量进行排序的函数
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 对没有比较器或λ函数的向量进行排序?
- C++中的析构函数和构造函数排序
- 数组中的函数排序错误
- 如何命名比较谓词函数排序向量
- 按lexicographical_compare()函数排序
- 构造函数排序(全局作用域)问题
- 按接受c++中参数的函数排序
- 函数排序错误
- 根据不同函数排序的std::向量的交集