Bar code program

I need to create a program that, upon collecting two sets of five digit bar code numbers:

a.Sums the second and forth numbers in the manufacturers code.

b.Sums the first, third, and fifth numbers in the product code.

c.Sums the results of steps a and b and multiplies the total by 3.

d.Sums the first, third, and fifth numbers in the manufacturers code.

e.Sums the second and fourth digits in the product code.

f. Sums the results of steps d and e and adds the results from step c.

g. Takes the remainder when the result of step f is divided by 10.

h. Subtracts the results of step g from 10. Compares this result to the check digit entered by the user. If the result and check digit are equal (if the Boolean value of the comparison is 1.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// UPC.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int manufacturerCode, productCode, checkDigit = 0;

	cout << "Enter five digit manufacturer code ";
	cin >> manufacturerCode;
	cout << "Enter five digit product code ";
	cin >> productCode;

	return 0;
}


This is as far as I've gotten. How can I seperate the digits from the entered code for calculation by only using modulus, multiplication, and subtraction?
Read them into an std::string and use std::getline(std::cin, s) instead of std::cin.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main() {
    std::string manufacturerCode, productCode;
    unsigned int checkDigit = 0;

    while (manufacturerCode.length() != 5) {
        std::cout << "Five (5) digit manufacturer code: ";
        std::getline(std::cin, manufacturerCode);
    }

    while (productCode.length() != 5) {
        std::cout << "Five (5) digit product code: ";
        std::getline(std::cin, productCode);
    }

    /* variable */ = (manufacturerCode[2] + manufacturerCode[4]) - '0'; /* Sum the second and forth numbers in the manufacturers code
  */

    /* ... */
    return 0;
}


I take '0' (important: not 0, '0') because manufacturerCode[2] is a character, not an integer. The result of that calculation would otherwise be '0' (usually 48) too big.
Ok I am still new to programming and I don't understand why you read them into a std::string. That part is over my head.
Because a string is an array of characters.

I suppose you can use a loop with arrays of integers (have you learnt about arrays yet?) to do this, too. It would be a little easier for the rest of the program, for example

Can barcodes contain signed (e.g. positive AND negative) numbers, or just positive numbers? I wouldn't know. If they can only contain positive numbers, then use unsigned ints. It increases the range of numbers that an integer can store, because it doesn't have to store a sign (+ or -).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main() {
    int manufacturerCode[5] = {0},
         productCode[5] = {0}, /* Create two integer arrays, intialized to 0 */
         checkdigit = 0,
         i = 0;

    std::cout << "Five digit manufacturer code: ";

    for (i = 0; i < 5; i++) {
        manufacturerCode[i] = std::cin.get();
    }

    std::cout << "Five digit product code: ";

    for (i = 0; i < 5; i++) {
        productCode[i] = std::cin.get();
    }

   /* ... */

    return 0;

}
Topic archived. No new replies allowed.