C++ 某些输入的数组错误
C++ Array error with certain inputs
因此,在此程序中,当用户输入例如"shirt"作为销售项目的名称时,它会直接跳到输出并失败。 但是,如果我输入一个简单的数字,例如销售项目的名称仅为 10,则一切正常。 任何帮助将不胜感激
编辑:该功能已从更大的程序中拉出以发布。 在主程序中,它是一个浮点函数,而不是一个 int,并且仍然给出错误
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
double taxPct;
char status;
double saleAmount, value;
do
{
system("cls"); // clears the screen when the user runs the program again
saleAmount = 0; // resets sale amount to 0 when the user runs the program again
cout
<<"********************************************" <<endl
<<"********************************************" <<endl
<<"*****" << right << setw(39) << "*****" <<endl
<<"*****" << setw(24) << "W E L C O M E" << setw(15) << "*****" <<endl
<<"*****" << setw(39) << "*****" <<endl
<<"*****" << setw(23) << "T O T H E" <<setw(16) << "*****" <<endl
<<"*****" << setw(39) << "*****" <<endl
<<"*****" << setw(30) << "S A L E S R E C E I P T" <<setw(9) << "*****" <<endl
<<"*****" << setw(39) << "*****" <<endl
<<"*****" << setw(24) << "P R O G R A M" <<setw(15) << "*****" <<endl
<<"*****" << setw(39) << "*****" <<endl
<<"********************************************" <<endl
<<"********************************************" <<endl
<<endl;
int items;
cout << "How many sales items do you have? : ";
cin >> items;
int sales[items][2];
int counter =0;
for (int counter = 0; counter < items; counter++)
{
cout << "Enter the name of sales item " << counter + 1 << ": ";
cin >> sales[counter][0];
cout << "Enter the price of " << sales[counter][0] << " : $";
cin >> sales[counter][1];
saleAmount=saleAmount+sales[counter][1];
}
cout << "Enter in the sales tax percentage" <<endl
<< "(Enter 10 for 10%): ";
cin >> taxPct;
cout <<endl <<endl;
if (taxPct>.9999)
{ // failsafe: converts tax percentage to a decimal for calculating tax amount if a whole number is entered (i.e. entering .06 or 6 will give the same result)
taxPct=taxPct/100;
}
double taxAmount = saleAmount * taxPct;
double grandTotal = saleAmount + taxAmount;
cout << fixed << setprecision(2)
<<"********************************************" <<endl
<<"********" << setw(26) << "S A L E S R E C E I P T" << setw(10) << "********" <<endl
<<"********************************************" <<endl
<<"**" << setw(42) << "**" <<endl
<<"**" << setw(42) << "**" <<endl
<<"** Item" << setw(26) << "Price" << setw(10) << "**" <<endl
<<"** ------------------------------------ **" <<endl;
for (counter = 0; counter < items; counter++)
{
cout <<"** " << left << setw(12) << sales[counter][0] << right << setw(11) << "$" << setw(9) << sales[counter][1] << setw(8) << "**" <<endl;
}
cout
<<"**" << setw(42) << "**" <<endl
<<"**" << setw(42) << "**" <<endl
<<"********************************************" <<endl
<<"**" << setw(42) << "**" <<endl
<<"**" << setw(42) << "**" <<endl
<< left << setw(4) << "**" << setw(11) << "Total Sales" << right <<setw(12) << "$" << setw(9) << saleAmount << setw(8) <<"**" <<endl
<< left << setw(4) << "**" << setw(9) << "Sales Tax" << right <<setw(14) << "$" << setw(9) << taxAmount << setw(8) <<"**" <<endl
<< left << setw(27) << "**" << setw (15) << "-----------" <<"**" <<endl
<< left << setw(4) << "**" << setw(11) << "Grand Total" << right << setw(12)<< "$" << setw(9) << grandTotal << setw(8) <<"**" <<endl
<<"**" << setw(42) << "**" <<endl
<<"**" << setw(42) << "**" <<endl
<<"********************************************" <<endl <<endl <<endl;
cout <<"Do you want to run this program again? (Y/N): "; // asks user if they wish to calculate another sale
cin >> status;
cout <<endl;
}
while (status == 'Y' || status == 'y');
return 0;
}
你需要
设计你的数据结构。您似乎想要存储销售信息,并且每个项目,名称和价格都有2件事。因此,让我们制作一个销售项目
struct SalesItem
{
std::string Name;
int Price; // maybe there is a better type, but int will do
}
现在让我们分配一些
SalesItem sitems[items];
好的,现在让我们输入一个
cout << "Enter the name of sales item " << counter + 1 << ": ";
cin >> sitems[counter].Name;
cout << "Enter the price of " << sitems[counter].Name << " : $";
cin >> sitems[counter].Price;
你应该考虑 std::vector 而不是数组。
你应该使用一种设计用于持有货币的类型(谷歌 c++ 货币类型)
相关文章:
- 类动态数组错误
- 如何返回多维数组?错误C2440
- 数组错误(不允许类型不完整)
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 从文本文件读取 2D 数组错误
- 动态结构数组错误
- 旋转数组 - 错误
- 按字母顺序排序名称,数组错误
- 因为每个循环都读取数组错误
- 二维数组错误
- C 获取数组错误
- 编译时出现字符串数组错误
- c++2维指针类数组错误(使用openframework)
- C++数组错误
- C++上数组错误的大小
- C++数组错误:访问冲突读取位置0xC0000005
- c++2D数组错误
- C++:为“char [4]”数组错误获取太多初始值设定项
- 分段错误(核心转储)排序字符串数组错误