going from numbers to barcode

I need help going from numbers in a code and encoding this into a barcode in a function. Honestly I'm just really lost and dont even know what to do and I could really use some help.
This is all i have so far :( I'm just really stuck on how to do this. I'm sorry I'm just terrible at C++ and coding in general
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


string bar_code(int zip)
{
	int n = zip;
	string r;
	if (n = 0)
	{
		r = "||:::";
	}
	if (n = 1)
	{
		r = ":::||";
	}
	if (n = 2)
	{
		r = "::|:|";
	}
	if (n = 3)
	{
		r = "::||:";
	}
	if (n = 4)
	{
		r = ":|::|";
	}
	if (n = 5)
	{
		r = ":|:|:";
	}
	if (n = 6)
	{
		r = ":||::";
	}
	if (n = 7)
	{
		r = "|:::|";
	}
	if (n = 8)
	{
		r = "|:::|";
	}
	if (n = 9)
	{
		r = "|:|::";
	}
	return 0;
}

int main()
{
	int zip;
	cin >> zip;

	string code = bar_code(zip);
	cout << code << endl;

}


also just to make this a little more clear its supposed to be that a person enter a 5 digit code (like 90128) and the coder will output the code with the :'s and the |'s
Last edited on
sorry I shouldve specified. I can't use that code. Some of the stuff in there hasn't been taught yet and it wont be accepted. Thats why im trying to make a new one :)
Your conditions are assigning to n, and you don't return r.
You've got two issues to solve.

One is to convert a single digit to its representation. It should look very much like your function above:

1
2
3
4
5
6
string digit_to_barcode( int digit )  // digit is in [0,9]
{
	if      (digit==0) return "||:::";
	else if (digit==1) return ":::||";
	...
}

The next issue is to convert a number to digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
string zip_to_barcode( int zip )  // five digit number!
{
	string r;
	r = digit_to_barcode( zip % 10 );  // convert one's place digit to code
	zip /= 10;  // get rid of one's place

	r = digit_to_barcode( zip % 10 ) + r;  // convert ten's place
	zip /= 10;  // get rid of ten's place

	...

	return r;
}

Hope this helps.
Here you go! Read the Comments Properly to understand the code and try to make it yourself next time :) so that you can improve your programming skills.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;

//This Function Takes one digit at a time and returns the assosiated barcode string
string Bar_Code(char digit)
{
    if (digit == '0')
        return  "||:::";
    else if (digit == '1')
        return ":::||";
    else if (digit == '2')
        return "::|:|";
    else if (digit == '3')
        return "::||:";
    else if (digit == '4')
        return ":|::|";
    else if (digit == '5')
        return ":|:|:";
    else if (digit == '6')
        return ":||::";
    else if (digit == '7')
        return "|:::|";
    else if (digit == '8')
        return "|:::|";
    else if (digit == '9')
        return "|:|::";
    else
        return "Invalid";

}

int main()
{
    //Declaration
    string user_input;
    string result;

    //Take Input the number as a string :) This will make it easy to work on individual digits
    cout << "Enter Number: ";
    cin >> user_input;

    //Now Pass Digits One by One
    for(int i=0;i<user_input.length();i++)
    {
        //Storing barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
        string current_barcode = Bar_Code(user_input[i]);

        //The Line Below Will Print Each Digit present in user_input
        //cout << user_input[i] << endl;
        
        //Checking for invalid digit -- Because they are characters anyone can enter other than a number
        if(current_barcode.compare("Invalid") == 0)
        {
            cout << "The Number You Entered in Invalid!" << endl;
            break;
        }

        //Concatenating the barcode string returned by Bar_Code() after taking a digit that is 'user_input[i]'
        result = result + current_barcode;
    }

    //Printing Result
    cout << result;

    cout << endl;
    cin.ignore();
    return 0;
}
i have a question really quick for @Atyab.
Why is the line cout << user_input[i] << endl; noted out? Do i not need it. The code worked perfectly and i do understand it but i just dint understand what that line would be for.
But i really truly appreciate your help sooo much!! Thank you soo much!!!
Line 49 would have output one character per line for each character from user_input each time through the loop. It may have been there for debugging. It's certainly not required since the value the user input at line 40 is on the screen.
@ppate215 as mentioned above Line 49 is just for Debugging purpose because it is good to see that what ever you are getting in a loop is correct or not.

You don't need that in your program.
Topic archived. No new replies allowed.