Input problem

I am trying to create a program that prompts the user to input a fraction such as 5/3 and then simplify the fraction so 5/3 would be 1 2/3. The issue is that the input is displayed as a fraction. Numerator/denominator. When I prompt for the denominator I get 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
  cout << "Please enter a fraction (numerator / denominator)" << endl;
  int numerator;
  int denominator;
  cin >> numerator;
  cin >> denominator;
  cout << denominator << endl;





  return 0;
}


Please enter a fraction (numerator / denominator)
5 / 6
0


Any ideas??? Obviously the program isn't complete but I want to fix this problem first.
Last edited on
(Warning: I'm a novice, so not all of my advice may be correct.)
That character in the middle is killing you, I'd be willing to bet. Try doing something like this:
1
2
3
4
5
int numerator;
char slash;
int denominator;
cin >> numerator >> slash >> denominator;
cout << numerator << " / " << denominator << endl;

...and (if you put in 5/6) it *should* display...
5/6
5 / 6

Note that you don't have to have spaces in between the number and the character (in this case the slash). Also, I wouldn't recommend relying on "cin >> blah blah blah;" too often. You should look at other methods of input, like getline(...);. It's less buggy, but "cin >> ..." is fine for training purposes.
If this is the actual input 5 / 6 then you need to deal with the '/' character, either by actively reading it into a char variable, or by ignoring it with cin.ignore().

1
2
3
    cin >> a;
    cin.ignore(100,'/');
    cin >> b;
Last edited on
Hey man I'm in your class; working on the project right now.
Are you on campus?

The thing is we also need to account for negative numbers so we can't just use the int.
we have to use float.

so something like

float num; deno;
int whole;

or something

also we're learning if statements.
so there's like a shitload of if statements we have to use.

Edit:
Disregard everything I have said since it is wrong.

Like if the problem is solveable then the program needs to solve it
i.e. 8/2 = 4

But if it is a improper fraction then we need to display it with the remainder
i.e 8/3 = 2 2/3

And if it's negative, if it's 0, if it's undetermined,
we need to account for it...
Last edited on
Topic archived. No new replies allowed.