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>
usingnamespace 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.
(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:
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().