Thread: Why does this happen?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    Why does this happen?

    Hi. When I run the following program....... and enter in for example 12345

    why does it print out 2345. Where is 1 gone?

    Code:
    int main ()
    {        
    
     int c;        
    c = getchar();
    while(c !=EOF) {
    
    c = getchar();
    printf("%c", c);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You read it before the loop, but never printed it out. As such, you should swap the order of the two statements in the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by laserlight View Post
    You read it before the loop, but never printed it out. As such, you should swap the order of the two statements in the loop.
    Ah I see. But why does having c=getchar() before print make 1 disappear?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by metros View Post
    Ah I see. But why does having c=getchar() before print make 1 disappear?
    Step 1: getchar reads 1
    Step 2: getchar reads 2
    Step 3: print 2
    Step 4: getchar reads 3
    Step 5: print 3
    ...
    Step x: getchar reads eof
    Step x+1: print eof

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by metros View Post
    Ah I see. But why does having c=getchar() before print make 1 disappear?
    If you pour out the contents of your coffee cup and replace them with Pepsi, would you expect the coffee cup to still contain coffee?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    I changed it a bit to this.........


    Code:
    line[i] = getchar();
    while(line[i] != EOF) {
    line[i] = getchar();
    
    }
    
    for(i = 0; i < strlen(line);  i++) {
    
          printf("%c",line[i]);
    }
    However I get the following output.......

    Dۅ��r���5���2���ۅ�

    after putting in ddfdf

    What went wrong here?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    ultimately what I want to do is take each input form the command line and put them into their own variable. At the moment I'm playing around with printf to see what is actually contained in the array.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    What you had
    Quote Originally Posted by metros
    Code:
    int main ()
    {        
    
     int c;        
    c = getchar();
    while(c !=EOF) {
    
    c = getchar();
    printf("%c", c);
    }
    was almost entirely correct. The hint
    Quote Originally Posted by laserlight
    you should swap the order of the two statements in the loop.
    is referring to the two bold lines.

    The first "getchar" before the while loop, of course, gets a char. Then you go in the while loop and overwrite it by immediately getting another char. What you want to do is get a char, enter the while loop, print the char, get another char, and loop. This is what the hint above is saying to do.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The problem you had above with the "printf" is that you're trying to print a "char[]", but it doesnt have a null terminating character ('\0'). When you pass a string to "printf", it will print all of the contents of the string, up until (and excluding) the null terminating character. If the string does not contain the null terminating character, it keeps printing until either it finds one or it segfaults. If the string isnt null terminated, then just printf a character ("%c"), or add the terminating character.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by metros View Post
    I changed it a bit to this.........


    Code:
    line[i] = getchar();
    while(line[i] != EOF) {
    line[i] = getchar();
    
    }
    
    for(i = 0; i < strlen(line);  i++) {
    
          printf("%c",line[i]);
    }
    However I get the following output.......

    Dۅ��r���5���2���ۅ�

    after putting in ddfdf

    What went wrong here?
    Do you initialize i to a proper value? And you don't increment it after reading a character. And, as nadroj said, you have to 0-terminate the string as well.
    Also, watch out that i doesn't become bigger than the size of the buffer, as the buffer will overflow otherwise.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    The best way to read the characters and printing in the stdout is the following.I did small changes in your code as follows.

    Code:
    int main ()
    {        
    int c;        
    while((c=getchar()) !=EOF) //reading the character from user and checks whether it isn't EOF
    printf("%c", c); //prints the character if it isn't EOF.Else,come out from the loop
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vivekraj
    The best way to read the characters and printing in the stdout is the following.I did small changes in your code as follows.
    Your example is fundamentally the same as metros' first posted attempt, once the two statements that I mentioned are swapped.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. magic could happen on recvfrom
    By -EquinoX- in forum C Programming
    Replies: 22
    Last Post: 03-27-2009, 06:06 PM
  2. Is what I think is gonna happen gonna happen
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 10:34 PM
  3. Hey, what would happen if...
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-05-2002, 08:54 AM
  4. What would happen?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-04-2002, 10:54 AM
  5. Why Does This Happen?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-28-2001, 11:55 AM