Solved

Error: Bad Ptr

Posted on 2011-02-24
3
471 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 33

Expert Comment

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

Sara
0
 
LVL 33

Expert Comment

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

Sara
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

856 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