Thread: Converting C to C++

  1. #1
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17

    Converting C to C++

    I am working on an assignment in a C++ class where I have to convert a C program to C++. I don't have a background in C and I am having some trouble with a calculation and an if statement (highlighted below in bold). I just don't understand the symbols and what they mean. I tried the C tutorials on this site and figured some of it out but not all. Any help would be appreciated.

    Here is the program in C
    Code:
    /* Convert this C program into C++ style.
    This program computes the lowest common denominator.
    */
    #include <stdio.h>
    int main(void)
    {
    int a, b, d, min;
    print( "Enter two numbers: ");
    scanf("%d%d", &a, &b);
    min = a > b ? b : a;
    for(d = 2; d<min; d++)
    
    if(((a%d)++0) && ((b%d) ==0)) break; if(d==min) {
    printf("No common denominators\n"); return 0;
    } printf("The lowest common denominator is %d\n", d); return 0;
    }
    Here is what I have coded:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int a;
    	int b;
    	int d;
    	int mn;	//calulated lowest common denominator
    
    	cout << "Enter 2 numbers: ";
    	cin >> a, b;
    	min = 0;
    	for(d = 2; d < min; d++)
    	{
    		if()break;
    			if(d == min)
    			{
    				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;
    			}
    			
    			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;
    	}
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    ?: is a ternary operator in C/C++.

    http://en.wikipedia.org/wiki/%3F:

    % in C/C++ is the modulus operator. It divides the first number by the second number and returns the remainder, not the quotient.

    && is logical AND. It resolves to true if both operands are true and false otherwise.

    break is a keyword that means to "break" out of the current loop. Execution of the loop will stop and the program will resume right after the end of the loop.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... the code in bold (as in "(a%d)++0") looks invalid. Is it a typo? Either way, once corrected it is normal C++ code.
    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. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This is the first errormessage that I get trying to compile your code
    Code:
    lcd.cc: In function `int main()':
    lcd.cc:13: error: overloaded function with no contextual type information
    meaning min is a template in c++.
    use a different variable name.
    Kurt

  5. #5
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by laserlight
    Err... the code in bold (as in "(a%d)++0") looks invalid. Is it a typo? Either way, once corrected it is normal C++ code.
    Thats what I was given so I am not sure if it is a typo or not

  6. #6
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by ZuK
    This is the first errormessage that I get trying to compile your code
    Code:
    lcd.cc: In function `int main()':
    lcd.cc:13: error: overloaded function with no contextual type information
    meaning min is a template in c++.
    use a different variable name.
    Kurt

    Thanks for that tip I changed the variable name

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    meaning min is a template in c++.
    use a different variable name.
    Better yet, do not use "using namespace std;" so carelessly.
    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

  8. #8
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by MacGyver
    ?: is a ternary operator in C/C++.

    http://en.wikipedia.org/wiki/%3F:

    % in C/C++ is the modulus operator. It divides the first number by the second number and returns the remainder, not the quotient.

    && is logical AND. It resolves to true if both operands are true and false otherwise.

    break is a keyword that means to "break" out of the current loop. Execution of the loop will stop and the program will resume right after the end of the loop.
    the ? was the tricky one for me. Thanks for the link!

    this is the line I am having trouble with now

    if(((a%d)++0)&&((b%d)==0))break;

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by laserlight
    Better yet, do not use "using namespace std;" so carelessly.
    Or perhaps he could have declared his variable correctly to begin with:

    Code:
    int min;

  10. #10
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by laserlight
    Better yet, do not use "using namespace std;" so carelessly.
    What do you mean by that?

  11. #11
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by MacGyver
    Or perhaps he could have declared his variable correctly to begin with:

    Code:
    int min;

    lol nice find, i noticed that right away as well and fixed it.

    Thanks

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Or perhaps he could have declared his variable correctly to begin with:
    I believe ZuK fixed some obvious typos when testing, including that one.

    Thats what I was given so I am not sure if it is a typo or not
    Just in case I was suffering from code blindness or something, I went to test it in gcc. gcc reports two errors, both having to do with that line. In my opinion the "++" should be "==", upon which that line of code is pretty much in C++ syntax.

    What do you mean by that?
    Instead of a "using namespace std;", you could use say, "using std::cout;" etc.
    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

  13. #13
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Quote Originally Posted by laserlight
    Instead of a "using namespace std;", you could use say, "using std::cout;" etc.
    So if I call the individual ones that I am using ex 'using std::cout;' then if I named a variable 'min' and it doesn't have a 'using ' statement associated with it I can use that variable name?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the point of namespaces is to help avoid naming conflicts. So yes, if you avoid "using namespace" indiscriminately, there is less chance of a conflict.
    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

  15. #15
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    Thanks for your help all. I almost have it working. It seems to never want to run the bold text. Can you see anything wrong with the code? If I include the break that was in the C code it always shows the lowest common denominator as 2 even when it that is incorrect.

    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;
    			}
    	}
    }
    Last edited by cdn_bacon; 03-24-2007 at 12:59 PM.

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