?
Solved

How do I modify a C++ class to display which variable is uninitialized?

Posted on 2010-11-13
8
Medium Priority
?
413 Views
Last Modified: 2012-05-10
How do I modify a C++ class to display which variable is unitialized?  My program accepts an expression and checks, among other things, that all variables are initialized.  Currently, it throws an error when there is an unitialized variable, but how to I get it to display that variable that is uninitialzed?

Example:

I enter: (x + y), x=2;

It throws an exception and displays:
Error: Uninitialzed variable.

I want it to display which variable is uninitialized:

Error: Uninitialized variable y
main.cpp
symboltable.cpp
variable.cpp
subexpression.cpp
0
Comment
Question by:NSing9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34128525
Make your exception have a string constructor
Then do
	if (symbolTable.lookUp(name) == -1)		//
		throw UninitializedException(name);		// terminate function

Open in new window


You didn't post see the code for UninitializedException, but I think you can figure it out from there.
0
 

Author Comment

by:NSing9
ID: 34128622
I'm not sure how to do that.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34128676
Let me see the UninitializedException code.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:NSing9
ID: 34128737
See attached.
UninitializedException.h
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1000 total points
ID: 34128793
Try this.
#include <stdexcept> // stdexcept header file contains runtime_error
#include <string>
using std::runtime_error; // standard C++ library class runtime_error
using std::string

class UninitializedException : public runtime_error
{
public:
	// constructor specifies default error message
	UninitializedException::UninitializedException()
		: runtime_error( "Uninitialized Variable" ) {}
	UninitializedException::UninitializedException(string name)
		: runtime_error( "Uninitialized Variable: " + name ) {}

}; // end class UninitializedException

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34128797
Actually, the line probably needs to be

: runtime_error( ("Uninitialized Variable: " + name).c_str() ) {}

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34128804
If name isn't a std::string type you'll need to adjust of course.
0
 

Author Closing Comment

by:NSing9
ID: 34128875
Thank you!  It works after modifying the constructor.  Although I don't know C++ well, some of the basic programming concepts have escaped me after being away from it for so long.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 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