My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
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 <iostream>

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
Mark is offline  
 
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!
milin is offline  
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
Mark is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
beginner



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's dilemma shunya Computer Science 3 November 27th, 2013 10:54 AM
I'm a beginner, need urgent help with 3 problems J8m Algebra 0 February 24th, 2013 06:21 AM
beginner's book Peter1107 Calculus 6 August 2nd, 2011 10:23 AM
Help plz with VERY beginner limits. NathanielZhu Calculus 7 August 1st, 2010 06:17 PM
Calculus-1 beginner prathimarao Algebra 7 June 10th, 2009 09:26 PM





Copyright © 2017 My Math Forum. All rights reserved.