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() {
...
}
Related
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.
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
Here is my code and problem. The code compiles fine. But when I run it. After I enter the menu option in getMenuOption() "Segmentation Fault (core dumped)" pops up. What is wrong?
I'm new to programming in general. Thanks for the help if its provided.
#include <stdio.h>
#include<math.h>
#define CALCULATE_PI 'a'
#define CALCULATE_GEOMEAN 'b'
#define CALCULATE_HARMMEAN 'c'
void printInstructions (void);
void printMenuOptions (void);
int runMenuOption ();
int getMenuOption ();
int getLimit ();
int calculatePi ();
int calculateGeoMean ();
int calculateHarmonicMean ();
int main(void)
{
printInstructions();
printMenuOptions();
runMenuOption(getMenuOption());
return 0;
}
void printInstructions (void)
{
printf("======================================================\n");
printf("= PI, Geometric Mean, and Harmonic Mean Calculator =\n");
printf("= Please refer to the menu to choose calucaltion =\n");
printf("=Choose desired menu option and press enter to begin =\n");
printf("= Proceed to follow on-screen instructions =\n");
printf("======================================================\n\n\n");
return;
}
void printMenuOptions (void)
{
printf("3 choices: Please enter a VALID letter.\n");
printf("Choice 'a' = Calcualtes PI\n");
printf("Choice 'b' = Calculates Geometric Mean\n");
printf("Choice 'c' = Calculates Harmonic Mean\n\n");
return;
}
int runMenuOption (int getMenuOption())
{
char option;
double answer,
Pi = 0.0,
geoMean = 0.0;
option = getMenuOption();
switch (option)
{
case CALCULATE_PI:
calculatePi(getLimit());
answer = Pi;
break;
case CALCULATE_GEOMEAN:
calculateGeoMean(getLimit());
answer = geoMean;
case CALCULATE_HARMMEAN:
printf("Harmonic Mean");
break;
default:
printf("Incorrect Character!\n");
printf("Try again");
break;
}
printf("Your answer is %5p", &answer);
return 0;
}
int getMenuOption (void)
{
char option;
printf("Please enter choice: ");
scanf("%c", &option);
return option;
}
int getLimit ()
{
int limit;
scanf("%d", &limit);
return limit;
}
int calculatePi (void)
{
int limit,
count = 0,
Pi = 0;
printf("Please enter the PI limit: ");
limit = getLimit();
for (count = 1; count <= limit; count++)
{
Pi += 1 / count;
}
return sqrt(Pi * 6);
}
int calculateGeoMean()
{
int limit,
userValue = 0,
count = 0;
double geoMean = 0;
limit = getLimit();
while(count <= limit)
{
if (userValue <= 0)
printf("Incorrect. Try again");
else
{
count++;
userValue *= userValue;
}
}
geoMean = userValue;
return sqrt(userValue);
}
int calculateHarmonicMean()
{
int limit,
userValue = 0,
count = 0;
double harmMean = 0;
limit = getLimit();
while(count <= limit)
{
if (userValue <= 0)
printf("Incorrect. Try again");
else
{
count++;
userValue *= 1 / userValue;
}
}
harmMean = userValue;
return limit / userValue;
}
This function definition is totally wrong.
int runMenuOption (int getMenuOption())
either you can pass the return value of getMenuOption like this
int runMenuOption (int option)
or
you shouldn't pass any value to this function and call getMenuOption inside runMenuOption. You are doing both, which is incorrect.
int runMenuOption (int getMenuOption())
Here's your problem.
That should be:
int runMenuOption (int opt)
Also, you shouldn't be calling getMenuOption() within runMenuOption since you're calling getMenuOption() as you pass it to runMenuOption as a parameter. runMenuOption should only have a switch statement.
You need to modify the definition of your function from int runMenuOption (int getMenuOption()) to int runMenuOption (int option). In the call, getMenuOption() will be invoked and the output placed into the stack frame of the called function.
According to your declaration of the runMenuOption function, it takes a pointer to a function which returns an integer as its first argument:
int runMenuOption (int getMenuOption())
The function is then called in this line:
option = getMenuOption();
This is perfectly fine. However, the problem lies in this line:
runMenuOption(getMenuOption());
Here you are calling the getMenuOption function and passing the return value into the runMenuOption function. But what you should be doing is passing the function itself as the argument:
runMenuOption(getMenuOption);
The reason you are getting a Segmentation Fault error is because the return value from the getMenuOption function is being treated as a function pointer, and your program is attempting to call a function at that address, which is of course invalid.
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);
}
}
I am using Code::blocks to compile my first multiple source file, learned from "C Programming in easy steps" by Mike McGrath. Unfortunately my math functions seem to be having issues. Here's the header that contains the functions:
/* this header file contains utility functions */
int square(int x); /* function prototypes */
int multiply(int x, int y);
int square(int x)
{
return (x*x);
}
int multiply(int x, int y)
{
return (x*y);
}
The only function having the problem is "square()". It reads the input of "2" as "2293356" and outputs the square as "553755367"... What the heck?!?
Here's the menu.c file... There's menu.c, ops.c, calc.c, and utils.h. Abaov is the .h.
MENU.c
include
void menu();
void menu()
{
int num;
printf("\n\tEnter the number of an operation:\n");
printf("\t1. Square a number\n");
printf("\t2. Multiply two numbers\n");
printf("\t3. Exit\n");
scanf("%d", &num);
switch(num)
{
case 1 : getnum(); break;
case 2 : getnums(); break;
case 3 : return;
}
}
Here's ops.c...
#include <stdio.h>
#include "utils.h"
void getnum();
void getnums();
void getnum()
{
int num;
printf("Enter an integer to be squared: ");
scanf("%d", &num);
printf("%d squared is %d\n, num, square(num)");
menu();
}
void getnums()
{
int num1, num2;
printf("Enter two numbers to be multiplied, ");
printf("seperated by a space: ");
scanf("%d", &num1);
scanf("%d", &num2);
printf("%dx%d = %d\n", num1, num2, multiply(num1, num2));
menu();
}
This is the last part of the program, calc.c,
#include <stdio.h>
int main()
{
menu();
printf("end\n");
return 0;
}
The square of 2293356 doesn't fit into int and therefore overflows which leads to undefined behavior! As to why it reads 2 as 2293356 cannot be answered without more code.
Update:
And here's your real error:
printf("%d squared is %d\n, num, square(num)");
should be
printf("%d squared is %d\n", num, square(num));
:)