如何使用(char)接受句子并将其输出(c ++)?

时间:2019-01-08 03:27:45

标签: c++

在为我的小堂兄进行这个项目时,我遇到了许多困难。我想知道是否有人可以帮助我使用char输出完整的句子(或帮助我修复这些错误。)我是一个相对较新的程序员(大约8个月)。这是我的代码/我尝试过的。一旦插补了一个句子,或者打开文件后没有响应,大多数错误都会导致程序冻结。

  #include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>

class legoset
{
    char setcatname[25];
    char name[50];
    char legoinclude[25];
    char legotype[25];

public:
    void create_category();
    void show_category() const;
    void modify();
    void report() const;
    int retacno() const;
};

void legoset::create_category()
{
    std::cout << "Please enter a category name : \n";
    std::cin >> setcatname;
    //std::cin.getline(setcatname, 25);
    std::cout << "Please enter your username! \n";
    std::cin >> name;
    std::cin.ignore();
    std::cin.getline(name, 50);
    std::cout << name << " , is it a vehicle or building (V/B)?\n";
    std::cin >> legotype;
    legotype[25] = toupper(legotype[25]);
    std::cin.getline(legotype, 25);
    std::cout << "\n Please enter the name of the lego set. \n"; 
    std::cin >> legoinclude;
    //std::cin.getline(legoinclude, 25);
    std::cout << "\n\n Category Created Successfully!!!";

    return;
}

void legoset::show_category() const
{
    std::cout << "Category : \n" << setcatname;
    std::cout << "Username Of Holder \n: " << name;
    std::cout << " Lego type (B/V) : " << legotype;
    std::cout << " Lego set (with details) : " << legoinclude;
    return;
}

void legoset::modify()
{
    std::cout << "Category : \n" << setcatname[25];
    std::cout << "\nModify Holder's name : ";
    std::cin.ignore();
    std::cin.getline(name, 50);
    std::cout << "\nModify A Building or vehicle class ( B/V )  : ";
    std::cin >> legotype[25];
    legotype[25] = toupper(legotype[25]);
    std::cout << "\nModify Lego set (with details) : ";
    std::cin >> legoinclude[25];
}

void legoset::report() const
{
    std::cout << setcatname[25] << std::setw(10) << " " << name << std::setw(10) << " " << legotype[25] << std::setw(6) << legoinclude[25] <<  std::endl;
}

int legoset::retacno() const
{
    return setcatname[25];
}


void write_legoset();   //function to write record in binary file
void display_sp(int);   //function to display account details given by user
void modify_set(int);   //function to modify record of file
void delete_set(int);   //function to delete record of file
void display_all();     //function to display all account details
void intro();   //introductory screen function


int main()
{
    char choice;
    int num;
    intro();
    do
    {
        system("cls");
        std::cout << "\n\n\n\tMAIN MENU";
        std::cout << "\n\n\t01. New Category";
        std::cout << "\n\n\t02. ADD A NEW SET";
        std::cout << "\n\n\t03. ALL USERS HOLDER LIST";
        std::cout << "\n\n\t04. DELETE A CATEGORY";
        std::cout << "\n\n\t05. MODIFY A CATEGORY";
        std::cout << "\n\n\t06. EXIT";
        std::cout << "\n\n\tSelect Your Option (1-6) ";
        std::cin >> choice;
        system("cls");
        switch (choice)
        {
        case '1':
            write_legoset();
            break;
        case '2':
            std::cout << "\n\n\tEnter The category Name : ";    std::cin >> num;
            display_sp(num);
            break;
        case '3':
            display_all();
            break;
        case '4':
            std::cout << "\n\n\tEnter The Category Name : ";    std::cin >> num;
            delete_set(num);
            break;
        case '5':
            std::cout << "\n\n\tEnter The Category Name : ";    std::cin >> num;
            modify_set(num);
            break;
        case '6':
            std::cout << "\n\n\tThanks for using lego managemnt system!";
            std::exit;
            break;
        default:    std::cout << "\a";
        }
        std::cin.ignore();
        std::cin.get();
    } while (choice != '6');
    return 0;
}

//***************************************************************
//      function to write in file
//****************************************************************

void write_legoset()
{
    legoset lego;
    std::ofstream outFile;
    outFile.open("legoset.dat", std::ios::binary | std::ios::app);
    lego.create_category();
    outFile.write(reinterpret_cast<char *> (&lego), sizeof(legoset));
    outFile.close();
}

//***************************************************************
//      function to read specific record from file
//****************************************************************

