if i wan to total up the whole sales in array how can i do it?
The below is my coding, i only know how to total the book sales but if i wan to total up all the 3 sales how, pls give me a guide?
Any if i wan to search back one of the data in this 3 array, how can i do it.
last one is if i want to alter or rewrite the new data on once of this 3 how can i do it?
PLS give me a sign to solve it....thk
#include <iostream>
using namespace std;
struct totalSales
{
char name[50];
float price;
int copy_a;
int copy_s;
float sales;
};
int main ()
{
totalSales t1[3];
int a,b;
for (a=0;a<3;a++)
{
cout<<"Enter the book title:"<<endl;
cin.getline (t1[a].name,50,'\n');
cout<<"Price for the book:"<<endl;
cin>>t1[a].price;
cout<<"The copy available:"<<endl;
cin>>t1[a].copy_a;
cout<<"The copy that sale:"<<endl;
cin>>t1[a].copy_s;
t1[a].sales=t1[a].price*t1[a].copy_s;
cin.ignore ();
t1[a].sum=t1[a].sum+t1[a].sales;
cout<<endl;
}
cout<<endl<<endl<<"The information for the books are as follow:"<<endl<<endl;
for (b=0;b<3;b++)
{
cout<<"Title:"<<t1[b].name<<endl;
cout<<"The price of the book:"<<t1[b].price<<endl;
cout<<"Copy available:"<<t1[b].copy_a<<endl;
cout<<"Copy already sold:"<<t1[b].copy_s<<endl;
cout<<"Total sales for the book is:"<<t1[b].sales<<endl<<endl<<endl;
}
#include <iostream>
usingnamespace std;
struct totalSales
{
char name[50];
float price;
int copy_a;
int copy_s;
float sales;
};
int main ()
{
totalSales t1[3];
int a,b;
for (a=0;a<3;a++)
{
cout<<"Enter the book title:"<<endl;
cin.getline (t1[a].name,50,'\n');
cout<<"Price for the book:"<<endl;
cin>>t1[a].price;
cout<<"The copy available:"<<endl;
cin>>t1[a].copy_a;
cout<<"The copy that sale:"<<endl;
cin>>t1[a].copy_s;
t1[a].sales=t1[a].price*t1[a].copy_s;
cin.ignore ();
//t1[a].sum=t1[a].sum+t1[a].sales; //There is no sum member in totalSales struct.
cout<<endl;
}
cout<<endl<<endl<<"The information for the books are as follow:"<<endl<<endl;
float total = 0;
for (b=0;b<3;b++)
{
cout<<"Title:"<<t1[b].name<<endl;
cout<<"The price of the book:"<<t1[b].price<<endl;
cout<<"Copy available:"<<t1[b].copy_a<<endl;
cout<<"Copy already sold:"<<t1[b].copy_s<<endl;
cout<<"Total sales for the book is:"<<t1[b].sales<<endl<<endl<<endl;
total += t1[b].sales;
}
cout<<"Total sales for all books: "<<total;
return 0;
}
I am assuming that you are not worried about efficiency here so I did not bother suggesting ways to improve this code. There are better ways to do this kind of programs but for the learning's sake, you are on the right track.