# My music program: frequency returning zeros

// Calculates frequency (in Hz) of a note
int frequency(string note)
{   int n,r,a;
double oct, z;
float x = 0.0;
float y = 0.0;
char t;
t=0;
a = 0;r = 0;

n = strlen (note);
t = note[strlen(note) -1]; //Octave Number in char
a = atoi("t");//Octave number in integer
r = a - 52;//
oct = pow(2,r);

if (n == 2 || n ==3)
{
if (note[0] == 'A')
{
x = 0.0;
}
else if (note[0] == 'B')
{
x = 2.0;
}
else if (note[0] == 'C')
{
x = -9.0;
}
else if (note[0] == 'D')
{
x = -7.0;
}
else if (note[0] == 'E')
{
x = -5.0;
}
else if (note[0] == 'F')
{
x = -4.0;
}
else if (note[0] == 'G')
{
x = -2.0;
}
//else
// {
//   return 2;
//}
}

if (n== 3)
{   if (note[1] == 'b')
{
y = 1.0;
}
else if (note[1] == '#')
{
y = -1.0;
}

//else
// {
//  return 3;
//}
}

z = ((440*(pow(2,((x+y)/12.0))))*oct);

return round(z);

}

The output I get on executing ./notes is:

C4: 0
C#4: 0
D4: 0
D#4: 0
E4: 0
F4: 0
F#4: 0
G4: 0
G#4: 0
A4: 0
A#4: 0
B4: 0

a = atoi("t");

This always results in a=0. Because the t is in double quotes, it is a literal string, meaning that the call to atoi() takes the string composed of the letter "t" and checks to see if it's a number. If not, (t is not a number), it returns 0.

If you want to use the variable t, then remove the double quotes.

Also, a reminder. The use of single letter variable names for anything other than for loop counters is a really bad practice. Get out of that habit now and get used to using descriptive variables. Imagine if you had to update a 1000 line program in the future and had to find every use of the variable a in the code among every appearance of the letter a. It would be a nightmare.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Getting this error now:clang -helpers.c:43:17: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion] a = atoi(t);//Octave number in integer ^ & /usr/include/stdlib.h:147:30: note: passing argument to parameter '__nptr' here extern int atoi (const char *__nptr) ^ 1 error generated. make: *** [notes] Error 1
– gvr
Commented Mar 3, 2018 at 17:26
• atoi() takes a string with an end of string marker. t is a char. Commented Mar 4, 2018 at 2:28