void display_sp(int n)
{
    legoset lego;
    bool flag = false;
    std::ifstream inFile;
    inFile.open("legoset.dat", std::ios::binary);
    if (!inFile)
    {
        std::cout << "File could not be open !! Press any Key...";
        return;
    }
    std::cout << "\nLEGOSET DETAILS\n";

    while (inFile.read(reinterpret_cast<char *> (&lego), sizeof(legoset)))
    {
        if (lego.retacno() == n)
        {
            lego.show_category();
            flag = true;
        }
    }
    inFile.close();
    if (flag == false)
        std::cout << "\n\nLego set does not exist in this file";
}

//***************************************************************
//      function to modify record of file
//****************************************************************

void modify_set(int n)
{
    bool found = false;
    legoset lego;
    std::fstream File;
    File.open("legoset.dat", std::ios::binary | std::ios::in | std::ios::out);
    if (!File)
    {
        std::cout << "File could not be open !! Press any Key...";
        return;
    }
    while (!File.eof() && found == false)
    {
        File.read(reinterpret_cast<char *> (&lego), sizeof(legoset));
        if (lego.retacno() == n)
        {
            lego.show_category();
            std::cout << "\n\nPlease Enter The New Details For This Category." << std::endl;
            lego.modify();
            int pos = (-1)*static_cast<int>(sizeof(legoset));
            File.seekp(pos, std::ios::cur);
            File.write(reinterpret_cast<char *> (&lego), sizeof(legoset));
            std::cout << "\n\n\t Category Updated!";
            found = true;
        }
    }
    File.close();
    if (found == false)
        std::cout << "\n\n Category Not Found ";
}

//***************************************************************
//      function to delete record of file
//****************************************************************


void delete_set(int n)
{
    legoset lego;
    std::ifstream inFile;
    std::ofstream outFile;
    inFile.open("legoset.dat", std::ios::binary);
    if (!inFile)
    {
        std::cout << "File could not be open !! Press any Key...";
        return;
    }
    outFile.open("Temp.dat", std::ios::binary);
    inFile.seekg(0, std::ios::beg);
    while (inFile.read(reinterpret_cast<char *> (&lego), sizeof(legoset)))
    {
        if (lego.retacno() != n)
        {
            outFile.write(reinterpret_cast<char *> (&lego), sizeof(legoset));
        }
    }
    inFile.close();
    outFile.close();
    remove("legoset.dat");
    rename("Temp.dat", "legoset.dat");
    std::cout << "\n\n\tCategory Deleted ..";
}


//***************************************************************
//      function to display all accounts deposit list
//****************************************************************


void display_all()
{
    legoset lego;
    std::ifstream inFile;
    inFile.open("legoset.dat", std::ios::binary);
    if (!inFile)
    {
        std::cout << "File could not be open !! Press any Key...";
        return;
    }
    std::cout << "\n\n\t\tUSER HOLDER LIST\n\n";
    std::cout << "====================================================\n";
    std::cout << "A/c no.      NAME           Type  Balance\n";
    std::cout << "====================================================\n";
    while (inFile.read(reinterpret_cast<char *> (&lego), sizeof(legoset)))
    {
        lego.report();
    }
    inFile.close();
}

void intro()
{
    std::cout << "\n\n\n\t LEGOSET";
    std::cout << "\n\n\tMANAGEMENT";
    std::cout << "\n\n\t  SYSTEM";
    std::cout << "\n\n\n\nMADE BY : Philippe Barry";
    std::cin.get();
}

//***************************************************************
//              END OF PROJECT
//***************************************************************

1 个答案:

答案 0 :(得分:1)

请注意,C ++中的数组索引比数组大小小0到1。因此,索引0是第一个元素,索引1是第二个元素,依此类推。因此,如果将一个char数组声明为大小25,则访问索引25会超出数组的末尾,并导致未定义的行为-您的程序可能会崩溃,冻结或发生其他任何事情。另外,访问索引24只会给您每个字符串后面的“空字符”。如果您确定输入的字符长度为24个字符(不应输入),则索引23将包含最后一个字符。

无论如何,您实际上不应该在C ++中使用静态长度char数组。当输入字符串大于数组大小时,即使当前的代码大部分时间都可以在char数组中使用,您当前的代码也会失败。用std :: string替换所有char数组的类型,输入函数应该起作用。

此外,正如Sam Varsavchik所说,不要立即编写整个程序。这使其成为调试的噩梦。分块编写代码-首先编写输入函数,然后是调试函数,该函数打印出所有成员变量的值。首先调试该位,然后继续其余部分。