Loop Question

Im currently working on this problem
Write a program that asks the user how many numbers will be entered and then has the user enter those numbers. When this is done, report to the user the position of the first 8 entered and the last 8 entered. By position we mean, for example, that if the first 8 is the 2nd number entered then its position would be 2. Turn in the following 3 outputs to demonstrate that your program works in each case.
SAMPLE OUT PUTS
How many numbers will be entered? 8
Enter num: 5
Enter num: 8
Enter num: 6
Enter num: 8
Enter num: 8
Enter num: 3
Enter num: 7
Enter num: 6
The first 8 was in position 2
The last 8 was in position 5
SAMPLE OUT PUT

How many numbers will be entered? 8
Enter num: 5
Enter num: 2
Enter num: 6
Enter num: 8
Enter num: 1
Enter num: 3
Enter num: 7
Enter num: 6
The first 8 was in position 4
The last 8 was in position 4
SAMPLEOUTPUT

How many numbers will be entered? 8
Enter num: 5
Enter num: 1
Enter num: 6
Enter num: 5
Enter num: 2
Enter num: 3
Enter num: 7
Enter num: 6
Sorry, no eights were entered.

and here is my code so far
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
#include <iostream>
using namespace std;

int main ()
{ 
    int num,
        nums;
    char answer;
    
    do{
    cout <<"How many numbers will be entered?: "<<endl;
    cin>>num;
    for (int i=1;i<=num;i++){
     cout<<"Enter a number: "<<endl;
     cin>>nums;
    while(nums==8){
       cout<<"The first 8 was found in position "<<i<<endl;     
     break;
    }
     }
    
    cout<<"Do you want to enter more numbers (Y/N)?: "<<endl;
    cin>>answer;
    }while ((answer=='Y') || (answer=='y'));
    
    system ("PAUSE");
    return 0;
}



Currently i have it printing the position right after the 8 is stated. but my question is i don't understand how i am suppose to store the position the 8s are in or how i can have the computer skip over 8's not in the first and last position. or is it more of a problem where i need to tell the computer to GET the first and last 8 entered.

ya mainly just questions on this one, im pretty sure once i understand how i am suppose to go about it better i will be able to figure out the code.
oh and here is my output
How many numbers will be entered?:
4
Enter a number:
1
Enter a number:
2
Enter a number:
8
The first 8 was found in position 3
Enter a number:
3
Do you want to enter more numbers (Y/N)?:
n
Press any key to continue . . .



im using dev C++
Get the first 8: Pretty straightforward. If no 8 was entered before, store the position of an 8 if it comes. If you already have a first position for an 8, do not store it.
The last 8: Just as easy: Everytime the user enters an 8, it's position becomes the new last position for 8.

PS:
1
2
3
 while(nums==8){
       cout<<"The first 8 was found in position "<<i<<endl;     
     break;
That is pretty pointless. Basically, this is an if statement. Just more hacky.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i=1;i<=num;i++){
     cout<<"Enter a number: "<<endl;
     cin>>nums;
    if(nums==8){
     i==pos;
     }
     }
     cout<<"The first 8 was found in position "<<pos<<endl;    
     cout<<"The last 8 was found in posiiton "<<pos<<endl;
    
    if(nums!=8){
    cout<<"Sorry, no eights were entered."<<endl;
    }


shouldn't i just be able to make i==pos(ition) and then everytime it goes through it should change the position? now im confused on how to make it store the first position, since i need them botht o print at the end of the program and then change it to the last. am i in need of two different if statements? or two different position variables.

either was when i tired doing this it always stores i==pos as 2. a
OUTPUT
How many numbers will be entered?:
4
Enter a number:
1
Enter a number:
2
Enter a number:
8
Enter a number:
8
The first 8 was found in position 2
The last 8 was found in posiiton 2
Do you want to enter more numbers (Y/N)?:
y
How many numbers will be entered?:
4
Enter a number:
1
Enter a number:
2
Enter a number:
3
Enter a number:
4
The first 8 was found in position 2
The last 8 was found in posiiton 2
Sorry, no eights were entered.
Do you want to enter more numbers (Y/N)?:
n
Press any key to continue . . .
You need 2 additional variables, firstposition and lastposition. They should both start out with the value 0. Now if an 8 was entered, you check if firstposition is 0. If it is, you set firstposition to i+1. If not, you just ignore it. In any case, you always set lastposition to i+1 if an 8 is entered. And you know that no 8's were entered when firstposition is still 0 after the loop.
do i need more for loops?
and does this for loop even make sense logically?
for (int firstpos=0;firstpos==8;firstpos++)
Topic archived. No new replies allowed.