Im using segger embbeded studio and I keep getting a compilation error on count
ID: 3871560 • Letter: I
Question
Im using segger embbeded studio and I keep getting a compilation error on count = numTimesAppears(mystring, ch); Please Help.
#include
#include
#include
#include
extern float toKilometers(float) ;
extern float computeTax(float);
extern int numTimesAppears(char *, char) ;
extern int toInches(int );
void main(void) {
int i;
char mystring[100]="Yusuf Ozturk";
char ch;
float miles, km;
int inches, feet;
float salesamount, tax;
int count;
miles = 21.5;
km = toKilometers (miles);
printf(" %G miles is equal to %G kilometers", miles, km);
salesamount = 123;
tax = computeTax(salesamount);
printf(" the sales tax for the %G amount is %G",salesamount, tax);
feet = 25;
inches = toInches(feet);
printf(" %d feet is equal to %d inches", feet, inches);
ch = 'u';
count = numTimesAppears(mystring, ch); // The compiler keeps telling me "undefined reference to `numTimesAppears"
printf(" Number of times %c appears in string is %d", ch, count);
}
Explanation / Answer
#include <stdio.h>
extern float toKilometers(float) ;
extern float computeTax(float);
extern int numTimesAppears(char *, char) ;
extern int toInches(int );
void main(void) {
int i;
char mystring[100]="Yusuf Ozturk";
char ch;
float miles, km;
int inches, feet;
float salesamount, tax;
int count;
miles = 21.5;
km = toKilometers (miles);
printf(" %G miles is equal to %G kilometers", miles, km);
salesamount = 123;
tax = computeTax(salesamount);
printf(" the sales tax for the %G amount is %G",salesamount, tax);
feet = 25;
inches = toInches(feet);
printf(" %d feet is equal to %d inches", feet, inches);
ch = 'u';
count = numTimesAppears(mystring, ch); // The compiler keeps telling me "undefined reference to `numTimesAppears"
printf(" Number of times %c appears in string is %d ", ch, count);
}
float toKilometers(float miles) {
return miles * 1.60934;
}
float computeTax(float salesamount) {
float taxPercentage = 5;
return (salesamount * taxPercentage)/100;
}
int numTimesAppears(char *mystring, char ch) {
int count = 0,i;
for(i=0;mystring[i]!='';i++){ //looping each character until find null character in given string ''
if(mystring[i] == ch){ //checking whether each character in string is matching with given character
count++; //if matched then increasing the count
}
}
return count; //returning number of characters match
}
int toInches(int feet) {
return feet * 12;
}
Output:
$ gcc -o main *.c
sh-4.4$ main
21.5 miles is equal to 34.6008 kilometers
the sales tax for the 123 amount is 6.15
25 feet is equal to 300 inches
Number of times u appears in string is 3