Hey everyone, I'm having some trouble storing user input to an array in C++. I have created a class defining a fraction containing a numerator, denominator, and dash(/). I created an array of type fraction but I can't seem to store a user-inputted fraction to this array. cin is giving me an error "no operator '>>' matches these operands". I'm at a loss now.Thanks in advance for any tips guiding me in the right direction! :DCode:
#include"stdafx.h"
#include<iostream>
usingnamespace std;
classfraction
{
private:
int numerator, denominator; //numerator and denominator variable initialization
char dash; // "/"
public:
void getFraction() //get fraction function
{
cout << "Enter fraction (ex. 1/2) "; //prompts user to enter fraction
cin >> numerator >> dash >> denominator; //reads user input from console
}
void displayFraction() //display fraction function
{
cout << numerator << dash << denominator << endl; //prints fraction to console
}
void addFraction(fractiona, fractionb) //add fractions function
{
numerator = (a.numerator * b.denominator) + (b.numerator * a.denominator); //calculates numerator
denominator = a.denominator * b.denominator; //calculates denominator
}
void subtractFraction(fractiona, fractionb) //subtract fractions functions
{
numerator = (a.numerator * b.denominator) - (b.numerator * a.denominator); //calculates numerator
denominator = a.denominator + b.denominator; //calculates denominator
}
void multiplyFraction(fractiona, fractionb) //multiply fractions function
{
numerator = a.numerator * b.numerator; //calculates numerator
denominator = a.denominator + b.denominator; //calculates denominator
}
void divideFraction(fractiona, fractionb) //divide fractions function
{
numerator = a.numerator * b.denominator; //calculates numerator
denominator = a.denominator + b.numerator; //calculates denominator
}
voidfraction::lowterms() // change ourself to lowest terms
{
long tnum, tden, temp, gcd;
tnum = labs(numerator); // use non-negative copies
tden = labs(denominator); // (needs cmath)
if(tden==0 ) // check for n/0
{
cout << "Illegal fraction: division by 0"; exit(1);
}
elseif( tnum==0 ) // check for 0/n
{
numerator=0;
denominator = 1;
return;
}
// this ‘while’ loop finds the gcd of tnum and tden
while(tnum != 0)
{
if(tnum < tden) // ensure numerator larger
{
temp=tnum;
tnum=tden;
tden=temp;
} // swap them
tnum = tnum - tden; // subtract them
}
gcd = tden; // this is greatest common divisor
numerator = numerator / gcd; // divide both num and den by gcd
denominator = denominator / gcd; // to reduce frac to lowest terms
cout << "Lowest Terms: " << numerator << dash << denominator << endl; //outputs lowest terms fraction to console
}
void averageFraction()
{
}
};
int_tmain(intargc, _TCHAR* argv[])
{
fraction a, b; //fraction 1 and fraction 2
char op, cont; //op = operator, cont = continue?
int choice;
for(int i = 1; i != 0;) //for loop running until user enters 'n'
{
int showanswer = 1; //decides whether or not to show answer
cout << "Would you like to average fractions, or perform an operation? (1 = average, 2 = other) ";
cin >> choice;
if(choice == 1)
{
int arraysize = 0, numerator, denominator;
char dash;
cout << "How many fractions would you like to average? (Positive Integer)";
cin >> arraysize;
fraction *array = newfraction[arraysize];
for(int i = 0; i < arraysize; i++)
{
cout << "Enter a fraction to average: (Ex. 3/4)";
cin >> *array;
}
cout << "You have chosen average!\n";
}
elseif(choice == 2)
{
a.getFraction(); //calls getFraction function
cout << "Enter Operator (+ - * /): "; //prompts user to enter operator
cin >> op; //reads operator and stores in op
b.getFraction(); //calls getFraction function
if(op == '+'){ //checks if op is +
a.addFraction(a, b); //calls add function if true
i = 0; //sets i to 0
}
elseif(op == '-'){ //checks if op is -
a.subtractFraction(a, b); //calls subtract function if true
i = 0; //sets i to 0
}
elseif(op == '*'){ //checks if op is *
a.multiplyFraction(a, b); //calls multiply function if true
i = 0; //sets i to 0
}
elseif(op == '/'){ //checks if op is /
a.divideFraction(a, b); //calls divide function if true
i = 0; //sets i to 0
}
else{
cout << "You have entered an invalid operator, please use +, -, *, or /\n"; //prints to console if not + - * /
i = 1; //sets i to 1
showanswer = 0; //doesn't show answer
}
if(showanswer == 1){ //if show answer is 1, run below
a.displayFraction(); //displays fraction
a.lowterms(); //displays fraction in lowest terms
cout << "\nWould you like to perform another calculation? (y/n) "; //asks user if they would like to perform another calculation
cin >> cont; //reads user input and stores in cont
if(cont == 'y'){ //if yes
i = 1; //run loop again
}
elseif(cont == 'n'){ //if no
i = 0; //end loop and exit
}
else{
cout << "You have entered an invalid key. Exiting\n"; //if incorrect key
i = 0; //exit
}
}
}
else
{
cout << "Invalid Option\n";
}
}
return 0;
}