Terminating a the program whenever the same input was entered.

I'm trying to run this program whereas if i input the same number previously, it would terminate. I can't figure it out. Can anyone tell me where i went wrong or missed? I'm just starting the basics of C++ so feel free to criticize my newb code :)

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
#include <iostream>

using namespace std;

int main()
{

  int x = 1; // for increments. Starts counting at 1.
  int y = 0; // for starting inputs


while(x>0)
{   
    restart:
    cout << "Input a number other than " << y << " : ";
    cin  >> y;
    int a;
    a = y;
    if (a=y)
        goto restart;
    else
        break;
    x++;    

}

return 0;
}
Last edited on
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>

int main()
{
    //give variables meaningful names
  int counter{}; // for increments. Starts counting at 0
  int prevNum{};  // for starting inputs
  bool fQuit = false;

    while(!fQuit)
    {
        std::cout << "Input a number other than " << prevNum << " : ";
        int nextNum{};
        std::cin  >> nextNum;
        if(nextNum == prevNum)
        {
            fQuit = true;
            break;
        }
        else
        {
            prevNum = nextNum;//nextNum is the new prevNum;
            ++counter;
        }
    }
    std::cout << "Number of runs: " << counter << "\n";

}


Thank you so much, sir!
Topic archived. No new replies allowed.