math not working

This is just an example of my questionnaire. I want to have each answer revised numerically which works as desired but when I go to use those same outputs in a simple math problem the answer always comes out as zero. I'm teaching myself C++ and I'm not sure the proper terminology for some if these things. Thanks in advance for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
void test();
using namespace std;
int main()
{
cout<<"Text.\n";
test();
return (0);
}
void test()
{
int a=0,b=0;char c;float d,e;
cout<<"Question\n";
cin>>c;
if(c=='y')a=a+1;
if(c=='n')b=b+1;
//I repeat this 42 times
d=(a/42)*100;e=(b/42)*100;
cout<<"\nYou answered yes "<<d<<"% of the time\n";
cout<<"You answered no "<<e<<"% of the time\n";
}
Last edited on
I think part of your problem is because you're using float value (d, e) to hold the results of integer operations (a, b). The compiler truncates the result before storing it. Try making a and b floats too, or using d = static_cast<float>(a*100/42) (not completely sure about that...)
Last edited on
Why not include the code for the loop as well?
Hey, thanks for the quick reply. Switching a and b to float did the trick. I assumed it was something simple and I'm glad it was. Thanks again.
Last edited on
Topic archived. No new replies allowed.