Here is what I'm trying to do:

  • Take input with scanf()
  • Pass input to function
  • Beep frequency associated with input

I have notes defined, e.g
const in C5 = 523.25;

I want the user to be able to input "C5", and have the computer beep at a frequency of 523.25.
When I run my code, I get the same sound no matter what I input, even if it is just random letters.

I am fairly new to C, but I think what is happening is that the function is trying to beep at the frequency of the string the user inputs, rather than the in that the note is associated with.

How can I solve this problem?
Thanks.

Please post your code.

#include <dos.h>
#include <stdio.h>

// 2
const int C2    =65.41;
const int Cs2   =69.30;
const int D2    =73.42;
const int Ds2   =77.78;
const int E2    =82.41;
const int F2    =87.31;
const int Fs2   =92.50;
const int G2    =98.00;
const int Gs2   =103.83;
const int A2    =110.00;
const int As2   =116.54;
const int B2    =123.47;
// 3
const int C3    =130.81;
const int Cs3   =138.59;
const int D3    =146.83;
const int Ds3   =155.56;
const int E3    =164.81;
const int F3    =174.61;
const int Fs3   =185.00;
const int G3    =196.00;
const int Gs3   =207.65;
const int A3    =220.00;
const int As3   =233.08;    
const int B3    =246.94;    
// 4
const int C4    =261.63;
const int Cs4   =277.18;
const int D4    =293.66;
const int Ds4   =311.13;
const int E4    =329.63;
const int F4    =349.23;
const int Fs4   =369.99;
const int G4    =392.00;
const int Gs4   =415.30;
const int A4    =440.00;
const int As4   =466.16;
const int B4    =493.88;
// 5
const int C5    =523.25;
const int Cs5   =554.37;
const int D5    =587.33;
const int Ds5   =622.25;
const int E5    =659.26;
const int F5    =698.46;
const int Fs5   =739.99;
const int G5    =783.99;
const int Gs5   =830.61;
const int A5    =880.00;
const int As5   =932.33;
const int B5    =987.77;
// 6
const int C6    =1046.50;
const int Cs6   =1108.73;
const int D6    =1174.66;
const int Ds6   =1244.51;
const int E6    =1318.51;
const int F6    =1396.91;
const int Fs6   =1479.98;
const int G6    =1567.98;
const int Gs6   =1661.22;
const int A6    =1760.00;
const int As6   =1864.66;
const int B6    =1975.53;
// 7
const int C7    =2093.00;
const int Cs7   =2217.46;
const int D7    =2349.32;
const int Ds7   =2489.02;
const int E7    =2637.02;
const int F7    =2793.83;
const int Fs7   =2959.96;
const int G7    =3135.96;
const int Gs7   =3322.44;
const int A7    =3520.00;
const int As7   =3729.31;
const int B7    =3951.07;
// 8
const int C8    =4186.01;
const int Cs8   =4434.92;
const int D8    =4698.64;
const int Ds8   =4978.03;

int main()
{
    char notein[10];

    while (1==1)
    {
        printf("\nenter note: ");
        scanf("%s", notein);

        sound(notein);
        delay(50);
        nosound();
    }
    return 0;
}

Here you go.
I'm doing this on a MS-DOS machine by the way, hence the sound() function etc.

I'm guessing you're typing something like "C8" and expecting sound to magically determine that the string corresponds to 4186, which is unreasonable. You need to match the string with the frequency. Here's one option:

struct note { const char *name; int frequency; };
struct note notes[] = {
    {"C2",  65},
    {"Cs2", 69},
    {"D2",  73},
    {"Ds2", 77},
    {"E2",  82},
    /* ... */
    {NULL,   0}
};

/* ... */

for (i = 0; notes[i].name != NULL; i++) {
    if (strcmp(notes[i].name, notein) == 0) {
        sound(notes[i].frequency);
        delay(50);
        nosound();
    }
}

Also keep in mind that sound takes an integer argument, so you can't pass a string and have it do anything meaningful. In fact, I'm surprised it compiles as you have it.

I'll give this a go when I have time.

As I said I'm pretty new to C, so not entirely sure what to expect/where I'm going wrong most of the time.

Cheers

const int C2 =65.41;

int does not have decimal places. If you want decimals then use float or double instead of int

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.