C++ loop error help?

Discussion in 'Mac Programming' started by Jalaska13, Mar 23, 2009.

  1. macrumors newbie

    Joined:
    Mar 21, 2009
    #1
    So i've been working on this program that looks like a finger print scanner (it's for a murder mystery I'm doing at a party- it's a long story). Anyway, I have this little tidbit of code that i wrote that is supposed to say "Acquiring Fingerprint" and then put dots (".") after it. There is supposed to be a small pause in between each ".", and when i wrote it, and got the pause time right, it simply waits the time that it would take to print all 10 "."s and then prints the entire line at once. Help? I'm using xcode to compile.

    #include <iostream>
    #include <math.h>
    using namespace std;

    int main () {
    int n;
    int p;
    int q;
    p = 1;
    cout << "Acquiring Fingerprint";
    while (p < 11) {
    n = 1;
    while (n < 10000) {
    q = 2;
    while (q < n) {
    if (p % q == 0) {
    q = q;
    }
    q++;
    }
    n++;
    }
    p++;
    cout << ".";
    }
    return 0;
    }
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Please use the hash mark to insert code tags. I haven't looked yet, but here's the readable copy of the code.

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main () {
    	int n;
    	int p;
    	int q;
    	p = 1;
    	cout << "Acquiring Fingerprint";
    	while (p < 11) {
    		n = 1;
    		while (n < 10000) {
    		q = 2;
    		while (q < n) {
    			if (p % q == 0) {
    				q = q;
    			}
    			q++;
    		}
    			n++;
    		}
    		p++;
    		cout << ".";
    	}
    	return 0;
    	}
    -Lee
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    OK, pretty simple:
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main () {
            int n;
            int p;
            int q;
            p = 1;
            cout << "Acquiring Fingerprint";
            fflush(stdout);
            while (p < 11) {
                    n = 1;
                    while (n < 10000) {
                    q = 2;
                    while (q < n) {
                            if (p % q == 0) {
                                    q = q;
                            }
                            q++;
                    }
                            n++;
                    }
                    p++;
                    cout << ".";
                    fflush(stdout);
            }
            cout << endl;
            return 0;
    }
    
    
    Your output was being buffered because no endl was being printed and no explicit flush was being performed. I would suggest using sleep instead of busy-looping. Someday this code will take much less time.

    -Lee
     
  4. thread starter macrumors newbie

    Joined:
    Mar 21, 2009
    #4
    thanks a million!
    I'm fairly new to C++, so i'm not going to lie; i have no idea what that code does (well a tinsy bit of a guess...). But it worked, so thanks :)
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main (int argc, char *argv[]) {
            int p=1;
            cout << "Acquiring Fingerprint";
            fflush(stdout);
            while (p++ < 11) {
                    usleep(500000);
                    cout << ".";
                    fflush(stdout);
            }
            cout << endl;
            return 0;
    }
    This should behave the same, but not use as much CPU time... and you can also more directly tweak the length between each . by changing the value passed to usleep.

    -Lee
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    EDIT: Oops, never mind, I just repeated what Lee already said:

    That's sort of a really convoluted way to pause between periods, don't you think?

    Anyway, you can flush the output buffer to get what you want. You could do cout.flush() or cout << flush after you print each period.

    But why not just do something like this:

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    #define INTERVAL 200000L // whatever you want in microseconds
    
    int main (int argc, char *argv[]) {
    	
    	cout << "Acquiring Fingerprint" << flush;
    	
    	for (int i=0; i<10; i++) {
    		
    		usleep(INTERVAL);
    		
    		cout << "." << flush;
    		
    	}
    	
    	
    	cout << endl;
    	
    	return 0;
    }
    
    
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    That's not a problem, everyone starts somewhere... basically your code runs the inner-most loop 499850010 times, which does this:
    Code:
    while (q < n) {
                            if (p % q == 0) {
                                    q = q;
                            }
                            q++;
                    }
    
    Every 49985001 a dot is then printed.

    This is just busy work. % is the modulus operator, which gets the remainder of integer division. division is a costly operation (relatively), so this kills a little time (more than say, +). The assignment of q=q is likely irrelevant and changed into a no-op, the real "business" is the mod that uses a little time.

    As eddietr and i stated above, this is just busy work to try to kill some time. I said this code wouldn't take so long in the future because as machines get faster, they'll be able to get through this more quickly, or an optimizing compiler might eliminate large swaths of the code entirely.

    -Lee
     
  8. macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    Did anyone else get an ad for a fingerprint reader below this thread..? Uncanny...
     

Share This Page