Title says it, mostly. Here is the exercise from my e-book in particular:
Write a program that encourages the user to enter two fractions, and then displays their sum in fractional form. (You don’t need to reduce it to lowest terms.) The interaction with the user might look like this:
Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10
You can take advantage of the fact that the extraction operator (>>) can be chained to read in more than one quantity at once:
cin >> a >> dummychar >> b;
Now I understand how to do it all, except being able to cin the numerator and denominator as two separate values. I'm not entirely sure what the author meant by "dummychar". I've tried actually typing in dummychar, /, '/', and "/" to no avail.
TL;DR: How to cin 1/2 to assign "1" to "a" and "2" to "b". Thanks in advance.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
int a,b;
char c;
cout << "Enter first fraction: ";
cin >> a >> c >> b;
cout << "a: " << a << " b: " << b << endl;
return 0;
}
Wow, I was stuck on that for a good 15 - 30 mins before looking it up / asking here. How simple, gotta love logic. It just makes so much sense now, thanks a ton.
Edit: My way actually worked out fine originally. I was just dumb and had a variable named a for my "Press any key to exit", and had an integer called a for the first part of the first fraction.