Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Product Code Reader

Options
  • 11-09-2012 1:18am
    #1
    Registered Users 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 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