Unused variable issue
Sep 30, 2020 at 1:17pm Sep 30, 2020 at 1:17pm UTC
Hi, I am currently writing a program for converting zip codes to bar codes. When attempting to compile my program it says that my variable checkDigit is set but not used. However I do believe I am using it and do not know how to fix this problem. Any help is 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
#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;
int checkDigit;
string barValue;
cout << "Enter zipcode: " << endl;
cin >> sum;
if (sum > 0)
{
checkDigit = getCheckDigitValue(sum);
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 "Invalid postal code" ;
}
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;
}
Sep 30, 2020 at 1:25pm Sep 30, 2020 at 1:25pm UTC
1 2 3 4 5 6 7
if (sum > 0)
{
checkDigit = getCheckDigitValue(sum);
barValue = barcode(sum);
cout << "Your barcode is: " ;
cout << barValue << endl;
}
You assign the result of the getCheckDigitValue function to a variable called checkDigit in your main function, but don't use the variable in main to actually do anything.
Last edited on Sep 30, 2020 at 1:27pm Sep 30, 2020 at 1:27pm UTC
Topic archived. No new replies allowed.