That portion of code is only run if d == lowcom. But the loop ends when d == lowcom, thus that portion of code is never reached.It seems to never want to run the bold text.
That portion of code is only run if d == lowcom. But the loop ends when d == lowcom, thus that portion of code is never reached.It seems to never want to run the bold text.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Here is the new code I am working on now. I changed the name of lowcom since it technically is not the lowest common denominator.
I tried adjusting the loop so it would run one step farther withCode:#include <iostream> using namespace std; int main() { int a; int b; int d; int m; cout << "Enter 2 numbers: "; cin >> a >> b; m = a > b ? b : a; for(d = 2; d < m; d++) { if(((a%d)==0)&&((b%d)==0)) if(d == m) { cout << "No common denominators" << endl; cout << "Press Enter to continue." << endl; cin.ignore(1); // Ignore leftover Enter key. cin.get(); //press to continue return 0; } else { cout << "The lowest common denominator is :" << d << endl; cout << "Press Enter to continue." << endl; cin.ignore(1); // Ignore leftover Enter key. cin.get(); //press to continue return 0; } } }
for(d = 2; d==m; d++)
but no luck. I also tried
for(d = 2; d==(m+1); d++)
but it still skips the code.
Any ideas?
The thing is, your question asks you to convert the C code to C++ code. If the C code is invalid, you simply cannot convert it. If it is logically incorrect, then your C++ code should likewise be logically incorrect, otherwise you would not meet the requirements of the question.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I guess you are right. If I had to get the program working what would you suggest I edit. I just cant wrap my head around this problem even though it should be so simple.Originally Posted by laserlight
You're rewriting the code already given to you. Don't do that. Here's the old loop:
It's the same thing as this:Code:for(d = 2; d<min; d++) if(((a%d)++0) && ((b%d) ==0)) break;
Undertand what it's doing. It's cycling through all posibilities to divide a and b by d, starting with 2 and moving all the way up until it reaches either a or b, whichever is lowest. If it can't find a common denominator by then, there is none. If it found one, it doesn't need to search anymore, so it breaks the loop.Code:for(d = 2; d<min; d++) { if(((a%d)++0) && ((b%d) ==0)) { break; } }
You changed the entire loop to include everything else after it. Take that stuff out and put it after the loop.
Outside of changing the obviously weird ++ to a ==, I don't think you should be altering much else.
Originally Posted by MacGyver
Ok, thanks! That helped out huge. I guess all the indents that are on the C code sample I was given are messed up and that is why I was placing the code in the loop. It looked like it should be there. Your explanation makes sense though. Thanks MacGyver and laserlight for all your help.! I really appreciate it!!!