Beginner problems.

Sup guys.

Im doing my homework and I dont how to seperate sums and remembering the types of numbers?. Its hard to explain.

6 ( How many lines)
1 3 125 45 17 ( The number of the machine, how many numbers there are, how much does it cost)
2 2 45 71
3 2 56 78
4 1 120
2 1 45
3 1 56

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
using namespace std;
//---------------------------------------
int main ()
{
    int n, i , gn, s, j, gs, price, S1;
    int saw, cooker, mixer, blender;

    ifstream df ("factory.txt");
    ofstream rf ("factoryanswer.txt");

    saw = 1;
    cooker = 2;
    mixer = 3;
    blender = 4;

    df >> n; S1 = 0;
    for (int i = 0; i < n ; i++)
    {
        df >> gn;
        df >> gs;
        for (int j = 0; j < gs; j++)
        {
            df >> price;
            if ( saw = gn) S1 += price;
        }

    }
    rf << S1 << endl;

    return 0;
} 

The answers are
Saws: 187 dollars
Cooker: 161 dollars
Mixer: 190 dollars
Blender: 120 dollars
Mixers were made of the biggest sum

How do I do i remember the code and make it that it seperates the sum. Because im getting all of the prices sum.

Any help would be great :ddDD
6
1 3 125 45 17
2 2 45 71
3 2 56 78
4 1 120
2 1 45
3 1 56

Should you get the same result from:
4
1 3 125 45 17
2 3 45 71 45
3 3 56 78 56
4 1 120

That is, you have to store something for each distinct type {1, 2, 3, 4} because you can get more of the same on later rows.

Yes, you could have four variables: int S1, S2, S3, S4; or you could a container,
where you can read/write: S[gn] += price;
Last edited on
are you tracking across lines? eg if you have 2 lines with saws, do you total that up or list saws twice?

anyway, without any containers, this program requires a fair number of variables to do it.
its exactly like you have it, except you need a price for each item (s1, s2, ...)

you can define constants with names via enum:
enum tooltypes{saw=1, cooker, etc}; //they start at 0 by default and add 1 each item. you can reset the counter with = at any point, but for your problem only the first one set to 1 is needed.


its good to use meaningful names. gn means nothing to me. tool_id or something readable is much better, so anyone can understand the code easily without having to unravel the variable names.

I have seen this kind of problem thrown at students right before a container is taught, so you understand why the container makes it easier.

initialize variables where you create them.
int S1{}; //zero
int S2{3}; //3
you don't have to -- you can seed them later in the code all over the place as you did and it works, but its neater to just do it all at one time. That eliminates 11-14 (though, those could be the enum I mention).
Note L24. This is not an equality conditional. = means assign. == is the equality test.
Topic archived. No new replies allowed.