Problem with taking 2 inputs on serial monitor

i want to take 2 inputs from user on serial monitor and i have coded it accordingly . But its taking the second input 0 as it is defined as global variable and if i give some delay such as 5s and enter the second variable within the time limit, its taking the particular value. And if I am doing it without any delay withing milliseconds its taking the value as 0 by default. what can i do to resolve this issue.

This is working with delay of 5 seconds and i have to enter the value before 5 secs.

int x,y;
void setup() {
  Serial.begin(9600);
  takeinp1();
  takeinp2();

}

void loop() {
  Serial.print(x);
  Serial.print(" ");
  Serial.println(y);
}
void takeinp1()
{
  int a;
  Serial.print("Enter a");
  while(!Serial.available()){}
  a=Serial.parseInt();
  x=a;
}
void takeinp2()
{
  int b;
  Serial.print("Enter b");
  while(!Serial.available()){}
  delay(5000);
  b=Serial.parseInt();
  y=b;
}

this will take the input by default

int x,y;
void setup() {
  Serial.begin(9600);
  takeinp1();
  takeinp2();

}

void loop() {
  Serial.print(x);
  Serial.print(" ");
  Serial.println(y);
}
void takeinp1()
{
  int a;
  Serial.print("Enter a");
  while(!Serial.available()){}
  a=Serial.parseInt();
  x=a;
}
void takeinp2()
{
  int b;
  Serial.print("Enter b");
  while(!Serial.available()){}
  b=Serial.parseInt();
  y=b;
}

Welcome to the forum

If the Line ending of the Serial monitor is set to anything other than none then your code will not work because the line ending character(s) will be available() after reading the user input

2 Likes