Program runs then says it stops working - c

The program is required to calculate total cost of items and display back to user the quantity of product, product and the price of products as a bill. Keep in mind I am only a beginner in programming in C so basic functions are used.
#include<stdio.h>
#include<string.h>
typedef struct
{
float price;
int quantity;
char product;
}Billing;
typedef struct
{
int code;
char product;
float price;
}Stocks;
main()
{
Stocks A;
Billing B;
FILE*s;
FILE*c;
printf(" Welcome to I.A.M Cash & Carry\n#8 New Trincity Industrial Estate\nTrincity\n");
printf("=================================\n");
void transfer(Billing,Stocks,FILE*,FILE*);
transfer(B,A,s,c);
}
void transfer(Billing B,Stocks A,FILE*s,FILE*c)
{
int count;
s=fopen("C:\\stock.txt","r");
int counter=0;
while(fgetc(s)!=EOF)
{
counter++;
}
fclose(s);
count=counter;
Stocks D[count];
int x;
s=fopen("C:\\stock.txt","r");
for(x=0;x<count;x++)
{
fscanf(s,"%d",&D[x].code);
fscanf(s,"%s",D[x].product);
fscanf(s,"%f",&D[x].price);
}
fclose(s);
void menu(Billing,Stocks[],FILE*,FILE*,int);
menu(B,D,s,c,count);
}
void menu(Billing B,Stocks D[],FILE*s,FILE*c,int count)
{
printf("1)Enter items for billing\n");
printf("2)Quit\n");
void choice(Billing,Stocks[],FILE*,FILE*,int);
choice(B,D,s,c,count);
}
void choice(Billing B,Stocks D[],FILE*s,FILE*c,int count)
{
int x;
printf("Enter choice:\n");
scanf("%d",&x);
if((x<1) || (x>2))
{
printf("Choice invalid\nPlease Re-enter\n");
menu(B,D,s,c,count);
}
if(x==1)
{
void submenu(Billing,Stocks[],FILE*,FILE*,int);
submenu(B,D,s,c,count);
}
if(x==2)
{
printf("Thank you, Have a great day\n");
}
}
void submenu(Billing B,Stocks D[],FILE*s,FILE*c,int count)
{
int x,z,quan,prod;
float sum=0;
float prices=D[x].price*quan;
c=fopen("C:\\bill.txt","w");
do{
printf("Select choice:\n");
printf("1)Enter items\n");
printf("2)Quit");
scanf("%d",&x);
if(x==1)
{
printf("Enter product code:");
scanf("%d",&prod);
for(z=0;z<count;z++)
{
if(prod==D[x].code)
{
printf("Enter quantity:");
scanf("%d",&quan);
fprintf(c,"%d ",quan);
fprintf(c,"%s ",prod);
fprintf(c,"%5.2f\n",D[x].price*quan);
sum= sum + prices;
}
}
}
if(x==2)
{
printf("Thank you, Have a great day\n");
}
}while(x!=2);
fclose(c);
void countbill(Billing,FILE*,float);
countbill(B,c,sum);
}
void countbill(Billing B,FILE*c,float sum)
{
int y;
c=fopen("C:\\bill.txt","r");
int x=0;
while(fgetc(c)!=EOF)
{
x++;
}
fclose(c);
y=x;
Billing V[y];
void displaybill(Billing[],FILE*,float,int);
displaybill(V,c,sum,y);
}
void displaybill(Billing V[],FILE*c,float sum,int y)
{
int x;
c=fopen("C:\\bill.txt","r");
for(x=0;x<y;x++)
{
fscanf(c,"%d",&V[x].quantity);
fscanf(c,"%s",V[x].product);
fscanf(c,"%f",&V[x].price);
printf("%d %s %f\n",V[x].quantity,V[x].product,V[x].price);
}
printf("Total=%f",sum);
}
Debugger output

im sorry im making this as an answer, i just want you to really read it.
this code have tons of problems, ill just clarify most of them:
1) you are not declaring any of your functions
2) the way you call most of your functions is wrong
3) you are using tons of variable without initializing them, like x
4)you cant put a non constant variable in array declaration, it must be a constant number, or use dynamic allocation. (for example Stocks D[count])
and this is only what i saw from general look.
its look like you have some homework to do before you continue this code.
hope its helping somehow.

Related

C program to print you are eligible if you are above 16 years old when entering birth year only, using functions get_input and validate_input

C program to print you are eligible if you are above 16 years old when entering birth year only, using functions get_input and validate_input. However, I am getting the same message saying "Sorry, you are not eligible" even if the age is more than 16. Please let me know where I am making the mistake.
#include <stdio.h>
void get_input(int x)
{
printf("Enter your birth year:");
scanf("%d", &x);
}
int validate_input(int y)
{
int year;
get_input(year);
{
if (y > 0){
if ((2021 - y) >= 16){
printf("You are eligible for the job!\n");}}
else{
printf("Sorry, you are not eligible for the job!\n");}
}
}
int main()
{
int birth_year;
validate_input(birth_year);
return 0;
}
You need to learn how to call a function, use a variable as function parameters and how a function returns a value. As it seems from your code that you are trying to develop your skills I am just trying to help you with exact solution. I hope that you understand the code well at first and try to implement all by yourself from the beginning.
#include <stdio.h>
int get_input()
{
int x;
printf("Enter your birth year:");
scanf("%d", &x);
return x;
}
void validate_input(int y)
{
if ((2021 - y) >= 16){
printf("You are eligible for the job!\n");}
else{
printf("Sorry, you are not eligible for the job!\n");
}
}
int main()
{
int birth_year, y;
y= get_input();
validate_input(y);
return 0;
}
There are a couple of problems with your approach, that I can see.
void get_input(int x)
{
printf("Enter your birth year:");
scanf("%d", &x);
}
This function takes an integer as a parameter, and overwrites the value passed in with the value entered by the user. That doesn't achieve anything, because the value is then discarded. It would be better to return an integer from the input function:
int get_input()
{
int x;
printf("Enter your birth year:");
scanf("%d", &x);
return x;
}
Second, we can simplify validate_input() greatly, and get it to return valuable information:
bool validate_input(int year)
{
return ((2021 - year) >= 16);
}
Finally, the reporting or handling of the result is best placed in main. (I say this because it's useful to separate out the logic, and the input, from the output.)
int main()
{
int year = get_input();
if(validate_input(year))
{
printf("You are eligible for the job!\n");}
}
else
{
printf("Sorry, you are not eligible for the job!\n");
}
return 0;
}
Finally, there's a problem with your fundamental approach. Someone's age depends not only upon their birth year, but whether they have had their birthday this year.
in the below function
int main()
{
int birth_year;
validate_input(birth_year);
return 0;
}
birth_year is taking a garbage value so take input here that's why it is giving wrong input.
Also, in this
int validate_input(int y)
{
int year;
get_input(year);
year is taking garbage value , either assign some value to it or take input.

Nested functions! error. please Enlighten me

Guys can you help me solve this problem is c programming. I dont get it why i get nested functions when i try to compile the code. I always got ISO forbids nested functions. Please help me understand the code. 3 days. im loosing my mind. Im still new to c programming. thank you!
#include<stdio.h>
#define lcost 35;
#define tax 0.85;
void readData(int* Len,int* Wid,float* Disc,float* cost);
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid);
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc);
int calcTPrice(float tPrice,float ctax,float sTotal);
void printMes(int Len, int Wid);
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float sTotal);
int main(void)
{
int x;
int Len, Wid;
float item;
float area, cost, Disc, iPrice, sTotal, tDisc, tPrice, ctax;
do{
system("cls");
printf("[1] Perform\n");
printf("[2] Exit progres\n");
printf("\n\nInput Selection:");
scanf("%d", &x);
switch (x)
{
case 1:
readData(&Len,&Wid,&Disc,&cost);
calcInPrice(area,iPrice,cost,Len,Wid);
calcSTotal(sTotal,Disc,iPrice,tDisc);
printMes(Len,Wid);
printCharges(area,cost,iPrice,Disc,tDisc,sTotal,ctax,tPrice);
printf("\n\nPress any key to return to main menu");
getch();
break;
case2:
system("cls");
printf("Exiting Program");
break;
default:
printf("\n\nInvalid Selection!");
}
}while(x < 3);
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
printf("Input Length of Room: ");
scanf("%d",Len);
printf("Input Width of Room: ");
scanf("%d",Wid);
printf("Input Discount of Customer: ");
scanf("%f",Disc);
printf("Input the cost per square foot: ");
scanf("%f",cost);
return;
}
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid)
{
area = Len * Wid;
iPrice = (lcost * area) + (cost * area);
return;
}
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc)
{
Disc = (Disc / 100)*iPrice;
sTotal = iPrice - (tDisc);
return;
}
int calcTPrice(float tPrice,float ctax, float sTotal)
{
ctax = sTotal * tax;
tPrice = sTotal + ctax ;
}
void printMes(int Len, int Wid)
{
printf("\n\t\tMEASUREMENT\n\n");
printf("Length\t\t %d ft", Len);
printf("Width\t\t %d ft", Wid);
printf("\nArea\t\t %d square ft", Len * Wid);
return;
}
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
float item = cost * area;
printf("\n\n\t\tCHARGES\n\n");
printf("DESCRIPTION\tCOST/SQ.FT.\tCHARGE");
printf("\n________\t _________\t______");
printf("\nCarpet \t%.2f \t %0.2f ",cost,item);
printf("\nLabor \t %lf \t %0.2f",lcost, area);
printf("\n\t\t\t__________");
printf("INSTALLED PRICE \t\t %.2f", iPrice);
printf("\nDiscount \t %.2f \t %.2f ",Disc,tDisc);
printf("\n\t\t\t\t______");
printf("\nSUBTOTAL\t\t\t %.2f",sTotal);
printf("\nTax\t\t\t\t %2f",ctax);
printf("\nTOTAL\t\t\t\t %.2f",tPrice);
return;
}
return 0;
}
Your code looks like this:
int main(void)
{
...
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
return 0;
}
which means that you're "nesting" all of your function definitions (readData, printCharges, etc.) inside your main function.
That's not allowed.
Instead, you need to write something more like this:
int main(void)
{
...
return 0;
}
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
You cannot define a function within another function in standard C.
You can declare a function inside of a function, but it's not a nested function.
gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.
The problem is that your main function doesn't end before you start to define your readData() function, so to the compiler it looks like you are defining "readData()" inside of main(). This is called nesting, and is not valid in C. This is one reason why it's important to choose a style for indentation and bracket placement in languages like C.
I'm going to help you figure out how to figure out how to fix it, by winding your program back to a better basis to start from, and you can gradually reintroduce the rest of your code -- don't just add it all in at once, but add it piece-by-piece and make sure it works as you go.
#include <stdio.h>
// proto-type, tells the compiler that we will define this function later.
void readData(int*, int*, float*, float*);
int main()
{
int Len, Wid;
float Disc, Cost;
printf("in main\n");
readData(&Len, &Wid, &Disc, &Cost);
return 0;
} // end of main()
void readData(int* Len, int* Wid, float* Disc, float* Cost)
{
printf("We're in readData now\n");
} // end of readData()
Here it is on ideone so you can see it works.

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

