我如何使用(char)来获取句子并输出它们(c++)

How could I use (char) to take sentences and output them(c++)?

本文关键字:输出 c++ 句子 何使用 char 获取      更新时间:2023-10-16

在为我的小表弟做这个项目时,我遇到了很多困难。我想知道是否有人可以帮助我使用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 << "nn 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 << "nnntMAIN MENU";
std::cout << "nnt01. New Category";
std::cout << "nnt02. ADD A NEW SET";
std::cout << "nnt03. ALL USERS HOLDER LIST";
std::cout << "nnt04. DELETE A CATEGORY";
std::cout << "nnt05. MODIFY A CATEGORY";
std::cout << "nnt06. EXIT";
std::cout << "nntSelect Your Option (1-6) ";
std::cin >> choice;
system("cls");
switch (choice)
{
case '1':
write_legoset();
break;
case '2':
std::cout << "nntEnter The category Name : ";    std::cin >> num;
display_sp(num);
break;
case '3':
display_all();
break;
case '4':
std::cout << "nntEnter The Category Name : ";    std::cin >> num;
delete_set(num);
break;
case '5':
std::cout << "nntEnter The Category Name : ";    std::cin >> num;
modify_set(num);
break;
case '6':
std::cout << "nntThanks 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 DETAILSn";
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 << "nnLego 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 << "nnPlease 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 << "nnt Category Updated!";
found = true;
}
}
File.close();
if (found == false)
std::cout << "nn 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 << "nntCategory 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 << "nnttUSER HOLDER LISTnn";
std::cout << "====================================================n";
std::cout << "A/c no.      NAME           Type  Balancen";
std::cout << "====================================================n";
while (inFile.read(reinterpret_cast<char *> (&lego), sizeof(legoset)))
{
lego.report();
}
inFile.close();
}
void intro()
{
std::cout << "nnnt LEGOSET";
std::cout << "nntMANAGEMENT";
std::cout << "nnt  SYSTEM";
std::cout << "nnnnMADE BY : Philippe Barry";
std::cin.get();
}
//***************************************************************
//              END OF PROJECT
//***************************************************************

请注意,C++中的数组索引从0到小于数组大小的1。因此,索引0是第一个元素,索引1是第二个元素,等等。因此,如果您声明一个char数组的大小为25,那么访问索引25就超过了数组的末尾,并导致未定义的行为——您的程序可能会崩溃、冻结或其他任何事情。此外,访问索引24只会在每个字符串后面提供一个"null字符"。如果您确信输入将是24个字符长(而您不应该是),那么索引23将包含最后一个字符。

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

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