0

I have condition to give me Hz of the octave but I got an error 51:16: runtime error: signed integer overflow: 1845493760 * 2 cannot be represented in type 'int' for line

for (int j = 0; 4 + j < octave; j++)
            hz *= 2;

it looks like my loop for do not stop but i think it is made correct. My code looks like this:

  int n = 0; // fraction(?) counter (n/12)
    int octave = note[strlen(note) - 1];
    int hz = 440; //Hz of A in the Octave
    double noteHz ;
    double temp ;


    //1st step: calculate the A hrtz of octave
    if (octave < 4)
    {
        for (int i = 0; 4 - i > octave; i++)
            hz /= 2;
    }

    else if(octave > 4)
    {
        for (int j = 0; 4 + j < octave; j++)
            hz *= 2;
    }

    else
        hz = 440;

2 Answers 2

1

The issue with this was that I have signed char to int and havent changed anything. I asummed that if char is 4 that the int will be also 4 but it is not (its 52) so that's why my loop was giving so big ints.

To fix char to int I added line:

octave -= '0'; // convering string number to int number
0

in this case we must ensure that the for loop stops at some point, here it should be enough to check that the variable octave is a valid limit, and that you do not go beyond the value that an integer can store, or simply that it is defined , it should be enough to do an octave printf and see what happens

1
  • i have just done it - it shows that hz = 0
    – koko loko
    Commented Jan 22, 2018 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .