C++ C++;具有特定输入的数组错误
因此,在这个程序中,当用户输入例如“shirt”作为销售商品的名称时,它会直接跳转到输出并失败。但是,如果我输入一个简单的数字,例如销售商品的名称仅为10,则一切正常。任何帮助都将不胜感激 编辑:该函数从一个更大的程序中拉出来发布。在主程序中,它是一个浮点函数,而不是int,并且仍然给出错误C++ C++;具有特定输入的数组错误,c++,arrays,C++,Arrays,因此,在这个程序中,当用户输入例如“shirt”作为销售商品的名称时,它会直接跳转到输出并失败。但是,如果我输入一个简单的数字,例如销售商品的名称仅为10,则一切正常。任何帮助都将不胜感激 编辑:该函数从一个更大的程序中拉出来发布。在主程序中,它是一个浮点函数,而不是int,并且仍然给出错误 #include <iostream> #include <iomanip> #include <stdlib.h> using namespace std; 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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
双taxPct;
字符状态;
双倍销售额、价值;
做
{
system(“cls”);//当用户再次运行程序时清除屏幕
saleAmount=0;//当用户再次运行程序时,将sale amount重置为0
库特
你需要设计你的数据结构。你似乎想存储销售信息,每个项目有两件事,名称和价格。所以让我们做一个销售项目
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;
您是否正在尝试将字符数据存储到int
中。对。我已经意识到这就是问题所在,正在查找代码中的错误位置。感谢不要使用允许您同时输入数字和字母数字字符串的类型。