0

So, I'm working on a fraction calculator to add, subtract, divide and multiply. Currently on the part where it's asking for the user to input their fractions. My professor states this on the assignment sheet: "A valid sign will be ‘+’ or '-', a valid whole part or numerator will be any non-negative integer value, and a valid denominator will be any positive integer value."

So, my question is this...how can I get it to detect whether a user has entered in a mixed number (ex: 3 4/7) or a normal fraction (4/7). I can get it to read a regular fraction fine, but where would I start to check the data they enter to see which it is?

int main(void)
{
    int num = 0;
    int den = 0;
    char dummy;
    cout << "Please enter in a fraction: " ;
    cin >> num >> dummy >> den;
    cout << num << "/" << den;
        
  system("pause");
  return 0;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

check the first field to see if it is +,-,or a number. if it's a sign or a number, take in the next number etc. until you reach / (so in your mixed number example you invalidate the input based on the space). read your numbers into a string but not the signs. do the same thing for the denominator after you've seen the / and then do any parsing necessary on the strings. Multiply your numbers by sign after they are parsed and present your answer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.