Calculator program issue in C

I'm writing a statistical calculator, with 3 different calculation options. The problem is whenever I choose the 2nd option, it wants to print both the answer from the 1st option and the 2nd option. When I choose the 3 option, it just prints the answer the the 3rd option(the wrong answer, but thats probably a mishap in the formula). Here is the results:
Please Enter a number of inputs
3
Please enter number 1
1
Please enter number 2
2
Please enter number 3
3
Statistical Calculator Menu
(1) Mean
(2) Standard Deviation
(3) Range
(4) Restart/Exit
2
Here is the Mean 2.0Standard Devition is 0.8
Now I thought it might be an issue with how I'm calling each function, but the best I can tell thats not the case. Then I thought it might be a value that I didn't initialize, but it seems as though thats not it either. I just need another pair of eyes to see where I went wrong here.
#include <stdio.h>
#include <conio.h>
#include <math.h>
const int MAX_DATA=8;
void menu(float numbers[], int amount);
float mean(float numbers[],int amount);
float standard_dev(float numbers[], int amount);
float range( float numbers[], int amount);
int main()
{
int i=0, amount=0;
float numbers[MAX_DATA];
printf("Please Enter a number of inputs \n");
scanf("%d", &amount);
if (amount>MAX_DATA)
{
printf("You entered too many numbers");
}
else
{
for (i=0; i<amount; i++)
{
printf("Please enter number %d\n", i+1);
scanf("%f",&numbers[i]);
}
menu(numbers,amount);
}
getch();
return 0;
}
void menu(float numbers[],int amount)
{
int input2=0;
printf("Statistical Calculator Menu");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit\n");
scanf("%d",&input2);
if(input2==1)
{
mean(numbers,amount);
}
if (input2==2)
{
standard_dev(numbers,amount);
}
if (input2==3)
{
range(numbers,amount);
}
}
float mean(float numbers[],int amount)
{
int i;
float sum=0;
float average=0;
for (i=0; i<amount; i++)
{
sum=sum+numbers[i];
}
average=sum/amount;
printf("Here is the Mean %.1f", average);
return average;
}
float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount);
for (i=0; i<amount; i++)
{
dev=numbers[i]-mean2;
sumsqr+=dev*dev;
}
variance=sumsqr/(float)amount;
sdev=sqrt(variance);
printf("Standard Devition is %.1f", sdev);
return sdev;
}
float range(float numbers[],int amount)
{
int i;
float diff=0;
for (i=0; i<=amount; i++)
{
diff=numbers[amount]-numbers[1];
}
printf("%f\n",diff);
return diff;
}
float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount); // Here it is.
It calls the mean function, which actually prints something :) You can add boolean flag shouldPrint to functions and pass it as true when you want to print it.
Also, this problem is easily solvable with simple debugging your code...if actually looking at it doesn't seem to help...

