Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ Product Code Reader

  • 11-09-2012 01:18AM
    #1
    Registered Users, Registered Users 2 Posts: 1


    Hi guys, I'm fairly new at programming and I've worked on a project with my friend that I'm attempting to convert to C++ from C#, as that's his native language and he suggested I try to convert it for practice, as my studies require C++.

    This is the original 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 the code that I've come up with after converting it to the best of my ability (with some help):
    #include <iostream> 
    #include <string>
    using namespace std;
    void checkCode(string code)
     
    {
            int total = 0;
            for (int i(0); i<code.length(); i++)
            {
                    cout << code.substr(i,1) << " rest: " << i %2 << endl;
                    if (i % 2 == 1)
                    {
                            cout << " added " << code.substr(i,1) << endl;
                            total += atoi(code.substr(i,1).c_str());
                    }
     
                    else
                    {
                            cout << " added " << atoi(code.substr(i,1).c_str()) * 3 << endl;
                            total += (atoi(code.substr(i,1).c_str()) * 3);
                    }
            }
     
            cout << total;
    
            if (total % 10 == 0)
     
            {
                    cout << "UPC IS OK" << endl;
            }
            cin >> total;
     
    }
     
     
     
    int main ()
    {
     checkCode("076281703220"); 
    }
    

    My problem is this (and I'm assuming it's fairly an easy fix) -- I need a more efficient way to take say, 15 or 20 product codes, so do I just copypaste this: checkCode("076281703220"); however many times I need to in the main method to check whichever codes I need to?

    Also, it says I have an error and that atoi is not defined within the scope. Any help is appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    You need to include the right header to use atoi: #include <stdlib.h>

    For the codes you should put them in an xml file, then read that in. There are many xml libraries for c++, I use one called Xerces. Failing that a simple list of items seperated by newlines can be easily read manually.


Advertisement