# Converting C to C++

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-24-2007
cdn_bacon
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;         } }```
• 03-24-2007
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.
• 03-24-2007
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.
• 03-24-2007
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
• 03-24-2007
cdn_bacon
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
• 03-24-2007
cdn_bacon
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
• 03-24-2007
laserlight
Quote:

meaning min is a template in c++.
use a different variable name.
Better yet, do not use "using namespace std;" so carelessly.
• 03-24-2007
cdn_bacon
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;
• 03-24-2007
MacGyver
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;`
;)
• 03-24-2007
cdn_bacon
Quote:

Originally Posted by laserlight
Better yet, do not use "using namespace std;" so carelessly.

What do you mean by that?
• 03-24-2007
cdn_bacon
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 :D
• 03-24-2007
laserlight
Quote:

Or perhaps he could have declared his variable correctly to begin with:
I believe ZuK fixed some obvious typos when testing, including that one.

Quote:

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.

Quote:

What do you mean by that?
Instead of a "using namespace std;", you could use say, "using std::cout;" etc.
• 03-24-2007
cdn_bacon
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?
• 03-24-2007
laserlight
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.
• 03-24-2007
cdn_bacon
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;                         }         } }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last