Can't get a basic C++ program to work

RapidSnail

Diamond Member
Apr 28, 2006
4,258
0
0
I can't get this damn program to work correctly. It's a past due, by a few hours, homework assignment for intro C++. The theme is do/while and while statements. Nothing advanced. The program itself should be simple but it doesn't work correctly. Please help.

By the way, I included in the end while statement a line of code:

cout << q << endl;

This is for debugging so I can see program output at each step. It will not be part of the final code.

Source

Edit: Moved source to pastebin. Problem solved.
 

RapidSnail

Diamond Member
Apr 28, 2006
4,258
0
0
Calculate the greatest common divisor of two numbers: p and q. The value p must be greater than q (p > q) and both must be non-zero. The program acts very strangely, sometimes calculating the number sometimes not. You'd have to look at the source code that I linked for a better understanding.
 

stevf

Senior member
Jan 26, 2005
290
0
0
that link doesnt work for me - just that site's home, no code I could see
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
There's a download file link on the right side... but it points to a .BIN file.. enough to scare me away
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
The problem is occurring because if, for example, p = 12 and q = 10 then your while loop [while (p%q > 0)] will terminate prematurely at q = 6, because 12%6 = 0. The loop should be changed to [while (p%q >= 0)] and it will work as intended.
 

RapidSnail

Diamond Member
Apr 28, 2006
4,258
0
0
Originally posted by: Dimmu
The problem is occurring because if, for example, p = 12 and q = 10 then your while loop [while (p%q > 0)] will terminate prematurely at q = 6, because 12%6 = 0. The loop should be changed to [while (p%q >= 0)] and it will work as intended.

Yep, figured it out just now. Thanks. Do you know how to calculate the GCD if q is negative, and p and q are negative? Shouldn't the GCD of 16 and -16 be 16? This calculator says otherwise.
 

RapidSnail

Diamond Member
Apr 28, 2006
4,258
0
0
Originally posted by: Crusty
There's a download file link on the right side... but it points to a .BIN file.. enough to scare me away

I don't know what .BIN file is, but the link should be .cpp. If not, you can add the extension. But is there a way for me to post source code that preserves white space?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: RapidSnail
Originally posted by: Crusty
There's a download file link on the right side... but it points to a .BIN file.. enough to scare me away

I don't know what .BIN file is, but the link should be .cpp. If not, you can add the extension. But is there a way for me to post source code that preserves white space?

Unfortunately not with fusecrap. The attach code is busted bigtime, and just pasting it into the forum msg won't preserve it either.
 

RapidSnail

Diamond Member
Apr 28, 2006
4,258
0
0
Originally posted by: Crusty
Originally posted by: RapidSnail
Originally posted by: Crusty
There's a download file link on the right side... but it points to a .BIN file.. enough to scare me away

I don't know what .BIN file is, but the link should be .cpp. If not, you can add the extension. But is there a way for me to post source code that preserves white space?

Unfortunately not with fusecrap. The attach code is busted bigtime, and just pasting it into the forum msg won't preserve it either.

Any websites that will let me upload a copy/paste and not require download?
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |