E
ehabaziz2001
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>
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>