So I have been writing a procedure, inside which I need to take input of two variables, int data and int index. Inside my main function, I have a while loop which takes input until a non integer value is entered.
The problem: When I call the procedure before the while loop inside the main function, everything works fine. However, when I call the procedure after the while loop, the console does not ask me for inputting int data and int index. I don't understand why?
What do I know so far and what I have tried: I have faced a similar problem with the getline() function for strings, and I was able to solve it with cin.ignore(), however, I cannot solve this one with cin.ignore(). I read quite a lot of related posts, but still can't figure it out.
My code
#include<iostream>
using namespace std;
void proced() {
int data, index;
cin >> data >> index;
cout << "this is data: " << data << endl;
cout << "this is index: " << index << endl;
}
int main() {
int temp;
int dance=0;
while (cin >> temp) {
dance = temp;
}
cout << "this is dance: " <<dance<< endl;
proced();
}
output when procedure is called after while loop
input: 1 2 3 4 5^Z
output:
this is dance: 5
this is data: -858993460
this is index: -858993460
output when procedure is called before while loop
input: 1 2 3 4 5^Z
output:
this is data: 1
this is index: 2
this is dance: 5
Ps. I am coding in visual studio 2019