Open Side Menu Go to the Top

02-05-2013 , 03:50 PM
All is well and good here, the code works fine. I just don't understand why numberArray[i] isn't element 1 and is instead element zero. Does the statement "please enter #" << i+1 << ": "; only impact i during that statement?
Code:
#include <cstdlib>
#include <iostream>
#include <new>

using namespace std;

void getInput (int *piUserInput);

int main()
{
    int n = 0;
    int *piUserInput = new int(0);
    int* numberArray = 0;
           
    // Create a function for getting user input   
    getInput(piUserInput);
    
    numberArray = new int[*piUserInput];
    //numberArray[0] = *piUserInput;
    //create array that gets that amount of numbers
    for(int i = 0; i < *piUserInput; i++)
        {
            cout << "Please enter #" << i+1 << ": ";
            cin >> n;
            numberArray[i] = n;
        }
    for(int i = 0; i < *piUserInput; i++)
        {
            cout << "array # is "  << numberArray[i] << endl;

        }
    //cout << "Please enter #" << adjustedInput << ": " << PopulateArrayElement <<endl;
    
    //cout << "The smallest number entered was " << smallArrayElement << endl; 
    delete piUserInput;
    delete numberArray;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
void getInput(int *piUserInput)
{
    cout << "How many number would you like to enter: ";
    cin >> *piUserInput;
}
I'm working with an array and asking the users to input how many numbers they want.

Then my statement is

" please enter #" << i+1 << ": ";

then I have them store the number into n with
"cin >> n;"

Then I put N into array element i
numberArray[i] = n;

isn't the value of i in this moment 1?
c++ array question Quote
c++ array question
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
c++ array question
02-05-2013 , 04:34 PM
The answer to your question is no. The value of i doesn't change until you do something to change it.

initially i is 0

(i+1) is an expression with value 1

An expression doesn't change anything.

i = i + 1;

That's an assignment which changes the value of i to the value of the expression making i == 1.

i++ is a shorthand assignment you can toss in where you like. It means the same as the previous assignment and executes sometime after your current statement.
c++ array question Quote
02-06-2013 , 12:07 AM
If you do:

i = 0
cout << i << i +1 << i +2 << endl;

you'll get: 0 1 2
not: 0 1 3
c++ array question Quote
02-06-2013 , 01:42 PM
Thanks guys! That's one of those things I was in my memory but I over thought. Appreciate the help!
c++ array question Quote
02-06-2013 , 05:22 PM
And:
Code:
i = 0;
cout << i++;
cout << i++;
will output 0 and then 1 and the value of i will be 2 when the program is done, but:

Code:
i = 0
cout <<++i;
cout << ++i;
will output 1 and then 2 and the value of i will be 2 when the program is done. So, it depends on what you want.
c++ array question Quote
c++ array question
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
c++ array question

      
m