search item, total in array, renew infor

Hi i got a problem here.........

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;
}

return 0;
}


Thk here first for anyone can guide me!!!!!
Just make a temporary variable to store the total.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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; //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.
Last edited on
thk for the help.........but can anyone tell me how to search back the data from the array?

example like.......i got 3 statement, now i wan to roll back only statement 1 and alter and rewrite new information of it how can i do that...

if can pls give me the guide...i am very thankful for that!!!!
Topic archived. No new replies allowed.