REALLY new to C++ (and this site) & need help please!

closed account (3CGE8vqX)
Hi, so I'm taking computer programming in school and one of the assignments we have is really stumping me.
We have to put a fraction in decimal form and mixed number form (letting the user input the numerator and denominator) WITHOUT using floats or doubles. Besides those data types, we've only learned about ceil, floor, fabs, pow, etc. but I doubt my teacher wanted that but anything is welcome.
Btw, this is the second language I'm learning, the first one being QBasic.
Thank you! :)

What do you have so far? It would be nice if I could see where you were getting stuck.
closed account (3CGE8vqX)
#include <iostream.h>

int num1, deno;

cout << "Input the numerator" << endl;
cin >> num1;
cout << "Input the denominator" << endl;
cin >> deno

cout << "The fraction is " << num1 << "/" << deno << endl;
cout << "The mixed number is " << (After this I don't know how to format it like this eg. 7/2 = 3 1/2)

[this is roughly my code, i don't have my exact one bc I can only run C++ at school]

thanks

Never ever ever, and I mean ever, post code without code tags.

Read this article ;) http://www.cplusplus.com/articles/jEywvCM9/

Now for your actual problem...

Are you familiar with the modulus operator '%' ?

It will return the remainder of a division operation for you.

Plain integer division: 7/2 = 3
Modulus: 7/2 = 1 ( It just gives you the remainder, that's all)

So you can get the whole number with plain integer division, then the numerator for the fraction using modulus. If you wanted to simplify the fraction part of the mixed number, well that'd be extra work and I don't know if you need to do that or not. If you did simplify it though, modulus would continue to prove itself useful ;)
closed account (3CGE8vqX)
I'm not very familiar with modulus, how do you use it? I think it does need to be simplified. How do you use modulus for the 1/2 remainder?
closed account (EwCjE3v7)
More about the modulus operator (%).

http://www.cprogramming.com/tutorial/modulus.html


1
2
3
4
5
    int i = 0;
    cin >> i;
    if (i % 2 == 0) {
        cout << i << " is an even number." << endl;
    }
Last edited on
Topic archived. No new replies allowed.