getting last input

Hi

i'm trying to get the first/last read input using 1 variable that accepts data

if the user inputs the first input as 1 that data gets saved in the first variable.

on the second loop if the user inputs 2 that data gets saved in the first variable and compares it to the last variable if the last variable is different to the first the last variable gets changed.

on the third loop if the user inputs 3 that data gets saved in the first variable and compares it to the last variable (currently 2), now since the last variable is 2 its not the same as the first (3) so the last variable will print as 3

but when i print the last variable i'm always getting the first variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
int main(){
int first = 0;
int last = 0;

//read input
//store that value in first
while(true){
   
   std::cout<<"enter first ";
   std::cin>>first;

      if(first != last)
      {
         last = first;
      }


      std::cout<<"\nfirst " <<first;
      std::cout<<"\n last " <<last;
      std::cout<<"\n";
   }
}


what i'm expecting on the 3 loops
1
2
3
4
5
6
7
8
9
10

first 1
last 1

first 2
last 1

first 3 
last2


trying to read though the code step by step trying to understand it more but i think i'm going wrong somewhere.
Last edited on
1
2
3
4
  if(first != last)
      {
         last = first;
      }
When this code is done, first and last will be the same value, always always.

Since you output to screen right after this, in your output first and last will always be the same in that output.

Your loop look like this:
1
2
3
Input first.
Make last the same value as first.
Output those two identical values.



on the second loop if the user inputs 2 that data gets saved in the first variable and compares it to the last variable

No. The comparison happens on every loop.
Last edited on
1
2
3
4
5
if the user inputs the first input as 1 that data gets saved in the first variable.

on the second loop if the user inputs 2 that data gets saved in the first variable and compares it to the last variable if the last variable is different to the first the last variable gets changed.

on the third loop if the user inputs 3 that data gets saved in the first variable and compares it to the last variable (currently 2), now since the last variable is 2 its not the same as the first (3) so the last variable will print as 3


Your expected output doesn't follow from the requirement.


first loop: first = 0, last = 0, enter = 1 -> first = 1, last = 0, enter = 1
second loop: first = 1, last = 0, enter = 2 -> first = 2, last = 2, enter = 2


but this doesn't match the expected output???
thanks for the input, although a more usefull answer would of been to add a "loop counter" so i can get what i wanted.
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

#include <iostream>
int main() {
    int first = 0;
    int last = 0;

    int i = 1;
    int k = 1;

    while (true) {
        //read input
        //store that value in first
        if (i == 0) {

            std::cout << "\n\tenter ";
            std::cin >> first;
        }

        if (i == 1) {

            last = first;

        }

        i++;
        if (i == 3)
        {
            i = 0;
        }


        if (i == 1) {
            std::cout << "\nfirst " << first;
            std::cout << "\n last " << last;
        }
    }
}


A more useful question would have been to ask what you actually wanted. The code above still doesn't match what you said in the first post.
Topic archived. No new replies allowed.