0

I am writing the frequency function for pset 3, and I'm taking the input string note and storing the character that denotes the octave number in an integer called octave1. I've been getting some pretty funky outputs so used the debugger to investigate. I've also used eprintf to double check that my stored value of octave1 is correct.

int octave1;    
octave1 = note[1];
eprintf("The octave1 is: %c\n", octave1);

I've tested my function by doing the following:

~/workspace/pset3/music/ $ ./notes 5
helpers.c:179: The octave1 is: 5

And according to eprintf it seems that my stored octave1 is correct. but in my debugger it shows that octave1 has actually been stored as 52??!! (see screenshot below)

This explains the weird outputs I've been getting but I don't understand how 52 was generated.

Any help will be gratefully received!

Screenshot of debugger showing octave 1 stored as 52

2
  • 1
    Screenshot and example do not match. '5' has a value of 53, not 52. And if you use %i to show integers, you'll see it also in eprintf.
    – Blauelf
    Jul 24 '18 at 16:18
  • 1
    Oh, and for conversion, remember caesar and vigenere, there you might have subtracted 65 or 'A' in some cases.
    – Blauelf
    Jul 24 '18 at 16:20
2

That's because you're trying to store the character at the 1st index of a char array (string), node, into an int variable directly, without converting it into an int first. So the 5 in your example is being treated as the character '5' and not the integer 5, and like storing any other character into an int variable, its ascii value gets stored in octave1.

3
  • Ahh that makes sense! Thank you so much. I tried to convert it into an integer using atoi but kept getting errors. Do you have any ideas how I can convert it into an integer please?
    – Xia Liu
    Jul 24 '18 at 16:16
  • @Xia atoi can't be used to convert a single character to integer, you have to do that manually, for which, as @Blauelf pointed out, remember what you've done in previous problem sets and look at the ascii values of digit characters to figure out what you need to subtract this time.
    – kappa122
    Jul 24 '18 at 16:47
  • 1
    @Blauelf Thank you both so much! The problem set if finished and submitted and works as intended (from what I can tell ;)). Really grateful to you both!
    – Xia Liu
    Jul 25 '18 at 3:20

You must log in to answer this question.

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