I'm trying to input some numbers into an array. Pretty simple task.
int array[100], n = 0, length = 0;
std::cout << "Input numbers: " << std::endl;
while (std::cin >> n) {
array[length++] = n;
}
I press shift + F10
in CLion, trying to run it and
it won't end the while (I pressed enter 5 times in a row), it goes on forever. am I doing something wrong here?
I tried using std::cin.ignore()
after each input. It does not seem to have any effect.
Thank you!
EDIT: It does successfully end when I press ctrl-D
, but I've encountered another problem. (I edited the title)
I have this program:
void read_input(int arr[], int &length) {
int n = 0;
std::cout << "Read input: " << std::endl;
while (std::cin >> n) {
arr[length++] = n;
}
}
int main() {
int array[100], length = 0;
int c = -1;
while (true) {
std::cout << "Menu: (TO DO)\n";
std::cout << "Your option: ";
std::cin >> c;
if (c == 1) {
read_input(array, length);
}
if (c == 0) break;
}
}
What happens is, I enter the option:
1
Read input:
1
2
3
4
^D
Menu: (TO DO)
Your option:
Read input:
Menu: (TO DO)
Your option:
Read input:
Menu: (TO DO)
Your option:
Read input:
...
Basically, after it goes into read_input() and I give it some numbers, I press ctrl-D and it won't ask me again for an input/option, it will just read 1
for std::cin >> c;
again and again and it will go on forever.