Calculate Reverse Matrix?

Why should i get crash with this , where did i wrong !? :(
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
const volatile max=15;
int read(float[][max],float[][max]);
void compute1(float[][max],float[][max],float[][max],int,int,int,float);
float compute2(float[][max],int);
void display(float[][max],float[][max],int,float);
int main(){
float num[max][max],g[max][max],v[max][max],a[max][max];
int dn,u;
float det;
int register i,j;
dn=read(num,a);
det=compute2(num,dn);
for(i=0;i<dn;i++)
for(j=0;j<dn;j++){
compute1(a,g,v,dn,i,j,det);
}
display(a,v,dn,det);
getch();
return 0;
}
//****************************************************************************
int read(float num[][max],float a[][max]){
int dn;
clrscr();
int register i,j;
printf("\nenter degree of matrix:");
scanf("%d",&dn);
clrscr();
for(i=0;i<dn;i++){
printf("\n\n\nenter arguments of row[%d]:\n\n",i);
for(j=0;j<dn;j++){
scanf("%f",*(num+i)+j);
*(*(a+i)+j)=*(*(num+i)+j);
}
}
return dn;
}
//****************************************************************************
void display(float c[][max],float inv[][max],int dn,float det){
int register i,j;
clrscr();
printf("\n\n\n\n\n\t\t\t --ORIGINAL MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.3f",c[i][j]);
}
printf("\n\n\n\t\t\t --INVERSE MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.6f ",inv[i][j]);
}
printf("\n\n\n\t\tعؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤ؟");
printf("\n\t\t³ determinan of matrix= %19.7f ³ ",det);
printf("\n\t\tہؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤظ");
}
//****************************************************************************
void compute1(float g[][max],float v[][max],float inv1[][max],int dn,int r,int c,float e){
int col=0,row=0,add=r+c,y;
float y1=1;
if(add%2)
y1=-1;
int register i,j;
for(i=0;i<dn-1;i++){
if(i==r)
row=1;
col=0;
for(j=0;j<dn-1;j++){
if(j==c)
col=1;
v[i][j]=g[i+row][j+col];
}
}
inv1[c][r]=y1*compute2(v,dn-1)/e;
}
//****************************************************************************
float compute2(float c[][max],int s){
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
int register i,j;
while(y>0){
for(i=1;i<k;i++){
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
}
y--;
k--;
for(i=0;i<k;i++)
for(j=s-1;j>=0;j--)
c[y-i][j]=c[y-i][j]-h[i]*c[y+1][j];
}
for(i=0;i<s;i++)
h1*=c[i][i];
return h1;
}
There's quite a number of problems with your code. In compute2 you're allocating memory for.. a row?
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
You're allocating space for just one float though. That could be a possible source of crashes.
You never actually deallocate the memory either, too.
You need to use debugging and logging tools rather than eyeballing the code. We don't even know what your crash is.
However here is one suspicious place
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
In both branches of the if...else you are dividing by c[y][y]. You know when it enters the else that is zero. Unless the for loop changes it you will have divide by zero. So I suggest you test it.

Resources