Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to print error for excess variables

ravi100ravi100 Posts: 3Member
im writing a barcode program using C and have the following so far done:
#include
int main(void) {

int b1;
int b2;
int b3;
int b4;
int b5;
int b6;
int b7;
int b8;
int b9;
int b10;
int b11;
int b12;
int c1;


printf("please enter barcode:
");
scanf("%1d", &b1);
scanf("%1d", &b2);
scanf("%1d", &b3);
scanf("%1d", &b4);
scanf("%1d", &b5);
scanf("%1d", &b6);
scanf("%1d", &b7);
scanf("%1d", &b8);
scanf("%1d", &b9);
scanf("%1d", &b10);
scanf("%1d", &b11);
scanf("%1d", &b12);
scanf("%1d", &c1);
printf("%d%d%d-%d%d%d%d%d%d-%d%d%d-%d.
" ,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,c1);

firstly is there an easier way of doing this ie. getting input from user and arranging it in the from 111-111111-111-1? without using functions, strings or arrays (throughout the program).
and i also would like to know how to print an error message if the input exceeds 13 integers or is less then 12 integers.

if anyone could help that would be much appreciated. i don't really no much C, pretty much self teaching.
thank-you.


Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Nothing can be programmed without functions, strings or arrays. Those things are easy.

    What is a purpose of the code? Taking input and then printing it - not much business sense in it.

    I suggest entering all digits as the string and then checking if the string length is 12 or 13 characters. If not - print an error. If yes - then continue with checking if each of the characters is a digit. If you see a non-digit - print an error. If not - you got a proper barcode.[/color]
    [code]
    #include
    #include
    #include
    #include

    [color=Green]// ----------------------------------------------------------[/color]
    char barcode [32];
    int length, i, nondigit, error;

    [color=Green]// ----------------------------------------------------------[/color]
    void PrintBarCodePart (char code [], int n)
    {
    for (i=0; i 13))
    {
    printf ("

    ERROR: BARCODE LENGTH MUST BE 12 OR 13 DIGITS.");
    error = 1;
    }
    else
    {
    for (i=0; i 12)
    {
    printf ("-%s", barcode+12);
    }
    }
    return 0;
    }
    [/code]
  • ravi100ravi100 Posts: 3Member
    hey mate
    thanks alot this really goes a long way for me. Programming is difficult at first but i think, with the help you have given me it kind of makes sense.

    again many thanks, and its greatly appreciated.
    :)
  • mariahsharpmariahsharp Posts: 47Member

    not knowing whether this barcode in c# helps ?

Sign In or Register to comment.