基于c+;大小的条件语句的问题+; 我是C++新手,正在做一个简单的DVD租赁程序。我特别对案例3和4有问题。也许我误解了sizeof背后的目的。我试图让它做的是告诉char数组是否为空,如果为空,则允许用户通过输入他们的名字来检查它,如果不可用,则给他们一个响应,说它不可用。案例4应该做相反的事情,让他们登记。如有任何建议,将不胜感激 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <limits> using namespace std; const int arrSize = 5; struct dvdStruct //distance struct { int id; char title[51] = { 0 }; char rating[5] = { 0 }; double price; char borrower[51] = { 0 }; } dvd; dvdStruct dvds[arrSize] = {}; int userSelection; //intput variable for main menu selection int borrowId (0); int borrowIdReturn(0); //void initalize(); int main() { int size(0); dvds[0].id = 1; dvds[1].id = 2; dvds[2].id = 3; dvds[3].id = 4; dvds[4].id = 5; strcpy(dvds[0].title, "Fast 1"); strcpy(dvds[1].title, "Fast 2"); strcpy(dvds[2].title, "Fast 3"); strcpy(dvds[3].title, "Fast 4"); strcpy(dvds[4].title, "Fast 5"); strcpy(dvds[0].rating, "PG - 13"); strcpy(dvds[1].rating, "PG - 13"); strcpy(dvds[2].rating, "PG - 13"); strcpy(dvds[3].rating, "PG - 13"); strcpy(dvds[4].rating, "PG - 13"); dvds[0].price = '19.1'; dvds[1].price = '19.2'; dvds[2].price = '19.3'; dvds[3].price = '19.4'; dvds[4].price = '19.5'; strcpy(dvds[0].borrower, ""); cout << strlen(dvds[0].borrower) << endl; strcpy(dvds[1].borrower, "\0"); strcpy(dvds[2].borrower, "\0"); strcpy(dvds[3].borrower, "\0"); strcpy(dvds[4].borrower, "\0"); do { cout << "1.Display All DVD’s" << endl << "2.Display DVD Detail" << endl << "3.Check Out a DVD" << endl << "4.Check In a DVD" << endl << "5.Exit" << endl; cin >> userSelection; //Input from the user. switch (userSelection) { case 1: for (int i = 0; i < arrSize; i++) { std::cout << dvds[i].title << "' " << dvds[i].rating << " " << dvds[i].borrower << endl; } system("pause"); system("CLS"); break; case 2: int dvdNum; cout << "Enter a DVD number:"; cin >> dvdNum; std::cout << dvds[dvdNum - 1].title << "' " << dvds[dvdNum - 1].rating << endl; system("pause"); system("CLS"); break; case 3: cout << "Enter and id:"; cin >> borrowId; if (strlen(dvds[borrowId-1].borrower) == 0) { cout << "Enter your name: "; cin >> dvds[borrowId-1].borrower; } else { cout << "This dvd is not available" << endl; } system("pause"); system("CLS"); break; case 4: cout << "Enter and id:"; cin >> borrowIdReturn; if (strlen(dvds[borrowIdReturn - 1].borrower) == 0) { cout << "This dvd is available" << endl; } else { cout << "Your DVD has been returned " << endl; strcpy(dvds[borrowIdReturn - 1].borrower, "\0"); } system("pause"); system("CLS"); break; case 5: return 0; break; } } while (userSelection == 1 || userSelection == 2 || userSelection == 3 || userSelection == 4); } \define\u CRT\u SECURE\u NO\u警告 #包括 #包括 #包括 使用名称空间std; 常数int arrSize=5; 结构DVD结构//距离结构 { int-id; 字符标题[51]={0}; 字符评级[5]={0}; 双倍价格; 字符借用者[51]={0}; }dvd; dvdStruct dvds[arrSize]={}; int用户选择//主菜单选择的输入变量 int-id(0); int-followeidreturn(0); //void initalize(); int main(){ 整数大小(0); DVD[0]。id=1; DVD[1]。id=2; DVD[2]。id=3; DVD[3]。id=4; DVD[4]。id=5; strcpy(DVD[0]。标题为“快速1”); strcpy(DVD[1]。标题为“快速2”); strcpy(DVD[2]。标题为“快速3”); strcpy(DVD[3]。标题为“快速4”); strcpy(DVD[4]。标题为“快速5”); strcpy(DVD[0]。评级为“PG-13”); strcpy(DVD[1]。评级为“PG-13”); strcpy(DVD[2]。评级为“PG-13”); strcpy(DVD[3]。评级为“PG-13”); strcpy(DVD[4]。评级为“PG-13”); DVD[0]。价格='19.1'; DVD[1]。价格='19.2'; DVD[2]。价格='19.3'; DVD[3]。价格='19.4'; DVD[4]。价格='19.5'; strcpy(DVD[0]。借款人,“”; cout

