Why Does My C++ Code Result in an Infinite Loop?

  • Context: C/C++ 
  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    C++ Logic
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
fluidistic
Gold Member
Messages
3,934
Reaction score
286
I don't understand why I enter in an infinite loop with the following code:
Code:
#include <iostream> //for cout
#include <cstdlib> //for rand()
#include <ctime> // for time()
#include <fstream> //to write into a text file
using namespace std;

static const int M = 3;

ofstream myfile;
int main()
{
	srand(time(0)); // set initial seed value to system clock
		myfile.open("data.txt", ios::app);
int x=0;
int xold=0;
int i;
do
{
	int xold=x;
	int x = rand() % M+1;
	myfile << x << endl;
	i++;
}
while(x-xold!=1 && x-xold!=-1);
cout << "It took" << i << "iterations"  << endl;


return 0;

myfile.close();
}

The idea is to generate a random number between 1 and 3, write it into a file. Repeat until the new random number differs by either +1 or -1 from the previous random number. With the current code, the loop never ends and in a matter of a few seconds my file.txt is filled by 1's, 2's and 3's up to several Mb's.
As I understand it, my do {...} wile(x-xold!=1 && x-xold!=-1) should continue when x-xold is not equal to 1 and when x-xold is not equal to -1. So if I get xold=0 and x=1, then x-xold=1 and the loop should stop because the first condition is not satisfied... but it doesn't stop at all.
I guess my logic is flawed but I'm blind and don't see it. Any help will be appreciated.
P.S.:I started c++ 5 days ago for fun.
 
Physics news on Phys.org
fluidistic said:
Code:
int x=0;
int xold=0;
int i;
do
{
	int xold=x;
	int x = rand() % M+1;
	myfile << x << endl;
	i++;
}
while(x-xold!=1 && x-xold!=-1);

You have defined two variables called xold, and two called x. Delete the "ints" inside the do-while loop.

FWIW a sensible compiler should give you a warning message if you "mask" one variable with another.
 
  • Like
Likes   Reactions: 1 person
Also, if you want to make the numbers be truly random, I would make the number int xold be something like 10, something far enough away from the range of the random numbers.

Otherwise, you will always be starting with xold = 0, and you will be more likely to break with the results 0, 1; (unless that was your intention).

Writing to console worked in C#...
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PFPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int x = 0;
            int xold = 10;
            int i = 0;

            do
            {
                xold = x;
                x = random.Next(1, 4);
                Console.WriteLine(x);
                i++;      
            }
            while (x - xold != 1 && x - xold != -1);
            Console.WriteLine("It took {0} iterations", i);

            Console.ReadLine();
        }
    }
}
 
  • Like
Likes   Reactions: 1 person
AlephZero said:
You have defined two variables called xold, and two called x. Delete the "ints" inside the do-while loop.

FWIW a sensible compiler should give you a warning message if you "mask" one variable with another.
Ah I see! I didn't even notice this. The program works fine now! Thanks!
TheDemx27 said:
Also, if you want to make the numbers be truly random, I would make the number int xold be something like 10, something far enough away from the range of the random numbers.

Otherwise, you will always be starting with xold = 0, and you will be more likely to break with the results 0, 1; (unless that was your intention).

Writing to console worked in C#...
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PFPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            int x = 0;
            int xold = 10;
            int i = 0;

            do
            {
                xold = x;
                x = random.Next(1, 4);
                Console.WriteLine(x);
                i++;      
            }
            while (x - xold != 1 && x - xold != -1);
            Console.WriteLine("It took {0} iterations", i);

            Console.ReadLine();
        }
    }
}
Well spotted, thank you!