# How do I simulate a program with a \frac{}{} command

This is a discussion on How do I simulate a program with a \frac{}{} command within the C++ Programming forums, part of the General Programming Boards category; Code: I'm doing an excerise to make factions, but I need to simulate a \frac{}{} command. To display numbers in ...

1. ## How do I simulate a program with a \frac{}{} command

Code:
I'm doing an excerise  to make factions, but I need to simulate a \frac{}{}
command. To display numbers in faction and decimal form. How do I simulate a
\frac{}{} command? Thanks.

2. Code:
What is a \frac{} command?

3. It's latex-speak.

4. You don't, at a console. If you're trying for "nice" output like on paper, just write a LaTeX file. If you need to fake it on a screen, print out "2 3/8" or something.

5. Originally Posted by robwhit
Code:
What is a \frac{} command?
I need to simulate a \frac{}{} command.

6. Originally Posted by tabstop
You don't, at a console. If you're trying for "nice" output like on paper, just write a LaTeX file. If you need to fake it on a screen, print out "2 3/8" or something.
I don't think you get what I mean like:

Code:

\frac{1}{4} is -     1/4

or

0.25

7. then do 1/4 in your program.
Code:
#include <iostream>

int main(){
float frac = 1/4;
std::cout<< frac;
return 0;
}

8. Originally Posted by xbusterx
I don't think you get what I mean like:

Code:

\frac{1}{4} is -     1/4

or

0.25
So just print it out. I don't know what kind of spacing you're going for over there on the right, so you'll have to fiddle, but:
Code:
cout << "\\frac{1}{4} is - \t 1/4" << endl << "\t\t\t or" << endl << "\t\t\t 0.25" << endl;

9. Originally Posted by tabstop
So just print it out. I don't know what kind of spacing you're going for over there on the right, so you'll have to fiddle, but:
Code:
cout << "\\frac{1}{4} is - \t 1/4" << endl << "\t\t\t or" << endl << "\t\t\t 0.25" << endl;

lol That wouldn't work though I'm trying to prompt the user not print it obivously I know how to print it all your doing is making stings that's not what I'm trying to do.

10. Originally Posted by xbusterx
lol That wouldn't work though I'm trying to prompt the user not print it obivously I know how to print it all your doing is making stings that's not what I'm trying to do.
And where did "prompting the user" come from? If that's what you wanted to do, why didn't you say so the first (or second, or third) time?

What are you stuck on about trying to get input?

11. then explain what you are trying to do?

do you mean you want to parse it?

Code:
int denominator, numerator;
scanf("\\frac{&#37;d}{%d}", &numerator, &denominator);
cout << numerator << " / " << denominator;

12. Originally Posted by cyberfish
then explain what you are trying to do?

do you mean you want to parse it?

Code:
int denominator, numerator;
scanf("\\frac{%d}{%d}", &numerator, &denominator);
cout << numerator << " / " << denominator;
ok did that but how would I make into decimal form?

13. the output?

the scanf line reads the values into denominator and numerator.

14. Originally Posted by xbusterx
ok did that but how would I make into decimal form?
Code:
cout << double(numerator)/double(denominator);
You have to convert it to double to get it to be treated as a decimal.

15. Here is what the excerise says

Code:
 One such command displays fractions. For example 5/4 is entered
"\frac{5}{4}". Write a computer program that simulates a \frac{}{}
command. The program reads two integers n1 and n2

EXAMPLE 1:
Enter fraction in the form n1/n2: 5/4
.....                  5
\frac{5}{4} is         --
......                  4

....               1
or 1  --
....               4

or 1.25

Page 1 of 2 12 Last