#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Code conversion Question


    Hi I have the following C++ code and wanted to know if anyone could translate it in C for me. Thanks
    Code:
    int c, d, num, sum;
    cout << “Enter a number -> ”;
    cin >> num;
    
    while (num > 20)
    {
    	cout << “enter the first number  -> ”;
    	cin >> c;
    	cout << “enter the second number  -> ”;
    	cin >> d;
    	sum = c + d;
    	cout <<  “The sum of the numbers is  -> ” << sum << endl;
    	num = num -1;
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    have you tried doing it yourself??
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Replace cin with scanf. Replace cout with printf. The Web abounds with examples, including the man pages for both functions (IOW, RTFM). Keep in mind that you need to give scanf the address of the variable, while you only need to give printf the value.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Hi I have tried doing it myself and the only part I need help with is how to do the while num> 15 and the last line num=num-1. How would I convert those lines into C?
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Originally Posted by vcapp23
    Hi I have tried doing it myself and the only part I need help with is how to do the while num> 15 and the last line num=num-1. How would I convert those lines into C?
    Again: Replace cin with scanf. Replace cout with printf.

    It's not a simple literal replacement, but rather use the scanf function for input and the printf function for output. Read their man pages.

    PS

    Also -- and this is extremely important --, do not use a word processor to write your code! The compiler will not recognize the quotation marks that a word processor will insert and will issue errors for each quotation mark. We know this because somebody else had tried it before and that's what happened to him.

    PS

    BTW, I just performed the conversion and it is rather straightforward, nothing at all tricky about it.
    Last edited by dwise1_aol; September 6th, 2013 at 11:02 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Okay I did what you stated and this is what I got: It is pretty straightforward, but what do I do with the while num>20 line and the num=num-1? I ignored them in the code below.Can I just add it in there with the same syntax as C++?
    Code:
    #include<stdio.h>
     
    int main()
    {
       int c, d, sum;
     
       printf("Enter the first number\n");
       scanf("%d%d",&c);
    printf("Enter the second number\n");
     scanf("%d%d",&d);
       sum = c + d;
     
       printf(" The sum of the numbers is = %d\n",sum);
     
       return 0;
    }
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    C++ is a superset of C. That means that C++ uses the same syntax as C except for features that C++ has which C does not (eg, classes). iostreams is just a class library that got added on for C++ (and is not supported by C). You could just as easily use C I/O functions in C++ and that is exactly what I do, since I find iostreams to be cumbersome and a stumble backwards towards Turbo Pascal.

    So to convert your C++ code to C, all you needed to do was to convert the iostreams to stdio.

    BTW, your scanf format string is wrong:
    scanf("%d%d",&c);
    Why are you telling it to read in two integers when you're only giving it the address of one? And when the original iostream statement only read in one value?

    And why are you asking this same exact question for Visual Basic? Smells like homework.

IMN logo majestic logo threadwatch logo seochat tools logo