My Math Forum C++ help (beginner)

 Computer Science Computer Science Forum

 October 30th, 2007, 02:00 PM #1 Newbie   Joined: Oct 2007 Posts: 24 Thanks: 0 C++ help (beginner) In the past half hour, I've started learning C++. As a project, I decided to try and write a program that will turn a fraction into a percentage. Here is my code: Code: #include using namespace std; int main() { int numerator, denominator, quotient; cout << "Percentage Calculator\n"; cin.get(); cout << "Enter numerator: "; cin >> numerator; cout << "\nEnter denominator: "; cin >> denominator; cin.get(); cout << numerator << "/" << denominator << " = "; quotient = numerator/denominator; cout << quotient * 100 << "%"; cin.get(); } And here is the .exe: Clicky For some reason it doesn't like the division, could anyone tell me what I'm doing wrong? Thanks
 October 30th, 2007, 02:07 PM #2 Senior Member   Joined: May 2007 Posts: 402 Thanks: 0 Of course it doesn't work since your quotient variable is set to integer, and therefore cannot have a float value! What you need to do is replace the following: (1) int numerator, denominator, quotient; ---with--- int numerator, denominator; float quotient; (2) quotient = numerator/denominator; ---with--- quotient = (float)numerator/denominator; Now it should work!
 October 30th, 2007, 03:32 PM #3 Newbie   Joined: Oct 2007 Posts: 24 Thanks: 0 Thanks very much, I really should've thought of that Thank you

 Tags beginner

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post shunya Computer Science 3 November 27th, 2013 10:54 AM J8m Algebra 0 February 24th, 2013 06:21 AM Peter1107 Calculus 6 August 2nd, 2011 10:23 AM NathanielZhu Calculus 7 August 1st, 2010 06:17 PM prathimarao Algebra 7 June 10th, 2009 09:26 PM

 Contact - Home - Forums - Cryptocurrency Forum - Top