help with casting - changing type int to double

closed account (G20RfSEw)
The title states it all. I cant see what i am doing incorrect here. I am trying to learn how to use this correctly but im getting an output of 1.14. Can someone help me with this. thank you.


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int ppl, candy;
double candy_per_person;

ppl = '2';
candy = '9';


candy_per_person = static_cast<double>(candy)/ppl;

cout << "each person will get " << candy_per_person << " candies\n";\

cin >> ppl;

return 0;

}
First, please you code tags for your code - look at the format bar when you edit your question.

Your problem is that '2' != 2:
1
2
3
4
int num = 2;
cout << num << '\n';
int ch = '2';
cout << ch << '\n';
2
62


Try removing the quotes from your numeric literals.
Last edited on
Topic archived. No new replies allowed.