Having trouble figuring this out


I'm supposed to write a program that reads a series of numbers from the user and output when was the first & last times that the number 12 was entered. I can get the program to output when the first 12 is entered but I'm not sure how to find out when the last 12 occurred. Any help will be appreciated.

This is the code I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
int counter = 0, first, last, number, num;

cout << "How many numbers will you enter: ";
cin >> number;

for (int I = 1; I <= number; I++)(
cout << "Number: ";
cin >> num;
counter++;

if (num == 12)
first = counter;
)
return 0;
}
I don't think you really need to dedicate a counter to how many total numbers have been entered by the user as you're currently doing because your loop is already bounded by the number variable, and the I variable holds the same value as counter anyway. Instead, use the counter variable to count how many times the number 12 has been entered. When its been entered once, set first equal to the I variable. When its been entered twice, set last equal to the I variable.
I figured it out. Thanks for the help!
Topic archived. No new replies allowed.