-1

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;
  }
APD
  • 159
  • 2
  • 12
  • May be using your preferred debugging tool, and stepping through your code line by line could shed some light on what's actually wrong. But it's ***you***, not us to do this, and investigate! – πάντα ῥεῖ Jun 13 '14 at 19:19
  • what does this have to do with `getchar_unlocked`? `getchar` will behave identically (and you should use it in preference, unless you know what you are doing and have called `flockfile` previously) – Chris Dodd Jun 13 '14 at 19:37
  • My debugger isnt working properly. I had even posted a question regarding it a few days back http://stackoverflow.com/questions/24197452/code-blocks-10-05-debugger-in-ubuntu-unable-to-use-it – APD Jun 14 '14 at 08:01
  • @Chris Dodd- I assumed that getchar_unlocked was an a time saving extension of getchar. I have come across the term flockfile for the first time – APD Jun 14 '14 at 08:03
  • @weston, Final Contest and Rakibul Hasan - How is the desired behaviour of my code not explained when the statement above the code begins in that tune? Also, how could i have possibly reduced my code further? All that my code contains is an int main() to describe the input procedure and another function scan() which describes the problem i am facing ?? – APD Jun 14 '14 at 08:40

2 Answers2

0

You are assigning the value of r to pointer a with a=r;, should be *a=r; and you have similar problems with n=10*n + ch -'0'; should be *n=10*(*n) + ch -'0';. Never forget your function arguments ( int &n, int &a) make n and a pointers and will need to be dereferenced to assign values. In fact, in the original thread, I took the arguments to be pseudo-code (int &n, int &a), int actual implementation, I would do (int *n, int *a) and adjust the code accordingly.

Here is how I updated the original, this illustrates the pass by reference implementation:

#include <stdio.h>

#define getcx getchar_unlocked

/* input parser reads - sign, and any digits that follow */
inline void inp ( int *num )
{
    int n = 0;
    int sign = 1;
    char ch = getcx();

    /* get the sign */
    while ( ch < '0' || ch > '9' )
        { if (ch == '-') sign = -1; ch = getcx();}

    /* add each char read. n is accumulator, (n << 3) + (n << 1) is just n * 10 */
    while (  ch >= '0' && ch <= '9' )
        n = (n << 3) + (n << 1) + ch - '0', ch = getcx(); /* = n*10 + ch - 48 */

    n = n * sign;

    *num = n;
}

int main (void) {

    int number = 0;
    printf ("enter something containing nummbers\n");

    inp (&number);

    printf ("number: %d\n", number);

    return 0;

}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Aren't a and n being passed as reference variables ? – APD Jun 14 '14 at 08:08
  • You are correct, updated. – David C. Rankin Jun 14 '14 at 20:18
  • This is the usual procedure which returns negative nos. also. My code differed from it because i wanted to print the position of the first space entered from the console. Hence i introduced a variable r which was incremented for each digit entered . When the user would enter a space, r's value should have been transfered to a. But sadly, it didnt execute that way which confused me. I removed the negative no. (sign variable step ) because i didnt need it. – APD Jun 15 '14 at 05:23
0

The scan function skips non-digits, then reads 1 or more digits as an integer into n, and finally skips a single non-digit character. In the first loop to skip non-digits, it counts the number of non-spaces read and stores that number into a whenever it sees a space. If it never sees a space, a will remain unmodified.

So if you give your program an input like 10 20 (with just a single space), that space will be skipped by the first call to scan (the non-digit after the number), and a will never be initialized.

If you want your scan routine to not skip the character after the number it reads, you need to call ungetc to put it back after reading it and discovering that it is not a digit.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • wr2 what you've mentioned in your second para, when the loop is run to skip a charachter ,Shouldn't the same loop be initializing a too – APD Jun 14 '14 at 08:07
  • No, because it is skipped by the second loop in `scan` (the one reading digits) which does not set a. – Chris Dodd Jun 15 '14 at 04:31
  • okay, so when it encounters a space, it comes out of the loop . One more query, why doesn't the control go out of the function then ? Why does the control get transfered to the first line of the function.? – APD Jun 15 '14 at 05:29
  • Why do you think control goes to the first line of the function? After reading the space, `scan` returns to main, which call `printf` and then `scan` again -- so it gets back to the first line that way, but it returns first. – Chris Dodd Jun 15 '14 at 06:40
  • Ok.. thanks al lot.. this really cleared the cloud – APD Jun 15 '14 at 08:32