Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jalaska13

macrumors newbie
Original poster
Mar 21, 2009
24
0
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;
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Jalaska13

macrumors newbie
Original poster
Mar 21, 2009
24
0
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 :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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;
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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...).

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
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
Did anyone else get an ad for a fingerprint reader below this thread..? Uncanny...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.