Solved

Error: Bad Ptr

Posted on 2011-02-24
3
468 Views
Last Modified: 2012-05-11
Hello Everyone,

I am writing a program that lets a user enter in a equation and then tell if the program encounters an operator.  What I am having a problem with is I am getting a Bad Ptr a run time error from the following pointer tokenPtr. When I debug the error is coming from this line

 if(*tokenPtr == '(' || *tokenPtr == ')' || *tokenPtr == '+' || *tokenPtr=='-')

Also,  the first variable  is in the equation is being skipped by the 'if' evaluation.  

(BTW: There is an expected space between each character)


 
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<iterator>
#include<vector>
#include<algorithm>

using namespace std;




int main()
{
	
   char equation[20];
   char* tokenPtr;

   cout<<"Please Enter In A Equation"<<endl;

   cin.getline(equation, '\n');
   
   tokenPtr = strtok(equation," ");

   while( tokenPtr != NULL)
   {
	   cout<<tokenPtr<<endl;
	   tokenPtr = strtok(NULL, " ");

	   if(*tokenPtr == '(' || *tokenPtr == ')' || *tokenPtr == '+' || *tokenPtr=='-')
	   {

		   cout<<"This Is A Operator"<<endl;
	   }
   }


	
	system("PAUSE");

	return 0;
}

Open in new window

0
Comment
Question by:brich744
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 34977363
You are calling strtok (the one inside the loop) but not checking the return value.

I'm not 100% sure of what your logic should be, but either check for a null ptr in the if statement, ie.

  if(tokenPtr != NULL && (*tokenPtr == '(' || *tokenPtr == ')' || *tokenPtr == '+' || *tokenPtr=='-'))

or move the strtok call on line 29 down below the if statement
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34977450
simply move the strtok call in line 29 to end of loop.

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34977455
mccarl, sorry for repeating.

Sara
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now