Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify Token_stream::get() to return Token(print) when it sees a newline. This i

ID: 3924513 • Letter: M

Question

Modify Token_stream::get() to return Token(print) when it sees a newline. This implies looking for whitespace characters and treating newline (' ') specially. You might find the standard library function isspace(ch), which returns true if ch is a whitespace character, useful.

This is what I have so far, but I can't seem to figure out the errors

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");
   }
}

Explanation / Answer

Please try below code. Let me know if your're facing further issue.

#include <iostream>

using namespace std;

int main()
{
cout << "Hello World" << endl;

return 0;
}

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) == 0 || 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");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote