#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Unexpected string output


    hey i m using code::blocks for c programming and when i run the code, it stops in the middle.
    When i take help of debugger in below code it shows ..
    a syntax error in expression near "if"..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
     char s1[10];
     int i=0;
     printf("enter s1 \n");
        i=0;
    
     while(getchar()!='\t')
     {
         printf("entered while loop.Iteration no. %d \n",i);
         s1[i]=getchar();
         i++;
         printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
     }
    
      i=i-1;
      s1[i]='\0';
    
    printf("Iteration no. %d & s1 char is: %s \n",i,s1);
    printf("\n");
    
     for(i=0;s1[i]!='\0';i++)
     {
         printf("Entered for loop \n");
      if(((s1[i]>='a')&&(s1[i]<='z'))||((s1[i]>='A')&&(s1[i]<='Z')))
      {
          printf("Entered if condition \n");
          printf("Iteration no. %d \n",i);
          s1[i]=s1[i+1];
          printf("Iteration no. %d \n",i);
      }
     }
        puts(s1);
        return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    can you tell what output you want or what are you trying to do exactly?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    @swapy hi.just trying to replace the preceding character with the following one till \0. eg: here pots=>ots.
    but there is some syntactical error.
    Originally Posted by swapy
    can you tell what output you want or what are you trying to do exactly?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    your loop condition seems somewhat wrong... what if i becomes 10 or more and i dont enter a space?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    a space? where? i is initialized 0 in for loop
    Originally Posted by swapy
    your loop condition seems somewhat wrong... what if i becomes 10 or more and i dont enter a space?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    The debugger raised a syntax error? Not the compiler? That's a first for me in my decades of experience. BTW, compiling that with gcc and all warnings on compiled clean.

    Is your input string what you expect it to be? Your input code looks like it's only inputting every other character:
    Code:
     while(getchar()!='\t')
     {
         printf("entered while loop.Iteration no. %d \n",i);
         s1[i]=getchar();
         i++;
         printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
     }
    Perhaps you were intending something like this:
    Code:
        char ch;
    
        //  intervening code
    
     while((ch = getchar()) != '\t')
     {
         printf("entered while loop.Iteration no. %d \n",i);
         s1[i]=ch;
         i++;
         printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
     }
    That second printf also doesn't look right, since you're displaying a character in s1 that you haven't written to yet. For example, the first character goes into s1[0], but then you increment i before echoing it out, so you are in fact "echoing" out s1[1], which, since you have not yet written anything there, contains whatever garbage is there. My recommendation is that the i++; should be moved to the end of the loop. Or replace the while with a for.

    When your output doesn't look right, one of the first things to check is whether your input is right.
    Last edited by dwise1_aol; February 4th, 2013 at 10:38 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    @dwise God you are so right abt the increment i before echoing it out,damn silly mistakes.
    And thanks a lot about that "Skipping" issue!
    That solves everything.
    And yea man im nice with the compiler cut lose sm slack man.

    Is your input string what you expect it to be? Your input code looks like it's only inputting every other character
    Q: Why is I/p string I/putting every other character???

    Originally Posted by dwise1_aol
    The debugger raised a syntax error? Not the compiler? That's a first for me in my decades of experience. BTW, compiling that with gcc and all warnings on compiled clean.

    Is your input string what you expect it to be? Your input code looks like it's only inputting every other character:
    Code:
     while(getchar()!='\t')
     {
         printf("entered while loop.Iteration no. %d \n",i);
         s1[i]=getchar();
         i++;
         printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
     }
    Perhaps you were intending something like this:
    Code:
        char ch;
    
        //  intervening code
    
     while((ch = getchar()) != '\t')
     {
         printf("entered while loop.Iteration no. %d \n",i);
         s1[i]=ch;
         i++;
         printf("Iteration no. %d & s1 char is: %c \n",i,s1[i]);
     }
    That second printf also doesn't look right, since you're displaying a character in s1 that you haven't written to yet. For example, the first character goes into s1[0], but then you increment i before echoing it out, so you are in fact "echoing" out s1[1], which, since you have not yet written anything there, contains whatever garbage is there. My recommendation is that the i++; should be moved to the end of the loop. Or replace the while with a for.

    When your output doesn't look right, one of the first things to check is whether your input is right.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    Originally Posted by lovecodecakes
    @dwise God you are so right abt the increment i before echoing it out,damn silly mistakes.
    And thanks a lot about that "Skipping" issue!
    That solves everything.
    And yea man im nice with the compiler cut lose sm slack man.


    Q: Why is I/p string I/putting every other character???
    because you have used getchar()... it takes only 1 char input at a time...
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by lovecodecakes
    Q: Why is I/p string I/putting every other character???
    Because your loop has two calls to getchar(), only the return value from one of which was assigned to the string, the other being discared after checking for TAB.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by lovecodecakes
    And yea man im nice with the compiler cut lose sm slack man.
    I don't know what you're talking about here.

    Originally Posted by lovecodecakes
    Q: Why is I/p string I/putting every other character???
    Because you're calling getchar() twice for every character you actually save as input. First you get a character to test it for being a tab. Then you get the next character to place into s1. Then you get the third character to test whether it's a tab. Then you get the fourth character place into s1 -- at this point, you have processed four characters from the input buffer and have only placed two of them into s1.

    Take a look at my suggested code. In while, you getchar a character and assign it to a char variable, thus saving it for use. Then in the same while, you test that char for not being a tab; remember that the value of an assignment expression is the value being assigned! Then within the body of the while loop when you insert that character into the s1 array, you do still have that character because you had saved it in that char variable. And this is just one of a number of approaches you could have taken.

    Sometimes, a very good debugging technique is to take paper and pencil in hand and play computer. Go through your program step-by-step and do everything that it tells you to do, keeping track of variable values on the paper. In your case, you should fill the input buffer with the string of characters that you intend to type in and keep track of which character each and every call to getchar() "eats" and which character will then be the next to be "eaten".

    One big problem that programmers have is the same that writers have: when we read our own code we almost never see what we have actually written, but rather what we "see" what we remember intending to have written. One solution is to have somebody else have a look, much like a writer will have somebody else proofread their work. Usually that different set of eyes will see an error jump right out of them, one that we were ourselves blind to. Or that other person have questions about something that we had thought we had taken care of but hadn't yet. Another solution for when ours is the only set of eyes available is to read through our program painstakingly paying attention to each detail. One way of doing this is to play computer with pencil and paper. Another way is to sit down with somebody else and walk them through our program explaining exactly what it is doing; this forces you to think through each line enough to be able to explain it, whereupon your obvious mistake will suddenly become apparent to you.

    It's a very common problem that we all have been through and continue to have to deal with.
    Last edited by dwise1_aol; February 4th, 2013 at 02:54 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Thanks yea I edited my code & its up and running. Thanks for the advice on proofreading.Skipped on reading twice from I/p.


    Originally Posted by dwise1_aol
    I don't know what you're talking about here.


    Because you're calling getchar() twice for every character you actually save as input. First you get a character to test it for being a tab. Then you get the next character to place into s1. Then you get the third character to test whether it's a tab. Then you get the fourth character place into s1 -- at this point, you have processed four characters from the input buffer and have only placed two of them into s1.

    Take a look at my suggested code. In while, you getchar a character and assign it to a char variable, thus saving it for use. Then in the same while, you test that char for not being a tab; remember that the value of an assignment expression is the value being assigned! Then within the body of the while loop when you insert that character into the s1 array, you do still have that character because you had saved it in that char variable. And this is just one of a number of approaches you could have taken.

    Sometimes, a very good debugging technique is to take paper and pencil in hand and play computer. Go through your program step-by-step and do everything that it tells you to do, keeping track of variable values on the paper. In your case, you should fill the input buffer with the string of characters that you intend to type in and keep track of which character each and every call to getchar() "eats" and which character will then be the next to be "eaten".

    One big problem that programmers have is the same that writers have: when we read our own code we almost never see what we have actually written, but rather what we "see" what we remember intending to have written. One solution is to have somebody else have a look, much like a writer will have somebody else proofread their work. Usually that different set of eyes will see an error jump right out of them, one that we were ourselves blind to. Or that other person have questions about something that we had thought we had taken care of but hadn't yet. Another solution for when ours is the only set of eyes available is to read through our program painstakingly paying attention to each detail. One way of doing this is to play computer with pencil and paper. Another way is to sit down with somebody else and walk them through our program explaining exactly what it is doing; this forces you to think through each line enough to be able to explain it, whereupon your obvious mistake will suddenly become apparent to you.

    It's a very common problem that we all have been through and continue to have to deal with.

    Comments on this post

    • clifford disagrees : Closing threads is for when the conversation becomes abusive or unconstructive. Do not close just because you have an answer - a better answer or further insight may come along! Also do not quote entire responses in your own unnecessarily.
    • dwise1_aol disagrees : with closing the thread. The "solution" you reach could be wrong, like in a recent thread where the "solution was malloc'ing a single-char buffer for string input. If he had closed that thread, that would never have been corrected.

IMN logo majestic logo threadwatch logo seochat tools logo