简体   繁体   中英

for loop exits after putting first number into array

I have a small program that asks for a length of an array and asks for numbers to put into the array positions. So far it asks for array length, and lets me enter the first number but then bugs out with exited with non-zero status.

#include <iostream>

int main ()
{
  int i;
  std::cout << "array length: ";
  std::cin >> i;
  int* myAarray = new int[i];
  for(int e=0, myArray; e<=i; e++){
      std::cout << "Number to put in:";
      int a;
      std::cin >> a;
      myArray[&e] = a;
  }


  return 0;
}

Edit: Typed my variable names in wrong, thanks for the help!

This loop

  for(int e=0, myArray; e<=i; e++){
      std::cout << "Number to put in:";
      int a;
      std::cin >> a;
      myArray[&e] = a;
  }

is wrong.

The valid loop could like like

  for ( int e = 0; e < i; e++ ){
      std::cout << "Number to put in:";
      int a;
      std::cin >> a;
      myArray[e] = a;
  }

I suppose that the pointer is defined like

int* myArray = new int[i];

instead of

int* myAarray = new int[i];

that is it has name myArray.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM