LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-28-2003, 10:14 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
What's wrong with case 0?


In chapter 6 of <<The c++ programming language>>, Stroustrup said: " By default >> skips whitespaces (that is, spaces, tabs, newlines, etc.) and leave the value of ch unchanged if the input operation failed. Consequently, ch==0 indicates end of input.
So, I write the program:
Code:
#include<iostream>
#include<istream>
#include<string>
#include<map>

using namespace std;

enum Token_value {
   END, PRINT=';'
};
Token_value curr_tok=PRINT;

Token_value get_token(); //the form of the function.

int main()
{
   while (cin) {
   get_token(); //call the funcion get_token() within the funcion int main().
   if (curr_tok==END) break;
   if (curr_tok==PRINT) continue;
   }
}

Token_value get_token() //define the funcion.
{
   char ch=0;
   cin>>ch;
   switch (ch) {
   case 0:
      return curr_tok=END;
   case ';':
      return curr_tok=Token_value (ch);
   default:
      return curr_tok=PRINT; 
   }
}
But, when I run the porgram, I can't exit it, even if I type spaces, tabs, etc.
What is wrong?
Thank you.
 
Old 08-29-2003, 12:12 AM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
0 as in null should represent end of input and not '0' as in character which has an ASCII value of 48... if you replace the line case 0 with case '0', this would work just fine...
 
Old 08-29-2003, 01:29 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I guess the statement
Code:
case (constant-expression) statement
means if the program can find something accord with case 0,
then it will invoke
Code:
case 0:
      return curr_tok=END;
and implement the clause:
Code:
if (curr_tok==END) break;
Then the user can exit the program.

when I type spaces, tabs, etc, I guess they accord with case 0, but why the program don't do so, I exit the program?

Thank you.
 
Old 08-29-2003, 06:54 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: What's wrong with case 0?

Quote:
Originally posted by Xiangbuilder

Stroustrup said:" By default >> skips whitespaces (that is, spaces,
tabs, newlines, etc.) and leave the value of ch unchanged if the input
operation failed. Consequently, ch==0 indicates end of input.

[..snip..]

But, when I run the porgram, I can't exit it, even if I type spaces,
tabs, etc. What is wrong?
There's nothing wrong at all. The book says that when >> reads
whitespaces, it will be skipped. i.e. it will immediately read the next
character without ever returning a whitesoace character. This
does not mean that entering whitespace will end the input. So
entering whitespace will not cause cin to return 0, and will not
have your program exit.

Your program will exit when the "cin >>" reaches the end of
input
. In other words, it will return 0 when it reads an
EOF (End Of File) character.

You can send a EOF character through the keyboard by hitting
CTRL-d, like you would do when creating a file with:

cat >myfile.txt

(in MS-DOS sending an EOF was done by hitting CTR-z)
 
Old 08-29-2003, 09:13 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you teach me many things.

I guess in the clause
Code:
   case 0:
      return curr_tok=END;
"return curr_tok=END;"
will never be invoked.

Last edited by Xiangbuilder; 08-29-2003 at 09:17 AM.
 
Old 08-29-2003, 09:34 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Sure it does!
When you hit CTRL-d to send an EOF character...

Try it. Insert a command that prints something:
Code:
    case 0:
        cout << "Boo!" << endl;
        return curr_tok=END;
Compile, run, type some text, hit ENTER.
Finally hit CTRL-d and "Boo!" will be printed!

P.S.
Be sure to use "case 0:" or "case '\0':"
Not "case '0': ".

Last edited by Hko; 08-29-2003 at 09:38 AM.
 
Old 08-29-2003, 11:05 PM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
I tried to use Ctrl-d in some of my programs,
I feel it is magic.
 
Old 08-30-2003, 07:26 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You're welcome.
 
  


Reply



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
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 08:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
Case Iain Wilson Linux - Newbie 1 03-16-2004 11:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration