Thread: Help!

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

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Exclamation Help!


    My buddy is in some trouble. He has to translate some coding from C# to C++, but he doesn't know how to do it. And he has to have it finished by tomorrow, which isn't enough time for him to learn how to do that. Can anyone help?

    The code:

    ht tp://pastie.org/private/ck2jxs69zm6qxzqfjqufxw

    Had to split up that URL cuz I'm new

    Some notes he had about it:

    -takes list of product codes
    -applies formula
    -tests if modulate/10 == 0
    -valid if does, else invalid

    Not sure what any of that means or if its relevant at all (I don't know squat about programming, I'm just trying to be a good friend). Please help!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Nevermind
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    I guess it does not matter any longer, but what is all that nonsense directing us to another site using a URL you couldn't post when you could have as easily pasted teh code here directly!?

    C# Code:
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace UPC
    {
        class Program
        {
            static void Main(string[] args)
            {
                checkCode("076281703220"); //12 numbers representing upc code
            }
     
            private static void checkCode(string code)
            {
                int total = 0;
                for (int i = 0; i < code.Length; i++)
                {
                    Console.WriteLine(code.Substring(i, 1) + " rest:" + i % 2); //test (if rest 0 multiply value by 3 if rest 1 take value(multiply by 1)
                    if (i % 2 == 1)
                    {
                        Console.WriteLine("added" + Int32.Parse(code.Substring(i, 1)));// test
                        total += Int32.Parse(code.Substring(i, 1)); //rest = 1 so add value
                    }
                    else
                    {
                        Console.WriteLine("added" + (Int32.Parse(code.Substring(i, 1)) * 3));  //test
                        total += (Int32.Parse(code.Substring(i, 1)) * 3); // rest = 3 so add value*3                    
                    }
                }
                Console.WriteLine(total);
                if (total % 10 == 0) //correct UPC
                {
                    Console.WriteLine("UPC IS OK");
                }
                Console.ReadLine();
            }
        }
    }


    This is very simple C# code - C# and C++ share common control-flow constructs and operators; the differences are largely at the "framework" level and in memory management and the way classes are defined and objects instantiated and destroyed. This code does not really use any of that - comprising of only a main() function and one other function. A translation really need not even have the additional function since that is the only thing main() does.

    Translating this to C++ or even C is trivial; hence I guess the "Nevermind".

IMN logo majestic logo threadwatch logo seochat tools logo