Programming and principles ch 7 exercise doubt

Question (Calculator program):
Modify Token_stream::get() to return Token(print) when it sees a newline. This implies looking for whitespace characters and treating newline ('\n') specially. You might find the standard library function isspace(ch), which returns true if ch is a whitespace character, useful.
Background : To complete expression, user has to type in 1 +1; // semicolon
This exercise wants us to modify the program that 1 +1 <enter> // and not semicolon
prints the output.

So I used a cin.get(ch) so that cin takes in white space.But, I only want it to take in newlines and ignore other white space. And, how is isspace() useful when it checks for all whitespace and not just newline?
So how do I do it? Tried reading cin.get() documentation with no success
Basically, get() has to return print if it finds a newline, at the same time ignore other whitespaces
Just give me a clue :p
Thanks
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
40
41
42
43
44
45
46
47
48
49
  Token Token_stream::get(){
	if(full){ 
		full = false;
		return buffer; // buffer never gets deleted!
	}

	char ch;
	cin >> ch;
	
	switch(ch){
		case print : 
		case '=' :
		case '(' : 
		case ')' :
		case '{' :
		case '}' :
		case '+' :
		case '-' :
		case '*' :
		case '/' :
		case '%' :
		return Token{ch};
		case '.' : case '0' : case '1' : case '2' : case '3' : case '4' : 
		case '5' : case '6' : case '7' : case '8' : case '9' :
		{
			cin.putback(ch);
			double val;
			cin >> val;
			return Token{number, val};
		}
		default : 
		if(isalpha(ch) || ch == '_'){
			string s;
			s+= ch;
			while(cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_'))
				s+=ch;
			cin.putback(ch);
                        //start keywords
			if(s == declkey) return Token (let);
			if(s == conskey) return Token (cons);
			if(s == helpkey) return Token (help);
			if(s == quitkey) return Token (quit);
                        //end keywords
			return Token{name, s}; // returns a name with type name
		}
		error("Invalid Token");
	}
}
Last edited on
Should I submit more details?
Well it was rather easy, i feel silly now
1
2
3
4
5
6
7
8
9
char ch;
	cin.get(ch);
	
	while(isspace(ch)){
		if(ch == '\n')
			return Token{print};
		else 
			cin.get(ch);
	}
Topic archived. No new replies allowed.