Converting C to C++

This is a discussion on Converting C to C++ within the C++ Programming forums, part of the General Programming Boards category; I am working on an assignment in a C++ class where I have to convert a C program 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,211
    ?: 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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    ZuK
    ZuK is offline
    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
    21,310
    meaning min is a template in c++.
    use a different variable name.
    Better yet, do not use "using namespace std;" so carelessly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,211
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 06:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21