Thread: How to add/subtract from array element...

  1. #1
    Unregistered
    Guest

    How to add/subtract from array element...

    */

    // Includes

    #include<iostream.h> // for << and >>
    #include<iomanip.h> // for setw and setf
    #include<fstream.h> // for file I/O
    #include"apstring.h" // for apstring identifiers
    // Function Prototypes



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~ Main Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~



    int main ()
    {

    cout.setf(ios::fixed, ios::floatfield); // Set up floating point
    cout.setf(ios::showpoint); // output format

    // Declaration and initialization of variables

    apstring prices[5]; // array of grades
    apstring items[5]; // array of grades
    apstring quantities[5]; // array of grades
    ifstream old; // Holds the figures

    // Open the files

    old.open("Ald.txt");

    // Get data // Main Function statements

    for (int i = 0; i <= 4; i++)
    {
    old >> items[i];
    old >> prices[i];
    old >> quantities[i];
    }

    cout << " item" << " price" << " quantity" << endl;

    for (i = 0; i <= 4; i++)
    {
    cout << i << " " << setw(6) << items[i] << " " << prices[i] << " " << quantities[i] << ' ' << endl;
    }
    cout << endl;

    cout << "Please enter a position value for the price array: ";
    cin >> i;
    cout << prices[i] << endl;

    cout << "Please enter a position value for the quantity array: ";
    cin >> i;
    cout << quantities[i];

    return 0;

    }// end main

    HERE'S THE PROB.

    cout << "Please enter a position value for the quantity array: ";
    cin >> i;
    cout << quantities[i];

    how do i subtract one from the value of quantites[i]?

    say quantities[i] = 20
    how do i make it equal 19??

  2. #2
    quantities[i] -= 1; // subtract 1 from the value held at this position in the array.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Unregistered
    Guest
    i tried that before i asked this question but it didn't work...
    hmmmm.....i'll try it again at school (in an hour)

  4. #4
    Unregistered
    Guest
    cout << "Please enter a position value for the quantity array: ";
    cin >> i;
    cout <<(quantities[i] -1);
    return 0;

    }// end main

    not working

    "illegal structure operation in function main"

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    quantities is declared as an array of apstrings, not an array of ints. I suspect the - operator is not overloaded for apstrings, hence the error message. If the apstrings are holding a string like 20, then I suspect you will need to convert the string value to an int value and do the subtraction. The apstring class may or may not have such a conversion function built in. I don't use apstrings so I don't know. Other string classes I am familiar do have such a function, so I suspect apstring will too. If it doesn't I would convert the apstring to a c-style string and then pass it to atoi(), which should work too.

  6. #6
    You haven't really been clear about whether you want the position of the array element or the value contained within the array.

    Your attempt at implementing my suggestion was missing the most important element: =-

    you wrote:
    cin >> i;
    cout <<(quantities[i] -1);
    it should be cout << quantities[i] -=1;

    Make sure the array has been created first and also been filled with values. The tip I gave was for the value inside the array.

    It seems you want the position index of the array - make sure you've declared
    int i;
    int quantities[20];
    NOW you can fill a certain array element with a value

    cin >> i;
    cout << quantities[i]; //retreive the value at this array position
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    If using cout >> quantities[i] - 1 bothers you, first set quantities[i] to quantities[i] - 1, then cout quantities[i].
    Truth is a malleable commodity - Dick Cheney

  8. #8
    Unregistered
    Guest

    Thumbs up

    yeah had to make the quantities array an int....thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM