Barcode reading for invalid inputs

I am currently writing a program for converting zip codes to barcodes. One requirement for the program is that the user must enter 5 digits for the zip code, any more or any less should yield an error message. The program must also give an error if anything entered is not a number 0-9. My program currently runs well as intended when inputting valid zip codes, but does not work when an invalid input is entered. I tried using .length but it gave the error of "expression must have a class type". Any help would be greatly appreciated, thank you.

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
70
71
72
73
74
75
76
77
78
  #include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
string getDigitCode(char input);
int getCheckDigitValue(int sum);
string barcode(int sum);

int main(int argc, char const *argv[]) {
  int sum;
  string barValue; 
  cout << "Enter zipcode:  " << endl;
  cin >> sum;
  if(sum.length() > 5); //error "Expression must have class type" 
cout << "You have entered an invalid zip code length";
 
 if(sum > 0)
  {             
    barValue = barcode(sum);
    cout << "Your barcode is: ";
    cout << barValue << endl;
  }
  else
  {    
    cout << endl << "You have entered an invalid zip code." << endl;
  }
  return 0;
} /// main


string getDigitCode (char input){
  if (input == 0) return "||:::";
  else if (input == 1) return ":::||";
  else if (input == 2) return "::|:|";
  else if (input == 3) return "::||:";
  else if (input == 4) return ":|::|";
  else if (input == 5) return ":|:|:";
  else if (input == 6) return ":||::";
  else if (input == 7) return "|:::|";
  else if (input == 8) return "|::|:";
  else if (input == 9) return "|:|::";
  else return "Error: Code is invalid.";
}

string barcode(int sum)
{
  int checkDigit = getCheckDigitValue(sum);
  int first;
  int second;
  int third;
  int fourth;
  int fifth;
  first = sum % 10;
  sum = sum / 10;
  second = sum % 10;
  sum = sum / 10;
  third = sum % 10;
  sum = sum / 10;
  fourth = sum % 10;
  sum = sum / 10;
  fifth = sum % 10;
  sum = sum / 10;
  string barcode = "|" + getDigitCode(fifth) + getDigitCode(fourth) + getDigitCode(third) + getDigitCode(second) + getDigitCode(first) + getDigitCode(checkDigit) + "|";
return (barcode);
}

int getCheckDigitValue (int sum)
{
  int sumDigits = 0;
  int checkDigit;
  while(sum)
  {
    sumDigits = sumDigits + sum % 10;
    sum = sum / 10;
  }
  checkDigit = 10 - (sumDigits % 10);
  return checkDigit;
}
Last edited on
Hello noahk81,
You need to learn how to use blank lines to break up your code. It will not only help you, but make it easier for others to read. The easier it is to read the quicker your response. But when someone try reading your code it takes longer or the code will have to be adjusted to make it easier to read. Some may work through your code and find your problem other may not want to take the time to figure out what you have posted. It will take a few extra minutes to unravel your code to see what is going on.

Andy
Try this:

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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

string getDigitCode(int input);
int getCheckDigitValue(int sum);
string barcode(int sum);

int main()
{
	string zip;

	cout << "Enter zipcode: ";
	getline(cin, zip);

	if (zip.length() != 5)
		return (cout << "You have entered an invalid zip code length\n"), 1;

	char* last;
	const int sum = strtol(zip.c_str(), &last, 10);

	if (*last)
		return (cout << "You have entered an invalid zip code\n"), 2;

	cout << "Your barcode is: " << barcode(sum) << endl;
}

string getDigitCode(int input)
{
	static const string zip[10] {"||:::", ":::||", "::|:|", "::||:", ":|::|", ":|:|:", ":||::",	"|:::|", "|::|:", "|:|::"};

	if ((input >= 0) && (input <= 9))
		return zip[input];

	return "(bad)";
}

string barcode(int sum)
{
	const int checkDigit = getCheckDigitValue(sum);
	std::cout << checkDigit << endl;

	const int first = sum % 10;
	sum /= 10;
	const int second = sum % 10;
	sum /= 10;
	const int third = sum % 10;
	sum /= 10;
	const int fourth = sum % 10;
	sum /= 10;
	const int fifth = sum % 10;

	return "|" + getDigitCode(fifth) + getDigitCode(fourth) + getDigitCode(third) + getDigitCode(second) + getDigitCode(first) + getDigitCode(checkDigit) + "|";
}

int getCheckDigitValue(int sum)
{
	int sumDigits = 0;

	while (sum)
	{
		sumDigits += sum % 10;
		sum /= 10;
	}

	return (10 - (sumDigits % 10)) % 10;
}


Note that check digit function has a logic error as it could return 10 if sumDigits is a multiple of 10.
Last edited on
Hello noahk81,


My program currently runs well as intended when inputting valid zip codes


I do not see how with the errors that it has.

I am not sure how it is working for yo when you are missing the header file "string".

"main" starts with:
1
2
3
4
5
6
7
8
int sum;
string barValue;

cout << "Enter zipcode:  " << endl;
cin >> sum;

if (sum.length() > 5); //error "Expression must have class type" 
cout << "You have entered an invalid zip code length";

This works down to line 7.

The ".length()" and the ".size()", both return the same number, are member functions of several classes. In this case it would be the "string" class. The problem is that "sum" is an "int" and has not member function because it is not a class.

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
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others. [isdigit()].
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;

string getDigitCode(char input);
int getCheckDigitValue(int sum);
string barcode(int sum);

int main(int argc, char const *argv[])  // <--- You do not use "argc" or "argv" in the program. Do you really need it?
{
    int numericZipCode;
    std::string stringZipCode;
    string barValue;

    cout << "Enter zipcode:  " << endl;
    cin >> stringZipCode;

    if (stringZipCode.length() != 5);
    cout << "You have entered an invalid zip code length";

    // <--- check if each element of the string is a digit.
    // if yes change the string to an int.
    // else end the program.
    // include the header file "cctype" http://www.cplusplus.com/reference/cctype/


This should give you an idea what to do with:

One requirement for the program is that the user must enter 5 digits for the zip code, any more or any less
should yield an error message.

The program must also give an error if anything entered is not a number 0-9.



Andy
I am not sure how it is working for yo when you are missing the header file "string".

This one's easy - standard headers often include other standard headers. <string> could easily be included by one of the other headers the OP includes.
Topic archived. No new replies allowed.