LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-26-2003, 03:24 AM   #1
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
while loop not looping as intended


This has me pretty baffled and I would like to know if I baffle easy.

Code:
#include <stdio.h>

int main()
{
        int nl, c, t = 0;
        nl = 0;
        while (t <= 10) {
                c = getchar();
                ++t;
                if (c == '\t')
                        ++nl;
        }
        printf("%d\n", nl);
        return 0;
}
This doesnt loop while 't' is less than or equal to 10, but it varies in results. Whats the reason?

Thanks, Tarts.

Last edited by Tarts; 11-26-2003 at 03:26 AM.
 
Old 11-26-2003, 03:52 AM   #2
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
It does loop, but you must enter a character to continue. The value being stored in c is of type int and not a character. So your out put will be different.

Last edited by cxel91a; 11-26-2003 at 04:01 AM.
 
Old 11-26-2003, 04:30 AM   #3
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by cxel91a
It does loop, but you must enter a character to continue. The value being stored in c is of type int and not a character. So your out put will be different.
It does loop, but when I output the value of 't', it seems has it looped once before already...

Code:
[Mirrus][stratus][04:28:53][/Source][$] /Exec/new1
a
The value of 't' is 0
The value of 't' is 1
a
The value of 't' is 2
The value of 't' is 3
a
The value of 't' is 4
The value of 't' is 5
a
The value of 't' is 6
The value of 't' is 7
a
The value of 't' is 8
The value of 't' is 9
a
The value of 't' is 10
0
[Mirrus][stratus][04:29:01][/Source][$]
It guess it just reads once, loops, skips the call to 'getchar()' , loops again and reads again. I understand...sure I do.

Last edited by Tarts; 11-26-2003 at 05:18 AM.
 
Old 11-26-2003, 06:39 AM   #4
jimveta
Member
 
Registered: Oct 2003
Location: los angeles
Posts: 32

Rep: Reputation: 15
you're right, because getchar() reads *all* the bytes in a stream, including a newline and that's why you see it done twice.

so for example, if you typed "1234567890" then enter, that will finish the loop off in one line.
 
Old 11-26-2003, 06:42 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Each character you enter, followed by a return = 2 characters
 
Old 11-26-2003, 07:14 AM   #6
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
I see! Thanks!
 
Old 11-26-2003, 09:45 AM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Avoid scanf() - for a lot of reasons. Use fgets() instead.
 
Old 11-28-2003, 02:50 AM   #8
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by jim mcnamara
Avoid scanf() - for a lot of reasons. Use fgets() instead.
I do, and if I ever make a program of relevence I wouldnt use it, I would still like to fix its problems though, or write a similar(in some ways) function that doesnt store a newline.

Tarts
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can linux firewall traffic not necessarily intended for it (promisc mode)? tisource Linux - Security 2 11-16-2004 07:19 PM
Konqueror ALWAYS opens Juk rather than intended folder hooded Linux - Software 3 09-30-2004 05:25 PM
Symbolic link in /etc/alternatives not working as intended davidas Debian 2 04-10-2004 07:09 PM
Linux is COMPLETELY ROOTED (no pun intended) split_banana Linux - General 6 08-17-2003 01:59 PM
Not intended to flame but this is funny bsm2003 General 4 03-24-2003 01:48 PM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration