ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.