I wanted to change the general getchar_unlocked program to print the position of space when the second number is entered.So i introduced 2 more variables r and a, such that r gets incremented every time a non space character is input and a gets r's value when a space character is input. However, by the change i made only the first no. is getting stored in s and a garbage value is getting stored in a. Why so?
#include <cstdio>
#define getcx getchar_unlocked
using namespace std;
inline void scan( int &n, int &a) //extra parameter a added
{
n=0; int r=0;
char ch;
ch=getcx();
while (ch<'0' || ch>'9') //Inclusion of a !=' ' condition
{
if (ch==' ')
a=r;
else
r++;
ch=getcx();
}
while (ch>='0' && ch<='9')
{
r++;
n=10*n + ch -'0';
ch=getcx();
}
}
int main()
{
int t,s,a;
scan(t,a);
printf("%d\n",t);
scan(s,a);
printf("%d",s);
printf("%d",a);
return 0;
}