Access Reading Violation

I'm having a mostly semantic problem with my calculator program.

The input string is read with cin into a character array, and the terms and operators are read straight from the array.
Following Order of operations, I've written a function that iterates through the input and looks for '^'. Then it passes the location of the '^' character and the input array to a DoExp() function. The DoExp() function has to read the characters (which are numbers 0-9) directly preceding and succeeding the '^' character. The problem occurs when the term directly preceding the '^' character happens to be the first term in the input array. (301^4 as opposed to 2+4^2).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define isnum int(input[counter])<58&&int(input[counter])>47

char* input=new char[50];
cin.getline(input,50,'\n');
int counter=0;
for(;input[counter];counter++){
      if(input[counter]=='^'){
            DoExp(input,counter);
            counter=-1;
            continue;
      }
}

void DoExp(char* input,int counter){
      counter--;
      for(;isnum;counter--){}
      //here,input[counter] should be equal to the first character of the term directly preceding '^'.
      ...
      Irrelevant Code Here
      ... 
}
 


The last for loop is to place the counter at the first digit in the number preceding '^'. The problem occurs when the array looks like "301^4". In that instance, when isnum is tested the last time, it is testing input[-1] which is obviously a problem.

Any ideas on how to handle this problem? I don't need a piece of code to copy+paste into my program-I want to do this myself-but I would appreciate some suggestions.
Thanks for reading,
You code is very messy, it's seems that you started coding without thinking about the problem.

Simply put, you are trying to write parser.
Split input text into tokens containing numbers, then look for + ^ etc. and then calculate.

PS. also after line 15 you can check
if ( &input[counter] < input ) break;
;]
Last edited on
Topic archived. No new replies allowed.