Strings Became Symbols When They Output in C -
i have problem when output strings in codes. when input 1 string , output it, there no problem @ all. but, when input @ least 2 strings, went fail. strings became symbols, except last one. here screenshot:
i've been using fflush(stdin) , fflush(stdout), problem still exist. so, should do? need advice.
here complete codes:
#include <stdio.h> #include <stdlib.h> void menu(); void entry(); void search(); void printsingle(); void printcomplete(); float totalused(int i); float regularcost(int i); float tax(int i); float discount(int i); float totalpayment(int i); #define nmaks 101 typedef enum {false=0,true=1} boolean; typedef struct {int billnumber,billclass;float lastmeter,currentmeter;char name[];} billdatabase; billdatabase bill[nmaks]; int dataamount=0; int main() { menu(); return 0; } void menu() { int i; repeat: system("cls"); printf("\t\t.: electric billing system :.\n\n"); printf("[1] entries customer information\n"); printf("[2] search customer\n"); printf("[3] print single bill\n"); printf("[4] print complete billing report\n"); printf("[5] exit\n\n"); printf("select menu[1..5]: ");scanf("%d",&i); switch (i) { case 1 : entry();break; case 2 : search();break; case 3 : printsingle();break; case 4 : printcomplete();break; case 5 : { printf("\n\ngoodbye!"); getch(); break; } default : { printf("\n\nwrong menu!"); goto repeat; } } } void entry() { char repeat; { system("cls"); printf("\t\t.: electric billing system :.\n\n"); printf("[1] entries customer information\n\n"); dataamount++; printf("bill number: ");scanf("%d",&bill[dataamount].billnumber); printf("customer name: ");fflush(stdin);fflush(stdout);gets(bill[dataamount].name); printf("class[1..3]: ");scanf("%d",&bill[dataamount].billclass); printf("last meter: ");scanf("%f",&bill[dataamount].lastmeter); printf("current meter: ");scanf("%f",&bill[dataamount].currentmeter); printf("\nentry again[y/n]: ");repeat=getche(); } while (tolower(repeat)=='y'); menu(); } void search() { int i,bn; boolean found; char repeat; { system("cls"); printf("\t\t.: electric billing system :.\n\n"); printf("[2] search customer\n\n"); printf("enter bill number: ");scanf("%d",&bn); found=false; (i=1;i<=dataamount;i++) if (bn==bill[i].billnumber) { found=true; break; } if (found) { printf("\ncustomer found!\n\n"); printf("bill number\tn m e class\t\tlast meter\tcurrent meter\n"); fflush(stdin);fflush(stdout); printf("%d\t\t%15.15s\t%d\t\t%.2f kwh\t%.2f kwh\n",bill[i].billnumber,bill[i].name,bill[i].billclass,bill[i].lastmeter,bill[i].currentmeter); } else printf("\ncustomer not found!\n"); printf("\nsearch again[y/n]: ");repeat=getche(); } while (tolower(repeat)=='y'); menu(); } void printsingle() { int i,bn; boolean found; char repeat; { system("cls"); printf("\t\t.: electric billing system :.\n\n"); printf("[3] print single bill\n\n"); printf("enter bill number: ");scanf("%d",&bn); found=false; (i=1;i<=dataamount;i++) if (bn==bill[i].billnumber) { found=true; break; } if (found) { printf("\ncustomer found!\n\n"); printf("bill number\tn m e class\t\tlast meter\tcurrent meter\n"); fflush(stdin);fflush(stdout); printf("%d\t\t%15.15s\t%d\t\t%.2f kwh\t%.2f kwh\n",bill[i].billnumber,bill[i].name,bill[i].billclass,bill[i].lastmeter,bill[i].currentmeter); printf("total used\tregular cost\ttax\t\tdiscount\ttotal payment\n"); printf("%.2f kwh\t$%7.2f\t$%7.2f\t$%7.2f\t$%7.2f\n",totalused(i),regularcost(i),tax(i),discount(i),totalpayment(i)); } else printf("\ncustomer not found!\n"); printf("\nprint again[y/n]: ");repeat=getche(); } while (tolower(repeat)=='y'); menu(); } void printcomplete() { int i; system("cls"); printf("\t\t.: electric billing system :.\n\n"); printf("[4] print complete billing report\n\n"); printf("bill number\tn m e\t\tclass\ttotal payment\n"); (i=1;i<=dataamount;i++) { fflush(stdin);fflush(stdout); printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].billnumber,bill[i].name,bill[i].billclass,totalpayment(i)); } printf("\npress key main menu"); getch(); menu(); } float totalused(int i) { return bill[i].currentmeter-bill[i].lastmeter; } float regularcost(int i) { float price; switch (bill[i].billclass) { case 1 : price=10.0;break; case 2 : price=7.5;break; default : price=13.75; } return totalused(i)*price; } float tax(int i) { float taxpercentage; switch (bill[i].billclass) { case 1 : taxpercentage=1.5/100;break; case 2 : taxpercentage=0.25/100;break; default : taxpercentage=3.5/100; } return regularcost(i)-(regularcost(i)*taxpercentage); } float discount(int i) { float discountpercentage; switch (bill[i].billclass) { case 1 : discountpercentage=3.0/100;break; case 2 : discountpercentage=2.0/100;break; default : discountpercentage=5.5/100;break; } if (regularcost(i)>100.0) return regularcost(i)-(regularcost(i)*discountpercentage); else return 0.0; } float totalpayment(int i) { return regularcost(i)+tax(i)-discount(i); }
note: use code blocks ide.
i see number of problems here.
first of all, over-use of global variables troubling. array bill
, int dataamount
should definitely not global. not c programming practice , not trust code looks this. please give "menu
", "entry
", "printcomplete
", , "search
" functions arguments can make these variables local, not global.
second, there few cases acceptable have many statements on same line in
printf("customer name: ");fflush(stdin);fflush(stdout);gets(bill[dataamount].name);
i'm getting headache looking @ it!!! pleeease don't!
third, not using array bounds correctly in loop, which cause of bug asking in first place.
(i=1;i<=dataamount;i++) { fflush(stdin);fflush(stdout); printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].billnumber,bill[i].name,bill[i].billclass,totalpayment(i)); }
remember, in c language, array indices start @ 0 , end @ "n - 1". loop starting @ 1 , ending @ "n".
the reason bogus character accessing billdatabase
outside boundaries of array bill
.
fourth, sami kuhmonen said, not allocating memory name
array. although not causing bug seeing, cause program crash segmentation fault
@ point in future. in order fix looming problem, have 2 options
the easy way out: change
billdatabase
struct followingtypedef struct { int billnumber, billclass; float lastmeter, currentmeter; char name[128]; } billdatabase;
this easy, not good, because name can never more 128 chars. also, please don't ever put entire struct on 1 line. started hitting myself on head saxophone.
the harder way out: learn use
malloc()
, , manually allocate name arrays.
Comments
Post a Comment