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);
}
}