I'm afraid to research too much because I don't want spoilers, I just want pointers. I'm getting a lot of errors, in particular atoi errors. I tried saving an array element as a char, but atoi still won't work on it...? Thanks for any help.
// Helper functions for music
#include <cs50.h>
#include "helpers.h"
#include "math.h"
// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
// TODO
//prompt user for the fraction
fraction = get_string("Please enter a fraction in the format X/Y");
//convert divisor to an integer
// int atoi(string str);
char f1 = fraction[0];
int divisor = atoi(char f1);
//convert divisor to an integer
int dividend = atoi(fraction[2]);
//calculate how many eights
if(dividend == 8)
{
//get the eights from divisor
duration = divisor;
}
else
{
//convert other fractions into eights with a common denominator
int common = 8 / dividend;
duration = divisor * common;
}
}
// Calculates frequency (in Hz) of a note
// The frequency, f, of some note is 2n/12 × 440, where n is the number of semitones from that note to A4
// (2 to the power of n/12) * 440
int frequency(string note)
{
// TODO
// take as input as a string a note (e.g., A4)
note = get_string("Please enter a letter and number to represent your note. A flat or sharp may be placed after the letter.");
//define the letter character
char letter= note[0];
//validate a letter from A to G, ascii 65-71
for (int ascii=65; ascii < 72; ascii++)
{
if letter == ascii)
{
ascii++;
}
else
{
printf("Please enter a capital letter between A and G.");
}
}
//determine whether the number of the octave is the 2nd or 3rd character
//check if the 2nd character is a number
// int isdigit(int c);
if (isdigit(note[1]))
{
int octave = atoi(note[1]);
}
else
{
octave = atoi(note[2]);
}
// validate the octave is a number from 0 - 8
for (int octaves = 0; octaves < 9; octaves++)
{
if (octave == octaves)
{
return 0;
}
else
{
printf("Please enter a note with a number between 0 and 8.")
}
}
// The frequency, f, of some note is 2n/12 × 440, where n is the number of semitones from that note to A4
// (2 to the power of n/12) * 440
// frequency = 2 ** (n / 12) * 440
//start with the A's
if (letter == 'A')
{
//check for octaves below 4 - for A0 to A3
if (octave >= 0 && octave <= 3)
{
//substract octave from 4 and store in tones
int tones = 4 - octave;
}
// check for octaves above 4 - for A5 to A8
else if (octave >= 5 && octave <= 8)
{
tones = 4 + octave;
} else
{
tones = 0;
}
}
//test A
printf("%i\n", tones);
//define semitones
int n = tones * 2;
printf("%i\n", n);
// frequency = 2 ** (n / 12) * 440
frequency = round((2**(n / 12)) * 440);
}
printf("%i\n", frequency);
// Determines whether a string represents a rest
bool is_rest(string s)
{
// TODO
s = get_string("Please enter a note.");
if (s == "")
{
frequency = 0;
}
}