+ 2

Just started with c++ today. Can you please help me.

#include <iostream> using namespace std; int main() { int a; int b; int c; cout << "How much did you get for your previous test or exam?\n"; cin >> a; cout << "What was it out of?\n"; cin >> b; c = (a / b) * 100; cout << "Your percentage was:" << c << "℅\n"; }

26th Feb 2017, 6:44 PM
Enrique
6 Réponses
+ 9
#include <iostream> using namespace std; int main() { int a = 6; int b = 2; int c; cout << "How much did you get for your previous test or exam?\n"; // cin >> a; cout << "What was it out of?\n"; // cin >> b; c = (a / b) * 100; cout << "Your percentage was: " << c << "% \n"; }
26th Feb 2017, 7:17 PM
Anne Livia
Anne Livia - avatar
+ 4
I've executed your code here, and it's everything ok. maybe the problem is with this '%' sign
26th Feb 2017, 7:13 PM
Anne Livia
Anne Livia - avatar
+ 3
What's wrong ? The output ? if it's that, you may just add a space between the percent sign and the \n, like this: cout << "your percentage was: " << c << "% \n"; if you want a float number in the result, you can put the variable c with the data type float instead
26th Feb 2017, 7:04 PM
Anne Livia
Anne Livia - avatar
+ 3
but there aren't anything wrong
26th Feb 2017, 7:10 PM
Anne Livia
Anne Livia - avatar
+ 1
I'm guessing your problem is that for any result other than 100%, your output is 0%. The simplest solution would be to change c's type to float and change the assignment to c = a * 100.0 / b otherwise you still have an integer division and c would be 0.0
26th Feb 2017, 11:56 PM
Huegel
Huegel - avatar
0
it's not calculating percentage
26th Feb 2017, 7:11 PM
Enrique