C++ input problem...

Status
Not open for further replies.

ringobob

Ars Praefectus
4,785
Subscriptor
This is sad... I'm finishing up my CS minor and I still can't do this crap (this is why I dropped it from my major... wasn't learning a damn thing).

Anyway...

Trying to write a scanner/parser for a calculator language in C++. Here's the scanner that I'm going to use (not the one I wrote, but it's more correct to the calculator language)

code:<HR>
Code:
  // COSC 325 Project 1
// C++ version by S. Dean 9/19/2001
// apparently the cin stops at blanks, so this is untested for bypassing them 9/19/2001<P>#include "iostream.h"
#include "string.h"<P>void main()
{ char s[100];
  int i; 
cout << "Enter a string"<< endl;
cin >> s;
cout << "The string was " << s << endl;
i = 0;
while (s[i] != 0)
{ 
  // bypass any blanks
  while (s[i] == ' ') i++; <P>  // recognize operator tokens
  if ((s[i] == '+') || (s[i] == '-') 
	  || (s[i] == '*') || (s[i] == '/')) {
	  cout << s[i] << endl;
	  i++; }
  else 
  // recognize the beginning of an identifier
  if (((s[i] >= 'a') && (s[i]<= 'z'))	  
	  || ((s[i] >= 'A') && (s[i] <= 'Z'))) {
		cout << s[i];
		i++;
		// and "accumulate" the rest of it
		while (((s[i] >= 'a') && (s[i]<= 'z'))	  
				|| ((s[i] >= 'A') && (s[i] <= 'Z'))
				|| ((s[i] >= '0') && (s[i] <= '9'))) {
			cout << s[i];
			i++;}
		cout << endl; // and then do the linefeed
		}
  else
  // recognize the beginning of a literal (numeric constant)
  if ((s[i] >= '0') && (s[i]<= '9')) {
		cout << s[i];
		i++;
		// and "accumulate" the rest of it
		while ((s[i] >= '0') && (s[i]<= '9')) {	  
			cout << s[i];
			i++;}
		cout << endl; // and then do the linefeed
		}
  else
  // recognize the assignment operator
  if ((s[i] == ':') && (s[i+1] == '=')) {
	  cout << ":="<< endl ;
	  i++; i++;}
  else
  // recognize the $$
  if ((s[i] == '$') && (s[i+1] == '$')) {
	  cout << "$$" << endl;
	  i++; i++;}
  else 
  // recognize left parenthesis 
  if (s[i] == '(') {
	  cout << '(' << endl;
	  i++;}
  else
  // recognize right parenthesis (no checking to be sure they match)
  if (s[i] == ')') {
	  cout << ')' << endl;
	  i++;}
  else {
	  // invalid character 
	  cout << "Illegal character:" << s[i] << endl;
	  i++; }
}<P>}

Right up at the top there, it says the problem. cin stops at blanks. What should I use to bypass this? Or, conversly, should I just require that the input be without blanks? Might be easier, but I would like to have more functionality.

At any rate, thanks in advance for any help/suggestions.
 
Actually, you'll probably want to use the other getline along with std::string.
code:<HR>
Code:
#include <iostream>
#include <string>
using namespace std;<P>void foo()
{
    string aLine;
    getline(cin, aLine);   // I think that's it anyway
}
That way you don't have length limitations.

Edit: Removed a :p because of stupid OpenTopic formatting.

[This message was edited by euzeka on November 28, 2001 at 12:58.]
 
You could write your own version of cin using getch();
the only advantage would be to confuse your proffesor, and give yourselve more control on how the input is parsed. For absolute control, thier is a inkey() function floating around somewhere.

code:<HR>
Code:
#include "conio.h"
#include "iostream.h"<P>void GetInputLine(char *ptr_return);<P>int main(void)
{
    char Array[100];
    GetInputLine(&Array[0]);
    cout << "\n" << Array << endl;   <P>    return 0;
}<P>void GetInputLine(char *ptr_return) //be sure to reset your pointer!
{
   char CharIn = '~'; //it doesn't matter what charecter we choose as
                      //long as it isn't a exit charecter
   while (CharIn != '\n' && CharIn != '\0' && CharIn != 13)
   {
       CharIn = getch();  //grab the char from the  keyboard buffer
       *ptr_return = CharIn; //send it to the ptr_return
       ptr_return++; //move ptr_return pointer to next address/index
   }
   --*ptr_return = '\0'; //add our null char to end of string
   //we are finished 
}

that should work .. perhaps I should check for bugs
//two bugs ; and II should be &&
//forgot to check for carriage return (duh), fixed

[This message was edited by Fblue on December 03, 2001 at 15:59.]

[This message was edited by Fblue on December 03, 2001 at 16:00.]

[This message was edited by Fblue on December 03, 2001 at 16:07.]
 

Screndib

Ars Tribunus Militum
2,038
Originally posted by yugami:
if he uses getline he's going to want to cin.ignore('\n') so that \n doesn't get stuck in the buffer

getline removes the '\n' from the buffer (or at least it should).

getline with std::string in VC++6 or below is :( . It doesn't work properly, though it does in VS.NET. There are two fixes that I've found for it, write your own getline function or go here and apply the fixes to the library files. The only problem with the latter is that you have to then make the change on every compiler that has the problem.
 
Status
Not open for further replies.