Thread: Converting C to C++

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    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.

    Code:
    #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;
    			}
    	}
    }
    I tried adjusting the loop so it would run one step farther with
    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?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by laserlight
    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.
    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.

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're rewriting the code already given to you. Don't do that. Here's the old loop:

    Code:
    for(d = 2; d<min; d++)
    
    if(((a%d)++0) && ((b%d) ==0)) break;
    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.

    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.

  6. #21
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by MacGyver
    You're rewriting the code already given to you. Don't do that. Here's the old loop:

    Code:
    for(d = 2; d<min; d++)
    
    if(((a%d)++0) && ((b%d) ==0)) break;
    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.

    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.

    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!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 07:38 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM