Solved

How do I check for a missing character and throw an exception if the character is not in the string?

Posted on 2010-11-14
12
357 Views
Last Modified: 2012-05-10
How do I check for a missing character and throw an exception if the character is not in the string?  I have a program where i enter an expression and I want it to display and error if the end of the expression is missing a ';'

ex. (x+2), x=2

This should throw an exception when I click enter because it is missing the ';' at the end of the expression. ie. (x+2), x=2;

I have tried to account for the missing ';' in a for loop, such as:

if (cin.peek() != ';')      
{
     throw SyntaxException();
}

but, my exception is thrown even on valid expressions that have a ';' at the end.  I assume that it is because even on valid expressions that end with ';', once enter is clicked, the next character will never be ';' which is what peek() is looking for.
0
Comment
Question by:NSing9
  • 7
  • 5
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34131284
Well, I don't know how you are reading your inpur, but you could just check the last character in the input line, e.g.
#include <iostream>
#include <string>
using namespace std;

//...

string sLine;

getline(cin,sLine);

for (string::reverse_iterator ri = sLine.rbegin(); ri != sLine.rend(); ++ri)
{
  if (*ri == ' ') continue; // skip whitespace

  if (*ri != ';')  throw SyntaxException();
   else break; // found semicolon, all is fine
}

Open in new window

0
 

Author Comment

by:NSing9
ID: 34131327
I think this might work.  I am entering the input from the console and it is being stored as an expression.  I have to try to figure out how to convert the expression to a string?
0
 
LVL 86

Expert Comment

by:jkr
ID: 34131379
How are you reading the epresson?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:NSing9
ID: 34131442
cout << "Enter expression: ";

The expression is entered from the keyboard when this statement is run.  Below is the main function that I have:

int main()
{
    Expression* expression;
    char paren, comma;
      
    cout << "Enter expression: ";
    cin >> paren;
   
      try
      {
            if (cin.peek() == '(')  //mistmatch left paren exception
            {
                  throw SyntaxException();
            }

            expression = SubExpression::parse();
            
            if (cin.peek() == ')')       //mistmatch right paren exception
            {
                  throw SyntaxException();
            }

            if (cin.peek() != ',')            //missing comma exception
            {
                  throw SyntaxException();
            }

            cin >> comma;  
            parseAssignments();
            double result = expression->evaluate();
            cout << "Value = " << result << endl;
      }
      catch (DivideByZeroException &divideByZeroException)
      {
            cout << "Error: " << divideByZeroException.what() << endl;
      }
      catch (UninitializedException &uninitializedException)
      {
            cout << "Error: " << uninitializedException.what() << endl;
      }
      catch (SyntaxException &syntaxException)
      {
            cout << "Error: " << syntaxException.what() << endl;
      }
    return 0;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 34131526
You could then just read a complete line and replace 'cin' by a stringstream, e.g.
#include <sstream>
using namepace std;

// ...

int main()
{
    Expression* expression;
    stringstream ss;
    string sLine;
    char paren, comma;
      
    cout << "Enter expression: ";
   
    ss >> paren;
   
      try
      {
            getline(cin,sLine);

            for (string::reverse_iterator ri = sLine.rbegin(); ri != sLine.rend(); ++ri)
            {
              if (*ri == ' ') continue; // skip whitespace

              if (*ri != ';')  throw SyntaxException();
               else break; // found semicolon, all is fine
            }

            ss.str(sLine.c_str());

            if (ss.peek() == '(')  //mistmatch left paren exception
            {
                  throw SyntaxException();
            }

            expression = SubExpression::parse();
            
            if (ss.peek() == ')')       //mistmatch right paren exception
            {
                  throw SyntaxException();
            }

            if (ss.peek() != ',')            //missing comma exception
            {
                  throw SyntaxException();
            }

            ss >> comma;  
            parseAssignments();
            double result = expression->evaluate();
            cout << "Value = " << result << endl;
      }
      catch (DivideByZeroException &divideByZeroException)
      {
            cout << "Error: " << divideByZeroException.what() << endl;
      }
      catch (UninitializedException &uninitializedException)
      {
            cout << "Error: " << uninitializedException.what() << endl;
      }
      catch (SyntaxException &syntaxException)
      {
            cout << "Error: " << syntaxException.what() << endl;
      }
    return 0;
}

Open in new window

0
 

Author Comment

by:NSing9
ID: 34131737
I get a fatal error when I try to modfy it this way:

fatal error LNK1104: cannot open file 'C:\Documents and Settings......
0
 

Author Comment

by:NSing9
ID: 34131797
My apologies.  I fixed the fatal error, but it appears that it throws the exception now when ';' is missing from the end of the expression, but it's not evaluating the expression and displaying the value as it did before when there is a correctly placed ';' at the end of the expression.
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 34132028
Oops, sorry, little correction:
#include <sstream>
using namepace std;

// ...

int main()
{
    Expression* expression;
    stringstream ss;
    string sLine;
    char paren, comma;
      
    cout << "Enter expression: ";
   
      try
      {
            getline(cin,sLine);

            for (string::reverse_iterator ri = sLine.rbegin(); ri != sLine.rend(); ++ri)
            {
              if (*ri == ' ') continue; // skip whitespace

              if (*ri != ';')  throw SyntaxException();
               else break; // found semicolon, all is fine
            }

            ss.str(sLine.c_str());

            ss >> paren; // should be here...

            if (ss.peek() == '(')  //mistmatch left paren exception
            {
                  throw SyntaxException();
            }

            expression = SubExpression::parse();
            
            if (ss.peek() == ')')       //mistmatch right paren exception
            {
                  throw SyntaxException();
            }

            if (ss.peek() != ',')            //missing comma exception
            {
                  throw SyntaxException();
            }

            ss >> comma;  
            parseAssignments();
            double result = expression->evaluate();
            cout << "Value = " << result << endl;
      }
      catch (DivideByZeroException &divideByZeroException)
      {
            cout << "Error: " << divideByZeroException.what() << endl;
      }
      catch (UninitializedException &uninitializedException)
      {
            cout << "Error: " << uninitializedException.what() << endl;
      }
      catch (SyntaxException &syntaxException)
      {
            cout << "Error: " << syntaxException.what() << endl;
      }
    return 0;
}

Open in new window

0
 

Author Comment

by:NSing9
ID: 34132133
I'm still having the same problem.  Does the fact that after I enter an expression such as:

(x+2), x=2;

I am immediately clicking <enter> after keying in the ';' at the end of the expression.  This is a valid expression and should display results but it does nothing after clicking <enter>.  It works correctly without making the modifications but then it doesn't throw the exception for the missing semicolon.
0
 
LVL 86

Expert Comment

by:jkr
ID: 34132250
That's weird, when I strip down the code to have a test, it seems to work. Can you step through the modified code to see where it stops working?
0
 

Author Comment

by:NSing9
ID: 34132340
There is another method that follows the main class that I didn't provide initially.  It is as follows:

void parseAssignments()
{
    char assignop, delimiter;
    string variable;
    double value;

    do
    {
        variable = parseName();
        cin >> ws >> assignop >> value >> delimiter;
            symbolTable.insert(variable, value);
    }
    while (delimiter == ',');
}

I'm not sure if or where this would affect the modifications, but I have tired it changed cin to ss and just leaving it as is, but I still get the same result.
0
 

Author Closing Comment

by:NSing9
ID: 34178903
Still couldn't get it to work.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell regex followup 13 75
base64 decode encode 12 134
designing in object programming 12 78
Bartender label printing - switch on and off graphics 3 39
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question