Thread: Storing to Arrays

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Question 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!

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    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.
    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.
    Last edited by Lesshardtofind; 01-07-2013 at 09:59 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with storing arrays
    By tcumech in forum C Programming
    Replies: 12
    Last Post: 05-06-2010, 02:34 AM
  2. Arrays and storing values
    By atlas07 in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2010, 08:46 PM
  3. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  4. 2D arrays and storing characters
    By John_L in forum C Programming
    Replies: 4
    Last Post: 10-13-2007, 12:17 PM
  5. Storing values in arrays
    By Ripper1 in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2003, 11:04 AM

Tags for this Thread