Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ (or logic question?)

  1. May 31, 2014 #1


    User Avatar
    Gold Member

    I don't understand why I enter in an infinite loop with the following code:
    Code (Text):
    #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;
        int xold=x;
        int x = rand() % M+1;
        myfile << x << endl;
    while(x-xold!=1 && x-xold!=-1);
    cout << "It took" << i << "iterations"  << endl;

    return 0;

    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.
  2. jcsd
  3. May 31, 2014 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Jun 1, 2014 #3


    User Avatar
    Gold Member

    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 (Text):
    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;

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

  5. Jun 1, 2014 #4


    User Avatar
    Gold Member

    Ah I see! I didn't even notice this. The program works fine now! Thanks!
    Well spotted, thank you!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook