Solved

Error: Bad Ptr

Posted on 2011-02-24
3
472 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
[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
  • 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 34

Expert Comment

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

Sara
0
 
LVL 34

Expert Comment

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

Sara
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 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 goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
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.

730 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