【问题标题】:Unexpectedly Returning the Main Function and Sorting Strings Function [closed]意外返回主函数和排序字符串函数[关闭]
【发布时间】:2014-05-13 10:38:00
【问题描述】:

我不知道为什么,但是这个程序的行为很奇怪,并返回了我插入的所有内容,并且是主函数的 2 或 3 倍,我不知道是什么问题。除此之外,我不知道如何将排序选项与字符串一起使用。我想编写一个对书名进行排序的函数。我需要将此功能(排序功能)添加到主功能。任何帮助将不胜感激。这是一个程序,它获取书名、作者和翻译者、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');
}

【问题讨论】:

  • 请先使用调试器,再在这里提问!
  • 您可能应该给我们一些示例输入和您期望的输出。这是家庭作业,对吧?
  • “返回我插入的所有内容,使用 2/3 倍的主要功能” - 这是非常模棱两可的。如果您想使用标准库对书籍进行排序,或者您想自己实现排序,这也是模棱两可的。请考虑发布您的输入和输出。如果您需要编写自己的排序,请考虑其他资源(说明“编写我的排序算法”的问题将被关闭),或者尝试编写算法并询问您是否卡住。投票结束。
  • 如果这不是家庭作业:您为什么要尝试重新实现非泛型 std::list
  • 其实伙计们,这不是家庭作业,我不知道调试器是什么,这是一个我只想知道的程序,我应该如何使用指针(我正在学习 C++ 3个月了)我已经阅读了很多关于排序的文章但没有帮助,我只是希望你们可以帮助初学者。

标签: c++ string sorting


【解决方案1】:

我假设在行

for (p = start->fl; p != NULL; p->fl = p)

在 delete_number_2() 中 for 循环的最后一部分应该是

p = p->fl

【讨论】:

  • 哦,顺便说一句,你永远不会删除任何不好的东西:)
  • 谢谢,但还是不行,如果我输入更多的字母,我会得到更多的主要功能,我不知道为什么!
  • 请准确一点。你输入了什么以及程序会发生什么(输出是什么,是否有崩溃..)?
  • 好的,我使用调试器快速浏览了一下。问题是由您的输入流 (cin) 引起的。它弄乱了输入,因此在调用 insert() 之后,流中仍然存在导致问题的字符。删除 insert() 中的所有 cin.ignore() 会有所帮助。在主循环的 switch(ch) 语句中有一个中断;在 report_number_1() 之后丢失,这会导致之后立即调用 delete_number_1() 。删除“cin.ignore()”后,插入将起作用。从那里开始并修复剩余的功能:)
  • 非常感谢,不过还是有问题,第一行会被忽略(书名)。
猜你喜欢
  • 2012-12-11
  • 2021-10-11
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多