ETERNAL OBLIVION
ETERNAL OBLIVION

Reputation: 71

C++: using for loop to allow user input of numbers into array

I'm new to the community and to coding as well. Right now I'm taking Intro to Computer Science at my CC and we're learning C++. Anyways, I have to create a program which asks the user for a number, which will be the size indicator of the array new_array. The program then asks the user to input the numbers one by one and afterwards, outputs them in reverse. #include using namespace std;

int main()
{
cout << "How many numbers?\n";
int numbers; // holds amount of numbers to be entered into array
cin >> numbers;
int new_array[numbers];
for(int counter = 0; counter < numbers; counter++)
{
    cout << "Enter number " << counter << endl;
    cin >> new_array[counter];
}

cout << "You entered: " << endl;

for(int i = numbers; i >= 0 ; i-- )
{
    cout << new_array[i] << endl;

}

return 0;
}

I understand how to do this and for the most part, my program worked. It outputs the numbers entered in reverse just fine, but before it does so, it outputs large, strange numbers. For example, if the user enters 5 as the amount of numbers to be entered, and then enters 1, 2, 3, 4 and 6 as the 5 numbers respectively, the program outputs the number 4669476 first and then outputs the numbers in the array in reverse. Can anyone explain to me what I did wrong and how I could fix this? Thank you in advanced! PS be gentle! I'm a newbie at this

Upvotes: 1

Views: 10811

Answers (5)

Nauman Ahmad
Nauman Ahmad

Reputation: 320

Because you are starting the index from out of range giving you garbage value.

your code should look some thing like this

 for(int i = numbers-1; i >= 0 ; i-- )
 {
     cout << new_array[i] << endl;
  }

Upvotes: 0

M.M
M.M

Reputation: 141633

This loop reads out of bounds:

for(int i = numbers; i >= 0 ; i-- )
{

If you follow i through in your head you will see that you output entries numbers through to 0, when in fact you should output entries numbers-1 through to 0.

An alternative patterns is:

for( int i = numbers; i--; )

Or you can use the fabled --> operator.

It would be possible to "simply" start from numbers - 1, however the loop pattern you have used would not work for an unsigned counter (because they are always >= 0). IMHO it is a good idea to use a pattern which works for all types; then you are less likely to make a mistake in future.

Upvotes: 1

Ray P.
Ray P.

Reputation: 1

In C arrays are stored from 0 instead of 1. So the last number is stored in array[4]

So when you're writing it out you should start an numbers - 1 instead of just numbers.

Upvotes: 0

Husain
Husain

Reputation: 845

Because you start from array[numbers] which is not defined.

array[0], array[1], ... array[numbers-1] are defined.

Upvotes: 0

Khalil Khalaf
Khalil Khalaf

Reputation: 9397

In your display for loop, you started from i = numbers which is out of the array's range. Since the array starts from 0 till size - 1, then you need to start from i = numbers - 1 all the way to >=0.

Upvotes: 0

Related Questions