C++收银机程序未使用<string>
C++ Cash Register Program Not using <string>
我必须在不使用字符串的情况下编写程序。这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
struct product
{
char productName[100];
double productPrice = 0;
};
const int MAX_CHAR = 101;
const int MAX_ITEM = 100;
int main()
{
product item[MAX_ITEM];
double total = 0;
int count = 0;
for (int i = 0; i < MAX_ITEM; i++)
{
cout << "Please , enter the product name(for checkout type -1) : ";
cin.get(item[i].productName, MAX_CHAR, '\n');
cin.ignore(100, '\n');
if (strcmp(item[i].productName, "-1") == 0 ) {
break;
}
else {
count++;
cout << "Please , enter the price for " << item[i].productName << " : $";
cin >> item[i].productPrice;
cin.ignore(100, '\n');
total += item[i].productPrice;
cout << endl << "Product entered : " << item[i].productName << " " << "$"
<< fixed << setprecision(2) <<item[i].productPrice << endl;
cout << "Total : $" << total << endl << endl;
}
}
cout << endl << "###############";
cout << endl << "Your Receipt : " << endl << endl;
for (int i = 0; i < count; i++) {
cout << item[i].productName << " $" << fixed << setprecision(2) << item[i].productPrice << endl;
}
cout << endl << "Total : $" << total;
cout << endl << "###############";
getchar();
getchar();
return 0;
}
我有几个问题:
为什么在 cin >> item[i].productPrice;
之后不使用 cin.ignore(100, '\n');
程序会崩溃?它只是 cin
没有任何条件,所以它不应该在输入流中留下一个换行符?
如何检查价格是否包含不正确的输入(因此它只有小数或浮点数)?
如何检查名称是否包含 >0(-1 除外)的字符和数字?
在这种情况下使用cin.getline
更好吗?
cin
是一个 istream
,因此如果您使用 cin.get()
,它应该在流中保留换行符。我没有测试这是否是您崩溃的原因,但听起来这可能会给您带来问题。
char
只是数字。 .
是 46,数字字符从 48 到 57。您可以将输入的价格读取到缓冲区中,并检查是否读取了任何不具有所需值的字符。如果发现不需要的字符,您可以决定是否要重复输入,忽略此项或退出程序。
在您的 else
分支中,检查 productName
的第一个字符是否为“-”。这样,您已经确保 productName
不是 -1
.
cin.getline()
丢弃换行符,因此您可以避免使用 cin.ignore()
.
我必须在不使用字符串的情况下编写程序。这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
struct product
{
char productName[100];
double productPrice = 0;
};
const int MAX_CHAR = 101;
const int MAX_ITEM = 100;
int main()
{
product item[MAX_ITEM];
double total = 0;
int count = 0;
for (int i = 0; i < MAX_ITEM; i++)
{
cout << "Please , enter the product name(for checkout type -1) : ";
cin.get(item[i].productName, MAX_CHAR, '\n');
cin.ignore(100, '\n');
if (strcmp(item[i].productName, "-1") == 0 ) {
break;
}
else {
count++;
cout << "Please , enter the price for " << item[i].productName << " : $";
cin >> item[i].productPrice;
cin.ignore(100, '\n');
total += item[i].productPrice;
cout << endl << "Product entered : " << item[i].productName << " " << "$"
<< fixed << setprecision(2) <<item[i].productPrice << endl;
cout << "Total : $" << total << endl << endl;
}
}
cout << endl << "###############";
cout << endl << "Your Receipt : " << endl << endl;
for (int i = 0; i < count; i++) {
cout << item[i].productName << " $" << fixed << setprecision(2) << item[i].productPrice << endl;
}
cout << endl << "Total : $" << total;
cout << endl << "###############";
getchar();
getchar();
return 0;
}
我有几个问题:
为什么在
cin >> item[i].productPrice;
之后不使用cin.ignore(100, '\n');
程序会崩溃?它只是cin
没有任何条件,所以它不应该在输入流中留下一个换行符?如何检查价格是否包含不正确的输入(因此它只有小数或浮点数)?
如何检查名称是否包含 >0(-1 除外)的字符和数字?
在这种情况下使用
cin.getline
更好吗?
cin
是一个istream
,因此如果您使用cin.get()
,它应该在流中保留换行符。我没有测试这是否是您崩溃的原因,但听起来这可能会给您带来问题。char
只是数字。.
是 46,数字字符从 48 到 57。您可以将输入的价格读取到缓冲区中,并检查是否读取了任何不具有所需值的字符。如果发现不需要的字符,您可以决定是否要重复输入,忽略此项或退出程序。在您的
else
分支中,检查productName
的第一个字符是否为“-”。这样,您已经确保productName
不是-1
.cin.getline()
丢弃换行符,因此您可以避免使用cin.ignore()
.