PDA

View Full Version : C++ loop error help?




Jalaska13
Mar 23, 2009, 08:10 PM
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
Mar 23, 2009, 08:24 PM
Please use the hash mark to insert code tags. I haven't looked yet, but here's the readable copy of the 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
Mar 23, 2009, 08:29 PM
OK, pretty simple:

#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
Mar 23, 2009, 08:33 PM
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
Mar 23, 2009, 08:34 PM
#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
Mar 23, 2009, 08:37 PM
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:


#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
Mar 23, 2009, 08:49 PM
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:

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
Mar 24, 2009, 04:25 AM
Did anyone else get an ad for a fingerprint reader below this thread..? Uncanny...