基于c+;大小的条件语句的问题+; 我是C++新手,正在做一个简单的DVD租赁程序。我特别对案例3和4有问题。也许我误解了sizeof背后的目的。我试图让它做的是告诉char数组是否为空,如果为空,则允许用户通过输入他们的名字来检查它,如果不可用,则给他们一个响应,说它不可用。案例4应该做相反的事情,让他们登记。如有任何建议,将不胜感激 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <limits> using namespace std; const int arrSize = 5; struct dvdStruct //distance struct { int id; char title[51] = { 0 }; char rating[5] = { 0 }; double price; char borrower[51] = { 0 }; } dvd; dvdStruct dvds[arrSize] = {}; int userSelection; //intput variable for main menu selection int borrowId (0); int borrowIdReturn(0); //void initalize(); int main() { int size(0); dvds[0].id = 1; dvds[1].id = 2; dvds[2].id = 3; dvds[3].id = 4; dvds[4].id = 5; strcpy(dvds[0].title, "Fast 1"); strcpy(dvds[1].title, "Fast 2"); strcpy(dvds[2].title, "Fast 3"); strcpy(dvds[3].title, "Fast 4"); strcpy(dvds[4].title, "Fast 5"); strcpy(dvds[0].rating, "PG - 13"); strcpy(dvds[1].rating, "PG - 13"); strcpy(dvds[2].rating, "PG - 13"); strcpy(dvds[3].rating, "PG - 13"); strcpy(dvds[4].rating, "PG - 13"); dvds[0].price = '19.1'; dvds[1].price = '19.2'; dvds[2].price = '19.3'; dvds[3].price = '19.4'; dvds[4].price = '19.5'; strcpy(dvds[0].borrower, ""); cout << strlen(dvds[0].borrower) << endl; strcpy(dvds[1].borrower, "\0"); strcpy(dvds[2].borrower, "\0"); strcpy(dvds[3].borrower, "\0"); strcpy(dvds[4].borrower, "\0"); do { cout << "1.Display All DVD’s" << endl << "2.Display DVD Detail" << endl << "3.Check Out a DVD" << endl << "4.Check In a DVD" << endl << "5.Exit" << endl; cin >> userSelection; //Input from the user. switch (userSelection) { case 1: for (int i = 0; i < arrSize; i++) { std::cout << dvds[i].title << "' " << dvds[i].rating << " " << dvds[i].borrower << endl; } system("pause"); system("CLS"); break; case 2: int dvdNum; cout << "Enter a DVD number:"; cin >> dvdNum; std::cout << dvds[dvdNum - 1].title << "' " << dvds[dvdNum - 1].rating << endl; system("pause"); system("CLS"); break; case 3: cout << "Enter and id:"; cin >> borrowId; if (strlen(dvds[borrowId-1].borrower) == 0) { cout << "Enter your name: "; cin >> dvds[borrowId-1].borrower; } else { cout << "This dvd is not available" << endl; } system("pause"); system("CLS"); break; case 4: cout << "Enter and id:"; cin >> borrowIdReturn; if (strlen(dvds[borrowIdReturn - 1].borrower) == 0) { cout << "This dvd is available" << endl; } else { cout << "Your DVD has been returned " << endl; strcpy(dvds[borrowIdReturn - 1].borrower, "\0"); } system("pause"); system("CLS"); break; case 5: return 0; break; } } while (userSelection == 1 || userSelection == 2 || userSelection == 3 || userSelection == 4); } \define\u CRT\u SECURE\u NO\u警告 #包括 #包括 #包括 使用名称空间std; 常数int arrSize=5; 结构DVD结构//距离结构 { int-id; 字符标题[51]={0}; 字符评级[5]={0}; 双倍价格; 字符借用者[51]={0}; }dvd; dvdStruct dvds[arrSize]={}; int用户选择//主菜单选择的输入变量 int-id(0); int-followeidreturn(0); //void initalize(); int main(){ 整数大小(0); DVD[0]。id=1; DVD[1]。id=2; DVD[2]。id=3; DVD[3]。id=4; DVD[4]。id=5; strcpy(DVD[0]。标题为“快速1”); strcpy(DVD[1]。标题为“快速2”); strcpy(DVD[2]。标题为“快速3”); strcpy(DVD[3]。标题为“快速4”); strcpy(DVD[4]。标题为“快速5”); strcpy(DVD[0]。评级为“PG-13”); strcpy(DVD[1]。评级为“PG-13”); strcpy(DVD[2]。评级为“PG-13”); strcpy(DVD[3]。评级为“PG-13”); strcpy(DVD[4]。评级为“PG-13”); DVD[0]。价格='19.1'; DVD[1]。价格='19.2'; DVD[2]。价格='19.3'; DVD[3]。价格='19.4'; DVD[4]。价格='19.5'; strcpy(DVD[0]。借款人,“”; cout,c++,C++,sizeof()提供对象的大小。无论对象中有什么,对象的大小总是相同的。事实上,sizeof()是在编译时计算的,其值无论如何都不会受到运行时发生的任何事情的影响 在大多数情况下,C++代码应该使用std::string,而不是char数组。std::string的empty()方法指示字符串是否为空 如果仍然坚持使用C-stylechar数组和C-style'\0'终止字符串,请使用Cstrlen()函数检查字符数组是否只包含前导的'\0',表示空字符串。sizeof()提供对象的大小。无论对

