How to make my overloaded fraction >> operator recognize negative numbers.

Okay, so I've been trying to get this to work for the past 2 days now, but to no avail. essentially there is a text file, from which the program extracts fractions and mixed numbers, and my >> operator is supposed to convert them to normal fractions to be outputted by my << operator, which works fine. but as soon as my function hits -8 in the file, the program crashes and ends, leaving me with the "press any key to continue" end prompt on the console. it handles -4/5 and -10/5 no problem, returning -4/5 and -2 respectively. but when the negative number is whole, it crashes.

istream& operator>>(istream& input, fraction& fr)
  {
    int Num=0;
    int Den=0;
    int Whole=0;
    input >> Num;
    if (input.peek() == '+')
    {
        Whole = Num;
        input.ignore() >> Num;
        input >> Den;
    }
    else if (input.peek() == '/')
    {
        input.ignore() >> Den;
    }
    else if (input.peek() == ' ')
    {
       input.ignore();
       Den = 1;
    }
    Num = (Whole*Den) + Num;
    fr = fraction(Num, Den);
    return input;
 }

Answers


Use a default of Den = 1 instead of 0. It's crashing due to divide by 0.


Need Your Help

Respect labels spacing

uitableview swift2 uilabel uistoryboard

Working with a UITableViewController. You can see my prototype cell in the screenshot; It's made up by 2 labels (1 on the left containing quantity X item name, and 1 on the right with the price).

MySql Composite Foreign Key Referncing same column

mysql sql foreign-keys foreign-key-relationship

I am attempting to create a composite foreign key in MySQL however both fields are referencing the same column in another table. I am not sure if this is the accurate approach since the sql is not