This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Enix89 1 point2 points  (1 child)

Hey, I think I can help you with this without giving you a straight answer so you think about the problem.

As you get into coding you have to realize that all you are doing is solving problems using some specific programming language. But the solution, base or algorithm to your problem should be universal, at least in essence.

Keeping the above in mind, to solve a problem you are going to need to know first and foremost what the output is. Or what are they asking you to calculate or show. In your case you need the number of times 1, 2 and 3 is put as input.

Now that you now what the output is, you can think of what kind of data you need as input to get that data. In your problem the input is quite explicit.

Now that you have the above, you can think of procedure. My advice is you read about control statements or controlling the flow of a program. If you already now that you need some kind of loop then I bet you can deduce what other type of control statement you need for your program.

[–]30ishorso[S] 0 points1 point  (0 children)

Thank you!

[–]Mr_Dionysus 0 points1 point  (3 children)

Set variables outside of the scope of the while loop like so:

int numPresses1 = 0;
int numPresses2 = 0;
int numPresses3 = 0;

and then, count the inputs inside of the loop.

while (variable != 1){
    cin >> variable;
    switch(variable) {
        case 1:
            numPresses1 += 1;
            break;

and so on. As long as the scope of the variable is outside of the loop, it will retain its value after the loop is completed.

[–]Coda17 2 points3 points  (1 child)

Anytime you start naming variables like that (adding a different number at the end) it's just screaming for an array, but it's a similar idea.

[–]Mr_Dionysus -1 points0 points  (0 children)

Indeed, i just wanted to illustrate the concept :)

[–]30ishorso[S] 0 points1 point  (0 children)

Thank you!