sizeof()
提供对象的大小。无论对象中有什么,对象的大小总是相同的。事实上,
sizeof()
是在编译时计算的,其值无论如何都不会受到运行时发生的任何事情的影响

在大多数情况下,C++代码应该使用
std::string
,而不是
char
数组。
std::string
empty()
方法指示字符串是否为空

如果仍然坚持使用C-style
char
数组和C-style
'\0'
终止字符串,请使用C
strlen()
函数检查字符数组是否只包含前导的
'\0'
,表示空字符串。

sizeof()
提供对象的大小。无论对象中有什么,对象的大小总是相同的。事实上,
sizeof()
是在编译时计算的,其值无论如何都不会受到运行时发生的任何情况的影响

在大多数情况下,C++代码应该使用
std::string
,而不是
char
数组。
std::string
empty()
方法指示字符串是否为空


如果您仍然坚持使用C-style
char
数组和C-style
'\0'
终止字符串,请使用C
strlen()
函数检查字符数组是否只包含前导的
'\0'
,表示空字符串。

您可能希望使用
strlen()
,而不是
sizeof()
.stdlen?新的c++17标准中的某些内容?您可能希望使用
strlen()
,而不是
sizeof())
.stdlen?新的c++17标准中的一些东西?谢谢你,这就是我一直在寻找的。为此,我正在尝试使用字符数组使其工作。如果我使用firstname,输入仍然会遇到问题,但如果我使用firstname,则不会。使用cin会有问题吗?我可以用什么来代替它?我有更新了上面的代码。操作符>提取空白符的单词。C++中的许多初学者被“>操作符引入歧途。当他们应该使用STD::GETLIN()时,他们不必使用它。如果你想读取一行输入,不要使用”。,这就是它的用途。>>不是,我重复一遍,也不是,用于阅读一行文本。但是,当我更改为getline时,我收到一个有意义的消息,并且在visual studio中出现错误,说“没有重载函数的实例与参数列表匹配”,我确信std::getline()工作很好。你不能正确地将它与std::string一起使用。因为我不知道你这次做了什么,我不确定你怎么能期望我告诉你你做错了什么。谢谢你,这就是我一直在寻找的。为此,我正试图使用char数组的使它工作。如果我先输入n,我仍然会遇到输入问题如果我放第一个名字,它就不会。这是使用CIN的问题吗?我能用什么来代替它?我已经更新了上面的代码。操作符>提取空白符的分隔词。C++中的许多初学者都被>操作员引入歧途。当他们使用STD::GETLIN()时,他们被教导不必要地使用它。。如果要读取一行输入,请不要使用>>。始终使用std::getline(),这就是它的用途。>>不是,我重复一遍,不是,用于读取一行文本。但是,当我更改为getline时,这是有意义的,并且在visual studio中出现错误,显示“没有重载函数的实例与参数列表匹配”我相信std::getline()可以正常工作。正确地说,您不能将它与std::string一起使用。因为我不知道您这次做了什么,我不知道您怎么能期望我告诉您您做错了什么。