我在做什么错了?我想计算项目的总价格并显示它。 我用for循环描述了我的问题。我要计算物品的总价格
#include<iostream.h>
#include<conio.h>
int T,N;
class item
{
int quantity;
char name[100];
float price;
float Total;
public:
void get_data();
void put_data();
void calculate();
void Total1();
};
void item::get_data()
{
cout<<"Enter the name of the item "<<T+1<<" = ";
cin>>name;
cout<<"Enter the quantity= ";
cin>>quantity;
cout<<"Enter the price= ";
cin>>price;
cout<<"\n";
}
void item::put_data()
{
cout<<"\n"<<name<<"\t\t "<<quantity<<"\t\t "<<price<<"\t\t\t"<<"SR";
}
我想计算物品的总价格,但是当我开始追踪程序时。它不执行函数calculate()的for循环。这就是为什么我无法添加价格变量中存在的值并且无法将其存储在变量Total中。
void item::calculate()
{
for(T=0;T<N;T++)
{
Total=Total+price;
}
}
void item::Total1()
{
cout<<"\nTotal Amount= "<<Total;
}
void main()
{
int N;
clrscr();
cout<<"Enter the Total number of item = ";
cin>>N;
item i[100];
for(T=0;T<N;T++)
{
i[T].get_data();
}
cout<<"\nName of items";
cout<<"\t\tQuantity ";
cout<<"\titem price ";
cout<<"\t\tGST ";
for(T=0;T<N;T++)
{
i[T].put_data();
}
i[T].calculate();
i[T].Total1();
getch();
}
您是否尝试在'calculate()'(例如'cout << N << endl;')中打印'N'的值?我想你会发现你没有正确初始化它,因为*你有两个同名的变量。* – Beta
我还没有完成它。首先,我必须计算总价格(通过访问价格变量),然后我将在计算的价格上应用GST(商品和服务税)。 – User123
如果要计算所有项目的总价格,该功能应该可能不是项目类别的一部分。现在就像有一罐咖啡豆计算购物车中所有其他物品的总成本。 –