-1

I'm working on an assignment that requires me to use "const unsigned char &fret" as input for a method. I have

void fretThing(const unsigned char &fret)
{
    char div = fret / 12;
    printf("%d\n", div);
}

but when I run the program, div = 0. I believe this is because char converts the number into an int, but when i try to cast to a double, it still does not work.

Is there any way to convert char to double?

when fret = 12, div =1. but when fret is not a multiple of 12, it returns 0.

Pinoyboy
  • 1
  • 2
  • 4
    What is `added`? And what did you cast to double? Also you convert the result to `char` (an integral type) after the calculation and print with `%d` anyway, I don't see where a `double` would be useful here. – Lukas-T Jun 18 '20 at 08:36
  • 3
    C and C++ are two different languages. C has no references – 463035818_is_not_an_ai Jun 18 '20 at 08:36
  • 4
    please provide a [mcve] including input, output and expected output. The code you posted cannot run, because it does not compile – 463035818_is_not_an_ai Jun 18 '20 at 08:38
  • You have to provide more details to get some answers. What is the type and value of your `added'` variable ? What is the relevanse of "const unsigned char &fret" to your question ? What did you expect `added/12'` to produce ? – nos Jun 18 '20 at 08:38
  • So your input parameter is `fret`, and then you start using `added` which seems to come from nowhere (if it's not initialised, most probably it will be set to zero)? – Dominique Jun 18 '20 at 08:39
  • Does this answer your question? [Why does dividing two int not yield the right value when assigned to double?](https://stackoverflow.com/questions/7571326/why-does-dividing-two-int-not-yield-the-right-value-when-assigned-to-double) – Marek R Jun 18 '20 at 08:40
  • sorry, im not sure how to respond to your comments, i was messing with it and "added" was just leftover from that, i changed the code to reflect my current situation. I need a double because i'm trying to convert a number into Hz, which sometimes requires decimal places. – Pinoyboy Jun 18 '20 at 08:43

1 Answers1

0
char div = added / 12;

depending on the type of added this probably is an integer divison, since 12 is an literal of type int. You should use a double-literal 12.0 here. But the assignement to char div would truncate the result anyway, so change this to a double. Lastly you want to print this with the correct format specifier %f. So you get something like this:

void fretThing(const unsigned char &fret)
{
    double div = fret / 12.0;
    printf("%f\n", div);
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • @Pinoyboy *'was just %f'* – well, no. If you just use %f together with char, you have undefined behaviour. Format specifier and argument always need to match. Here C++ facilities are much safer, as you don't need to explicitly specify a format, but instead compiler solves the problem for you by overload resolution. – Aconcagua Jun 18 '20 at 09:20
  • @Aconcagua Every modern C compiler warns you if you try to print a char with `%f` – th33lf Jun 18 '20 at 09:39
  • @th33lf Sure they do – but that doesn't change much. It's still better if you can rely on the compiler rather than on the coder. And can you imagine how many people ignore warnings and just care for errors? So many questions here on SO giving evidence... – Aconcagua Jun 19 '20 at 06:08
  • @Aconcagua It is the compiler that is giving the warning. The coder ignoring it is not the compiler's problem. I don't know of any professional programmer who ignores warnings. In fact, we escalate warnings to errors in most of our projects with the -Wall flag. The iostream approach is unnecessarily unweildy to use, especially for simple things like printing something in hex format. Add to that the horrible `<<` operator and we have ten steps backward! There are many things C++ does better than C, console IO is not one of them imho. – th33lf Jun 19 '20 at 11:09
  • @th33lf Well, we should probably distinguish professionals from hobby coders. What you write is correct in professional environment, and well formatted output (e. g. tables) indeed is pretty much a mess with C++ streams. On the other hand: Do some research here on SO, you won't believe how often people use bad format specifiers, how often you'll read the recommendation to turn all warnings on and listen to them... Side note: Those PRI and SCN macros for fixed width integers aren't that nice either... – Aconcagua Jun 19 '20 at 17:41
  • @Aconcagua Agree on the PRI macros, but they are rarely used. You can mostly get by with the 'classic' `%u/d/l` format specifiers. But I think I now agree with your point. Considering the OP is anyway using C++ (though he seems to think it is C), and he is just printing something without any complicated formatting, it is reasonable to suggest using streams rather than fiddle around with printf format specifiers which may be confusing for beginners! – th33lf Jun 22 '20 at 12:01