unexpected character displayed infront of the variable

Discussion in 'C Programming' started by ehabaziz2001@gmail.com, Mar 23, 2007.

  1. Guest

    My Program turned a char variable into string using null character but
    unexpected character displayed infront of the variable after using
    printf .

    Syntax :
    -------------
    #include <stdio.h>

    struct record
    {
    char cust_name[50];
    int cust_no;
    int week_no;
    char type;
    float type_value;
    float bill;
    };


    void invoice_entry(struct record *);
    void print_invoice(struct record *);

    char another(void);


    void main(void)
    {
    struct record cust;

    do
    {
    system("cls");
    invoice_entry(&cust);
    print_invoice(&cust);


    } while (another()=='y');
    }

    void invoice_entry(struct record *cust)
    {
    int i=0;
    printf ("Enter the Customer name : ");
    do
    {
    i++;
    (*cust).cust_name=getchar();
    } while ((*cust).cust_name!='\n');
    (*cust).cust_name[i+1]='\0';



    printf("Enter the customer number : ");
    scanf("%4d",&(*cust).cust_no);
    printf("Enter the weeks number : ");
    scanf("%4d",&(*cust).week_no);
    printf("Enter type option 1 FOR \"1.5\" 2 FOR \"2.5\" 3 FOR
    \"3.5\" : ");
    scanf("\n%c",&(*cust).type);
    switch ((*cust).type)
    {
    case '1':
    (*cust).type_value=1.5;
    break;
    case '2':
    (*cust).type_value=2.5;
    break;
    case '3':
    (*cust).type_value=3.25;
    break;
    default :
    break;
    }

    (*cust).bill=(*cust).type_value*(*cust).week_no;
    }

    void print_invoice(struct record *cust)
    {
    printf("\n");
    printf("Invoice\n");
    printf("------- ");
    printf("\nCust# CustName Weeks# TypeValue
    Bill \n");
    printf ("----- -------- ------ ---------
    ---- \n");
    printf ("%-4d %-40s %-4d %-4.2f
    L.E %-4.2f",(*cust).cust_no,(*cust).cust_name,
    (*cust).week_no,(*cust).type_value,(*cust).bill);
    }


    char another(void)
    {
    char ans;
    printf("\n\nEnter another customer (y/n) : ");
    scanf("\n%c",&ans);
    return (ans);
    }



    Output:
    ----------
    Enter the Customer name : 6667yy
    Enter the customer number : 7
    Enter the weeks number : 8
    Enter type option 1 FOR "1.5" 2 FOR "2.5" 3 FOR "3.5" : 2

    Invoice
    -------
    Cust# CustName Weeks# TypeValue Bill
    ----- -------- ------ --------- ----
    7 ├6667yy
    8 2.50
    L.E 20
    ..00

    Enter another customer (y/n) : n
    E:\programs\unix\c_lang\iti01\ch07\own>
    , Mar 23, 2007
    #1
    1. Advertising

  2. user923005 Guest

    On Mar 23, 12:35 pm, wrote:
    [snip]
    > do
    > {
    > i++;
    > (*cust).cust_name=getchar();} while ((*cust).cust_name!='\n');


    do {
    (*cust).cust_name[i++] = getchar();
    } while ((*cust).cust_name != '\n');

    Or better yet (by far):

    fgets(cust->cust_name, sizeof cust->cust_name, stdin);

    [snip]
    user923005, Mar 23, 2007
    #2
    1. Advertising

  3. user923005 Guest

    On Mar 23, 12:43 pm, "user923005" <> wrote:
    > On Mar 23, 12:35 pm, wrote:
    > [snip]
    >
    > > do
    > > {
    > > i++;
    > > (*cust).cust_name=getchar();} while ((*cust).cust_name!='\n');

    >
    > do {
    > (*cust).cust_name[i++] = getchar();
    > } while ((*cust).cust_name != '\n');
    >
    > Or better yet (by far):
    >
    > fgets(cust->cust_name, sizeof cust->cust_name, stdin);
    >
    > [snip]


    Probably better yet is Jack Klein's getsafe() function, which will
    remove the trailing newline character:

    #include <stdio.h>
    #include <string.h>
    char *getsafe(char *buffer, int count)
    {
    char *result = buffer, *np;
    if ((buffer == NULL) || (count < 1))
    result = NULL;
    else if (count == 1)
    *result = '\0';
    else if ((result = fgets(buffer, count, stdin)) != NULL)
    if (np = strchr(buffer, '\n'))
    *np = '\0';
    return result;
    }
    user923005, Mar 23, 2007
    #3
  4. Guest

    using the above method I had to press enter more than one time . Pls
    adjust the above the method using getchar() :
    do {
    (*cust).cust_name[i++] = getchar();
    } while ((*cust).cust_name != '\n');


    On Mar 23, 9:43 pm, "user923005" <> wrote:
    > On Mar 23, 12:35 pm, wrote:
    > [snip]
    >
    > > do
    > > {
    > > i++;
    > > (*cust).cust_name=getchar();} while ((*cust).cust_name!='\n');

    >
    > do {
    > (*cust).cust_name[i++] = getchar();
    > } while ((*cust).cust_name != '\n');
    >
    > Or better yet (by far):
    >
    > fgets(cust->cust_name, sizeof cust->cust_name, stdin);
    >
    > [snip]
    , Mar 24, 2007
    #4
  5. On 23 Mar 2007 12:43:52 -0700, "user923005" <> wrote:

    >On Mar 23, 12:35 pm, wrote:
    >[snip]
    >> do
    >> {
    >> i++;
    >> (*cust).cust_name=getchar();} while ((*cust).cust_name!='\n');

    >
    > do {
    > (*cust).cust_name[i++] = getchar();
    > } while ((*cust).cust_name != '\n');


    This invokes undefined behavior. By incrementing i inside the do
    block, you guarantee that the while clause tests an element of
    cust_name that has not yet received a value. Attempting to evaluate
    an indeterminate value is prohibited.

    >
    >Or better yet (by far):
    >
    > fgets(cust->cust_name, sizeof cust->cust_name, stdin);
    >
    >[snip]



    Remove del for email
    Barry Schwarz, Mar 24, 2007
    #5
  6. On 24 Mar 2007 00:56:31 -0700, wrote:

    >using the above method I had to press enter more than one time . Pls
    >adjust the above the method using getchar() :
    > do {
    > (*cust).cust_name[i++] = getchar();
    > } while ((*cust).cust_name != '\n');


    Please don't top post. Insert your text at the appropriate point in
    the quoted material or at the end.

    Since user923005's code inovkes undefined behavior, don't use it.

    >
    >
    >On Mar 23, 9:43 pm, "user923005" <> wrote:
    >> On Mar 23, 12:35 pm, wrote:
    >> [snip]
    >>
    >> > do
    >> > {
    >> > i++;
    >> > (*cust).cust_name=getchar();} while ((*cust).cust_name!='\n');

    >>
    >> do {
    >> (*cust).cust_name[i++] = getchar();
    >> } while ((*cust).cust_name != '\n');
    >>
    >> Or better yet (by far):
    >>
    >> fgets(cust->cust_name, sizeof cust->cust_name, stdin);
    >>
    >> [snip]

    >



    Remove del for email
    Barry Schwarz, Mar 24, 2007
    #6
  7. CBFalconer Guest

    wrote:
    >
    > using the above method I had to press enter more than one time .
    > Pls adjust the above the method using getchar() :
    > do {
    > (*cust).cust_name[i++] = getchar();
    > } while ((*cust).cust_name != '\n');


    Don't top-post. By so doing you have lost all context.

    Try:
    int ch;

    while ((EOF != (ch = getchar)) && ('\n' != ch)) {
    if (i < SOMEMAXINDEX) cust->cust)name[i++] = ch;
    }

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 24, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xuanly ly via .NET 247

    asp:dropdownlist display infront all the time

    Xuanly ly via .NET 247, Mar 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    aschmidt
    Mar 18, 2005
  2. A Causal
    Replies:
    6
    Views:
    419
    Derk Gwen
    Oct 16, 2003
  3. boney
    Replies:
    1
    Views:
    550
  4. Christopher
    Replies:
    1
    Views:
    406
    Christopher
    Dec 15, 2011
  5. Richard Lionheart
    Replies:
    5
    Views:
    109
    Jean-Hugues ROBERT
    Jun 7, 2004
Loading...

Share This Page