while(cin) does'nt react to keyboard input

Hello everyone,
I am currently working with Stroustrup's Book "Programming Principles & Practice Using CPP". At the moment, I am at Chapter 6, "Writing a program".
The main task is to write a calculator using tokens, among others.
I'm using the code below. I can compile it, but when I run the programm, I can't basically do anything. I expected, because of while (cin), that I can give as much input by the keyboard as possible, but if I start the programm, it does'nt react to my input.

https://s32.postimg.org/m93oeecn9/cpp_prob.png

After a certain time (approx. one or two minutes) an abortion message pops up.

If I add, for instance, something like "cout << "b"; in the code, it just prints out "b" in a infinite circle, which makes sense to me.

So actually, I am not sure whether it's because of my code or of my computer/compiler. I am using VisualStudios2015 on Win7_32, x86.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <fstream>




using namespace std;

class Token {
public:
	char kind; // what kind of token
	double value; // for numbers: a value
};

Token get_token()
{
	return Token();
} // i added "return Token" by myself, as the compiler gave out an error before

vector<Token> tok; // we’ll put the tokens here

int main()
{
		while (cin) {
			Token t = get_token();
			tok.push_back(t);
		}
for (int i = 0; i<tok.size(); ++i) { //multiplication
	if (tok[i].kind == '*') {
		double d = tok[i - 1].value*tok[i + 1].value;
	}
}

return 0;
}


Thanks in advance,
hrxs1
Last edited on
Firstly :
1
2
3
4
5
for (int i = 0; i<tok.size(); ++i) { //multiplication
if (tok[i].kind == '*'  && (i + 1) < tok.size()) {
		double d = tok[i - 1].value*tok[i + 1].value;
	}
}


Without it, your program will simply cause a buffer overflow though...
Last edited on
What code was given in the book ?

Your code seems to have quite some problems.
1
2
3
4
Token get_token()
{
	return Token();
}

So you're just returning a new empty Token here ?

while (cin) {
cin is converted to bool, it is true if the stream is in a good state, which it generally is. But since you aren't extracting anything from stdin nor are you modifying the stream in your program which sends your program into an infinite loop.
You probably wanted to do something like this
while (cin >> your_variable) {
That way, the program will keep on extracting stuff until and error or End Of File is reached. EOF (and thus exit your program) can be generated using Ctrl+D in unix-like systems and Ctrl+Z in Windows.
Look at this :
1
2
3
4
while (cin) {
		Token t = get_token();
		tok.push_back(t);
	}


And :
1
2
3
4
5
Token get_token()
{
    // Where is the input??
	return Token();
}


You haven't added a feature that let the program input data for each Token.

So :
1
2
3
4
5
6
7
8
Token get_token()
{
     Token token;
    cout << "Input token symbol : "; cin >> token.kind;
    cout << "Input token value : "; cin >> token.value; cout << endl;

	return token;
}


And :
1
2
3
4
for (int i = 0; i < 5; i++) {
		Token t = get_token();
		tok.push_back(t);
	}

Also :
while (cin) {...}

If only happens to be false if there is some error occured when you input something. But you haven't input anything at all! Thus, the while condition while(cin) is always true causing the program to loop forever plus your vector continuously add new elements and fill your memory very quickly. That's why you see your program stop responding for a quick while. When there is no more memory left, the program simply crashes.
Thanks to both of you, closed account & a k n.

Your replies completely make sense to me. I have the code from the book, actually even copied & pasted it (but changed the get.token(); declaration to my empty function) and I was wondering, where the input was; maybe one should add a statement to the function by self.

But one question closed account, if I don't input anything, how can the vector get filled?

Last edited on
> But one question, if I don't input anything, how can the vector get filled?

1
2
3
4
while (cin) {
		Token t = get_token();
		tok.push_back(t);
	}


Alright, assume your code is still original. The function get_token() is also involved too, so I will show it as well.
1
2
3
4
Token get_token()
{
	return Token();
}


There is absolutely no input function. There is absolutely nothing to pause the program. Everything is done in an instant. And the while(cin) loops forever. Each time a new loop is executed, you call get_token() to input (or whatever) initialize the token so that the token is guaranteed to be valid and it will be added to the vector. But get_token() only contains a single line of code return Token(); which just returns an empty (unused) Token object, it can't be valid since an unused Token contains just garbage values. And your vector adds the invalid Token in its list. The current loop ends and immediately a new cycle begins, and so on. If you are interested, try this :
1
2
3
4
5
6
while (cin) {
		Token t = get_token();
		tok.push_back(t);
        cout << "Blah - Blah ";
        cout << "(tok.size() == " << tok.size() << ")" << endl;
	}

Does it help you? :)
Yes :-) Thousand Thanks! Topic can be considered as solved.
Glad to hear :)
Topic archived. No new replies allowed.