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
#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.
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
#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;
}
}
}