So I tried all the tips and im still getting a seg error, only this time its at the end of the code.
I've put a comment after the last output i see b4 the seg fault.
Here's the updated code
Code:
;
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAXLINE 132
#define EXP '^'
#define LEFT_BRAKET '('
#define RIGHT_BRAKET ')'
#define DEVIDE '/'
#define MULT '*'
#define ADD '+'
#define SUBTRACT '-'
char ch[MAXLINE];
int main(int argc, char *argv[]){
int i = 0;
int kill_the_SF = 0;
int length, lengthb, length_counter = 1;
char test_string[MAXLINE];
char *token;
char text_stringvtwo[MAXLINE];
char text_stringthree[MAXLINE];
int result = 0;
if (argc <3){
printf("no Argument time to toke!\n");
printf("welcome to the very basic C calculator\n Please enter an equation:");
fgets(ch,MAXLINE,stdin);
printf("Input = %s\n", ch);
fflush(stdin);
strcpy(test_string,ch);
printf("Copy = %s\n", test_string);
fflush(stdin);
lengthb = strlen(test_string);
token = strtok(test_string, " ");
printf("+++++++++++++++++++++++++++++++++token = [ %s ] @ %d++++++++++++++++\n\n\n", token, i);
fflush(stdin);
strcpy(text_stringvtwo, token);
i++;
if (token!=NULL){
printf("Im NOT NULL\n");
fflush(stdin);
length = strlen(token);
length_counter =+ length+1;
}
else
length_counter = 0;
printf("\nlength_counter = %d",length_counter);
i =1;
do{
token = strtok(NULL, " ");
printf("In while loop\n");
fflush(stdin);//at this point I get a seg Fault
if (token != NULL ){
printf("im in another while loop\n");
fflush(stdin);
strcpy(text_stringvtwo, token);
if (strcmp(token, "\\n") == 0){
printf("it is equil to \\n");
fflush(stdin);
strncpy(text_stringvtwo, token, length-1);
}
printf("\n++++++++++++++++++++token =[ %s ] @ %d++++++++++++++++++\n\n\n", token, i);
fflush(stdin);
printf("lengths:\nCurrent token:\t%d\nCurrent String:\t%d\nOriginal String:\t%d\n",length ,strlen(text_stringvtwo) ,lengthb);
printf("Length counter =\t%d\nIndex =\t%d\n", length_counter, i);
printf("Values:\n\tToken val:\t%s\n",token);
}
else
break;
}while (token != NULL);
token = NULL;
printf("i made it out!");
}
else{
printf ("condition has been met");
}
while ( strcmp(text_stringvtwo[i], NULL ) ){//I know its the same, im working on the above 1st, code doesn't make it here anyways
printf("I'm in a while loop");
if (strlen(token) == 1){
switch (token[0]) {
case EXP:
printf("this program does not support exponential equations");
163,1-8 73%
break;
case LEFT_BRAKET:
printf("left b");
break;
case RIGHT_BRAKET:
printf("right b");
break;
case DEVIDE:
printf("devistion");
break;
case MULT:
printf("multiplication");
break;
case ADD:
printf("addition");
break;
case SUBTRACT:
printf("subtract");
break;
default:
printf("default");
break;
}
}
i++;
}
printf("I mane it out");
}
201,1 Bot