-3

I'm writing a program that solves fraction problems. The program inputs 26 fraction problems from a file (ex: 1/4 + 1/2) and stores them into an array. It then displays a random question from the array and should solve the problem and display the answer. I used getline to get each individual question from the file. How do I add the fractions together? I know how to add fractions in C++ but I just don't understand how to add them when it's inputted from a file, not user input.

EDIT: Here's my question. How do I add two fractions in a string? EX: "1/2 + 1/4"

This is my code so far:

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

const int MAX = 50;

void loadProblems(string problems[], int &count);
int gcd(int a, int b);

int main()
{
    ifstream myFile;
    srand(static_cast<unsigned int>(time(0)));
    string problems[MAX];
    int count, mode, randomIndex;
    int n1, n2, n3, d1, d2, d3;
    char slash;

    cout << "Welcome to the Fraction Tutor V1 by Vince!\n\n" << endl;
    cout << "There are 5 problems per session. The computer will select a problem from a list of problems and you have up to three attempts per problem. You must provide answer in reduced form (i.e., enter 3/4 instead of 6/8)." << endl;
    cout << "Have fun and good luck.\n" << endl;

    cout << "Loading problems from the file ..." << endl;
    loadProblems(problems, count);

    cout << "There are " << count << " problems available\n" << endl;
    randomIndex = rand() % count;

    cout << "Available Modes" << endl;
    cout << "   1. Training mode" << endl;
    cout << "   2. Normal mode" << endl;
    cout << "Please select a mode: ";
    cin >> mode;
    cout << "\n";

    if (mode == 1)
    {
        cout << "Training mode is selected.\n" << endl;
        cout << "Computer is selecting a random problem ..." << endl;
        cout << problems[randomIndex] << endl;


    }
    if (mode == 2)
    {
        cout << "Normal mode is selected.\n" << endl;

    }

    return 0;
}

void loadProblems(string problems[], int &count)
{
    count = 0;
    string problem;
    ifstream myFile;
    myFile.open("P4Problems.txt");

    getline(myFile, problem);
    while (!myFile.eof())
    {
        problems[count] = problem;
        count++;
        getline(myFile, problem);
    }
}
  • Presumably you got them as a string from the user, yes? How is getting them as a string from a file fundamentally different in terms of evaluating them? – paxdiablo Dec 02 '16 at 02:28
  • 1
    You're asking how to turn "1/4 + 1/2" into a representation of a fraction. You're asking how to parse a string, store a fraction, and store a problem consisting of two fractions and a mathematical operation. – Jack Deeth Dec 02 '16 at 02:29
  • 1
    You've got a start here. **Keep going**. – tadman Dec 02 '16 at 02:31
  • @JackDeeth Yes, I guess that's what I'm ultimately asking. – vincelam1998 Dec 02 '16 at 02:33
  • Are you, [and this fella](http://stackoverflow.com/questions/40923052/c-how-to-calculate-fractions-giving-in-a-string) in the same class, by any chance? This is too broad. You have to write an entire expression parser. Either you misunderstood your homework assignment, or you have an incompetent instructor, for giving you such an assignment without presenting sufficient material, in class, that's needed to implement something like this. We're talking lex/yacc, here... – Sam Varshavchik Dec 02 '16 at 02:34
  • Voted to **close as too broad**. There are zillions of different approaches. – Cheers and hth. - Alf Dec 02 '16 at 02:39
  • @SamVarshavchik probably not haha. – vincelam1998 Dec 02 '16 at 02:39
  • @paxdiablo I'm not sure how to separate the string so that I could evaluate them. – vincelam1998 Dec 02 '16 at 03:08

3 Answers3

1

If there's an idiomatic way to express rational numbers (aka integer fractions) using the C++ Standard Library, I haven't found it.

However this guy's written a useful class for storing fractions and doing mathematical operations on them: https://github.com/featdd/FractionClass

Featdd's Fraction even has a string constructor - if you can get the input down to "1/2" you're in business.

So you're left with reading a string, splitting it into "a/b", "+", "c/d" (where "+" could be any of the four main maths operations), and calculating the results.

I suggest you take a look at this earlier question for ideas on how to parse the string!

Community
  • 1
  • 1
Jack Deeth
  • 1,813
  • 16
  • 26
0

If I'm understanding your question correctly, you'll have to parse the string and break it down into its components. Assuming that your formatting above 1/4(space)+(space)1/2 is consistent, you could split the first fraction, the operation, and the second fraction into 3 strings (using the space as a delimiter). There is a good [OPINION] split function here that I have used before.

At this point you would have the following strings: "1/4", "+", "1/2". From here you could continue to break down the fractions (using the / as your delimiter), giving you numerator and denominators for both fractions.

Community
  • 1
  • 1
Ryan Appel
  • 35
  • 7
0

I agree a parser (of any complexity) is probably not the simplest approach. Definately a good thing to learn ... but you first need to learn enough C++ to get at least a little progress.

Perhaps you might consider:

with input (at std::cin) :

1/4 + 1/2

your code needs to detect the integer 1 first in sequence.

Because this is a C++ question, a starting place for code to consider might be:

int n1 = 0;  // numerator of fraction 1

std::cin >> n1;
std::cout << "n1: " << n1 << std::endl; // during development, echo check

Next is the slash (numerator first)

char slash = 0;
std::cin >> slash;
std::cout << "slash: " << slash << std::endl;
assert(slash == '/');  // user entry error kills the program

Now the denominator:

std::cin >> d1; // denominator of fraction 1
std::cout << "d1: " << d1 << std::endl; // echo check

Now you have fraction 1 in two integer variables.


Do something similar to capture oper, n2, and d2.

Now with n1, d1, oper, n2, d2, do you think you can figure out how to proceed?


before you try too much more, it would be good for you to learn how to write a function. It appears you have several.

Might I suggest,

"void getFraction(int& n, int& d)"; // prompts user, gets two integers

Create this function, and see if you can read in f1 and echo it.


Now you can see that two un-associated ints will be inconvenient. So try, maybe

class Fraction;

which contains two integer, ctor, dtor.

Then move the getFraction() function into the class as "get()" Add method show() to class.

And proceed.

Good luck

2785528
  • 5,162
  • 2
  • 16
  • 18
  • Thank you! Yeah we haven't learned about parsing strings yet. – vincelam1998 Dec 02 '16 at 02:57
  • This is for user input though, no? My program has the fraction problem on one single string. There is no user input. – vincelam1998 Dec 02 '16 at 02:59
  • I used std::cin in my examples. If you put your 'single string' into a "std::stringstream ssIn", my examples will work identically (replacing std::cin with ssIn). Another option ... code to read from std::cin, then rely on redirecting your program input (if allowed). stream i/o is quite flexible. – 2785528 Dec 02 '16 at 04:12
  • I agree that this problem could be a big effort for a C++ entry level class. It might be worth your while to check with the TA ... maybe they have a 'standard approach' that avoids overly complex approaches. If you bring your code and notes, he might at least encourage your, or redirect your efforts. Good luck. – 2785528 Dec 02 '16 at 04:17