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.
Related
I wrote a simple C program with a function that asks for two floating point numbers from the user passed by reference and the two numbers are printed to the screen using printf in main. My compiler is warning me about return at the end of the function "Expression result unused" and recommending to change it to this instead return (void)(*a),*b; What is the correct way to return this function?
#include <stdio.h>
float getFloats(float *a, float *b);
int main()
{
float num1,num2;
getFloats(&num1,&num2);
printf("%.2f, %.2f\n", num1, num2);
return 0;
}
float getFloats(float *a, float *b)
{
puts("Enter numbers:");
scanf("%f", a);
scanf("%f", b);
return *a,*b;
}
changed it to void and it works. Thank you
#include <stdio.h>
void getFloats(float *a, float *b);
int main()
{
float num1,num2;
getFloats(&num1,&num2);
printf("%.2f and %.2f\n", num1, num2);
return 0;
}
void getFloats(float *a, float *b)
{
puts("Enter numbers:");
scanf("%f", a);
scanf("%f", b);
}
In C you can't return more than one value. If you want to return more you need to wrap them into the struct.
struct TwoFloats
{
float a,b;
};
struct TwoFloats getTwoFloats(void)
{
struct TwoFloats tf;
if(scanf("%f %f", &tf.a, &tf.b) != 2)
{
/* error handling */
}
return tf;
}
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.
i having some problem with C. could u pls help me. im very new in C.
i got this error:
D:\yana\mini.o:mini.c|| undefined reference to `determine_price'|
D:\yana\mini.o:mini.c|| undefined reference to `calc'|
D:\yana\mini.o:mini.c|| undefined reference to `print_result'|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 7 seconds) ===|
#include<stdio.h>
#include<string.h>
#include <math.h>
void menu();
float determine_price(int);
float calc(float,int);
void print_result(float);
int main()
{
char selection;
int qty,quantity;
float price,pay,item_price,payment;
char set;
printf("**************************\n");
printf("|AZRI & YANA'S CAKE HOUSE|\n");
printf("**************************\n");
printf("\n");
printf("***************************\n");
printf("|1 CUP-------------RM3.50 |\n");
printf("|SET A(3 CUPS)-----RM10.00|\n");
printf("|SET B(6 CUPS)-----RM20.00|\n");
printf("|SET C(12 CUPS)----RM38.00|\n");
printf("***************************\n");
printf("\n");
printf("enter Set and quantity:\n");
scanf("%c %d",&selection,&qty);
price=determine_price(set);
pay= calc(price,qty);
print_result(pay);
return 0;
}
void menu()
{
printf("1 cup = RM3.50\n");
printf("Set A = RM10.00\n");
printf("Set B = RM20.00\n");
printf("Set C = RM38.00\n");
}
float determine_price(int item_code)
{
float set;
if(selection=='1')
printf("1 cup = RM3.50\n");
else if(selection=='A')
printf("Set A = RM10.00\n");
else if(selection=='B')
printf("Set B = RM20.00\n");
else if(selection=='C')
printf("Set C = RM38.00\n");
else
printf("set not available\n");
return(set);
}
float calc(float item_pricing,int quantity)
{
float answer;
answer = item_price*quantity;
return(answer);
}
void print_result(float payment)
{
printf("total cost = %4.2f",payment);
}
wht do i hv to do to call function? do i need to call function() or is there mistake in my syntax?
Edit:
after revised with help of you all, i make a little editing..
now, i got new error...
D:\yana\mini.c||In function 'determine_price':|
D:\yana\mini.c|54|error: 'selection' undeclared (first use in this function)|
D:\yana\mini.c|54|note: each undeclared identifier is reported only once for each function it appears in|
D:\yana\mini.c||In function 'calc':|
D:\yana\mini.c|70|error: 'item_price' undeclared (first use in this function)|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
Edit Number 2;
Edit Number 2;
i have found several mistake thank to all who answer my question.. here now, my new code... i try to make it clrscreen, however, it wont work... caN Someone help me?
#include<stdio.h>
#include<string.h>
#include <math.h>
#include <stdlib.h>
void menu();
int repeat();
float determine_price(char);
float calc(float,int);
void print_result(float);
int main()
{
char selection;
int qty,quantity,choice;
float price,pay,item_price,payment;
//char set;
while(choice!='2'){
menu();
printf("enter Set and quantity:\n");
scanf("%c %d",&selection,&qty);
price=determine_price(selection);
pay= calc(price,qty);
print_result(pay);
choice = repeat();
}
return 0;
}
void menu()
{
printf("**************************\n");
printf("|AZRI & YANA'S CAKE HOUSE|\n");
printf("**************************\n");
printf("\n");
printf("***************************\n");
printf("|1 CUP-------------RM3.50 |\n");
printf("|SET A(3 CUPS)-----RM10.00|\n");
printf("|SET B(6 CUPS)-----RM20.00|\n");
printf("|SET C(12 CUPS)----RM38.00|\n");
printf("***************************\n");
printf("\n");
}
void clr(){
system("cls");
}
int repeat(){
int choice;
printf("\n\n\n\n");
printf("Press\n[1] to return to menu\n[2] to end system\n");
scanf("%d",&choice);
if(choice=='1'){
clr();
}
return choice;
}
float determine_price(char selection){
float setprice;
if(selection=='1'){
printf("1 cup = RM3.50\n");
setprice=3.50;
}
else if(selection=='A'){
printf("Set A = RM10.00\n");
setprice=10.00;
}
else if(selection=='B'){
printf("Set B = RM20.00\n");
setprice=20.00;
}
else if(selection=='C'){
printf("Set C = RM38.00\n");
setprice=38.00;
}
else
printf("set not available\n");
return(setprice);
}
float calc(float item_pricing,int quantity){
float answer;
answer = item_pricing*quantity;
return(answer);
}
void print_result(float payment){
printf("total cost = RM%4.2f",payment);
}
You code is correct, but you miss an } to close your main function.
You have to declare the specification of your functions out of main function.
es:
/* Forward declarations */
void a();
float b();
/* Main */
int main() {
...
}
/* Function's specifications */
void a() {
...
}
float b() {
...
}
I'm a beginner and I am having a problem with the code I'm writing ..
I'm receiving the following error: "Unexpected unqualified-id before '{' token in line 9"
Also, I don't know how to set the output and make it shown so if you guys could really help me with that I would be grateful ..
And just to let you know .. I'm using the "Code Blocks"
#include<stdio.h>
#include<conio.h>
int read_temps(float temps[]);
int hot_days(int numOfTemp, float temps[]);
int printf_temps(int numOfTemp,float temps[],int numOfHotDays);
int main(void);
{
int index=0;
float tempVal;
float temps[31];
int numOfTemp,numOfHotDays;
do
{
printf("\n Enter the noon temperature (500 as a sential value)");
scanf("%f",&tempVal);
if(tempVal!=500.0)
{
temps[index]=tempVal;
index++;
}
}while(tempVal!=500.0);
return index;
{
int i;
int count=0;
for(i=0;i<numOfTemp;i++)
{
if(temps[i]>32.0)
count++;
}
return count;
}
{
float sum=0.0;
int i;
printf("\nTemperatures of the month");
printf("\n-------------------------");
for(i=0;i<numOfTemp;i++)
{
printf("\nDay %d : %.2fF",i+1,temps[i]);
sum=sum+temps[i];
}
printf("\nNumber of Hot Days : %d",numOfHotDays);
printf("\nAverage Temperature for a month : %.2f",sum/numOfTemp);
}
{
clrscr();
numOfTemp=read_temps(temps);
numOfHotDays=hot_days(numOfTemp,temps);
clrscr();
printf_temps(numOfTemp,temps,numOfHotDays);
getch();
}
}
int main(void);
{
Remove the semicolon. You seem to misunderstand the format of function declaration and definition.
Function definition:
void foo(void)
{
//something
}
Function declaration:
void foo(void);
The code:
int main(void);
{
is actually a prototype for main followed by an opening brace. Since the prototype is a distinct semantic element, the brace is not a legal token at that point.
You need to remove the trailing semicolon ;:
int main(void)
{
You also seem to have unreachable code in your main function following:
return index;
That return statement is executed unconditionally following the do...while loop so the code following it can never be executed.
That will become a lot clearer once you tidy up your formatting style (eg, use a four-space indent consistently).
In other words, something like this, where the unreachable code and unnecessary braces become obvious (the unnecessary braces are most likely the result of you forgetting to put in function definitions for the three sub-functions that you have prototypes for (a)):
#include <stdio.h>
#include <conio.h>
int read_temps (float temps[]);
int hot_days (int numOfTemp, float temps[]);
int printf_temps (int numOfTemp, float temps[], int numOfHotDays);
int main (void) {
int index = 0;
float tempVal;
float temps[31];
int numOfTemp, numOfHotDays;
do {
printf ("\n Enter the noon temperature (500 as a sentinel value)");
scanf ("%f", &tempVal);
if (tempVal!=500.0) {
temps[index] = tempVal;
index++;
}
} while (tempVal != 500.0);
return index;
{
int i;
int count = 0;
for (i = 0; i < numOfTemp; i++) {
if (temps[i] > 32.0)
count++;
}
return count;
}
{
float sum = 0.0;
int i;
printf ("\nTemperatures of the month");
printf ("\n-------------------------");
for (i = 0;i < numOfTemp; i++) {
printf ("\nDay %d : %.2fF", i+1, temps[i]);
sum = sum + temps[i];
}
printf ("\nNumber of Hot Days : %d", numOfHotDays);
printf ("\nAverage Temperature for a month : %.2f", sum/numOfTemp);
}
{
clrscr ();
numOfTemp = read_temps (temps);
numOfHotDays = hot_days (numOfTemp, temps);
clrscr ();
printf_temps (numOfTemp, temps, numOfHotDays);
getch ();
}
}
And one final note, though it's unrelated to your immediate problem. You should strive as much as possible to write portable code, which would entail avoiding the non-standard conio header file and the use of clrscr and getch (especially when getchar is available).
(a) If that is the case, you'll need to add the definition lines before each function and move them to outside of the main function.
It's just semicolon after your main. Try this one.
#include<stdio.h>
#include<conio.h>
int read_temps(float temps[]);
int hot_days(int numOfTemp, float temps[]);
int printf_temps(int numOfTemp,float temps[],int numOfHotDays);
int main(void)
{
int index=0;
float tempVal;
float temps[31];
int numOfTemp,numOfHotDays;
do
{
printf("\n Enter the noon temperature (500 as a sential value)");
scanf("%f",&tempVal);
if(tempVal!=500.0)
{
temps[index]=tempVal;
index++;
}
}while(tempVal!=500.0);
return index;
{
int i;
int count=0;
for(i=0;i<numOfTemp;i++)
{
if(temps[i]>32.0)
count++;
}
return count;
}
{
float sum=0.0;
int i;
printf("\nTemperatures of the month");
printf("\n-------------------------");
for(i=0;i<numOfTemp;i++)
{
printf("\nDay %d : %.2fF",i+1,temps[i]);
sum=sum+temps[i];
}
printf("\nNumber of Hot Days : %d",numOfHotDays);
printf("\nAverage Temperature for a month : %.2f",sum/numOfTemp);
}
{
clrscr();
numOfTemp=read_temps(temps);
numOfHotDays=hot_days(numOfTemp,temps);
clrscr();
printf_temps(numOfTemp,temps,numOfHotDays);
getch();
}
}
You have not provided your used methods and used return before end of main.
That generate dead code. so anyhow this won't give expected output.
Correct that first. or update your question
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);
}
}