# Storing to Arrays

• 01-07-2013
th3unforgiving
Storing to Arrays
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.
Code:

``` #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; } ```
Thanks in advance for any tips guiding me in the right direction! :D
• 01-07-2013
Lesshardtofind
Hey!

Just a tip for you turn up your warnings and compile things. If you post with an error message then we at least know what to focus on

These are my warnings/ errors
Code:

```main.cpp:4:1: error: ‘usingnamespace’ does not name a type main.cpp:7:1: error: ‘classfraction’ does not name a type main.cpp:150:10: error: expected constructor, destructor, or type conversion before ‘(’ token```
So the first one points to line 4 and it doesn't recognize usingnamespace. When you inspect that you might realize that:
Code:

`usingnamespace std;`
should be
Code:

`using namespace std;`
since using namespace std isn't being recognized properly it won't reconize the cin explaining your first issue.
Quote:

cin is giving me an error "no operator '>>' matches these operands".
Second one line 7 says classfration does not name a type. What does that mean? Well every data has a type associated with it. Your classes work the same way so C++ is assuming you are meaning to declar something named classfraction. I'm guessing what you intending was to declare a class as the type and fraction as the name. Something like this:
Code:

`classfraction`
changed to
Code:

`class fraction{`
last one points to your int main it expects a valid constructor before the ( because my complier doesn't recognize
Code:

`int_tmain(intargc, _TCHAR* argv[])`
As a valid declaration of the main entry point for the program. It is looking for something a little more like
Code:

`int main(int argc, char* argv[])`
There will be more error messages to follow these fixes but post your complete list of error messages as learning to read these will lead to better programming and understanding of the language.

"You can give a man debug tips for a day, but teach him to read error messages and he will debug for life." ;o)

PS: The reason my compiler might not recognize the main declaration is my lack of stdafx.h on my system as I am building in G++ on Ubuntu.