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
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace 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
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 :)
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"||:::";
elseif (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;
}
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.
#include<iostream>
usingnamespace std;
//This Function Takes one digit at a time and returns the assosiated barcode string
string Bar_Code(char digit)
{
if (digit == '0')
return"||:::";
elseif (digit == '1')
return":::||";
elseif (digit == '2')
return"::|:|";
elseif (digit == '3')
return"::||:";
elseif (digit == '4')
return":|::|";
elseif (digit == '5')
return":|:|:";
elseif (digit == '6')
return":||::";
elseif (digit == '7')
return"|:::|";
elseif (digit == '8')
return"|:::|";
elseif (digit == '9')
return"|:|::";
elsereturn"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.