Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,931 people online right now. Registration is fast and FREE... Join Now!




Segmentation Fault?

 
Reply to this topicStart new topic

Segmentation Fault?

nazzan1
15 Dec, 2007 - 12:28 AM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 5


My Contributions
Hey there! I'm wrapping up an intro to C course in college and have spent hours trying to resolve a seg-fault error I've been getting. Not trying to waste anybody's time (whatever the problem is will probably be obvious to an expert) and I'll promptly answer any questions you've got.
Grateful for any tips!

My program's supposed to read a text file that contains equations involving integers and the +, -, *, and / operators that are written in postscript notation (i.e. "3 4 +" is the same as "4 + 3"; "3 6 8 + *" is the same as "3(8+6)"), and solve them by making use of stack-manipulation functions (that I have defined in the included stack.h header file and a stack.c source file--I'm very confident that this is not the source of problems because I have used these stack manipulation functions successfully in previous programs). My program is called post.c. It makes calls to fgets to get lines in the form of strings from input.txt (my text file that contains the postscript equations), and then uses strtok (as is required for the exercise) to parse the strings. I have used strcmp to allow the program to pick out the operators (+,-,*,/) and everything that is not an operator (i.e. everything that is an integer) is pushed onto the stack via my push (push(stackptr s, double x)) function. Items are then popped off and processed according to the operators read.

Here's the source code:

CODE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stack.h"

int analyze(char *c);

int main(int argc, char * argv[]){
  
  FILE *inp;
  stack s = newStack();
  char line[100];
  char *fname = argv[1];
  
  inp = fopen(fname, "r");
  
  if(inp == NULL){
    fprintf(stderr, "Error: unable to read from file\n");
    exit (8);
  }

  char chars[100];

  while(fgets(chars, sizeof(line), inp) != NULL){
    printf("\n");
    char *strptr;
    strptr = strtok(chars, " ");
    
    while(strptr != NULL){
      strptr = strtok(NULL, " ");
       while(atoi(strptr) != 0){
     push(s, (double)(atoi(strptr)));
     strptr = strtok(NULL, " ");  
       }
      
       if(analyze(strptr) == 1){
     double x = 0;
     x = (pop(s))+(pop(s));
     push(s, x);
     /* strptr = strtok(NULL, " "); */  
     printf("%f ", top(s));
    
     }
       else if(analyze(strptr) == 2){
     double d = 0;
     double e;
     double f;
     d = (pop(s));
     e = (pop(s));
     f = e - d;
     push(s, f);
     printf("%f ", top(s));
    
       }
       else if(analyze(strptr) == 3){
     double z = 0;
     z = (pop(s))*(pop(s));
     push(s, z);
     printf("%f ", top(s));
    
       }
       else if(analyze(strptr) == 4){
     double a = 0;
     a = (pop(s))/(pop(s));
     push(s, a);
     printf("%f ", top(s));
       }
       /* strptr = strtok(NULL, " "); */  
              
       /*  printf("%f\n", top(s)); */
  
    }
    
  }


     /*  printf("%f, ", top(s)); */

  

  
        

  
  fclose(inp);
  
  printf("\n");
  return 0;
}

int analyze(char *c){

  if(strcmp(c, "+") == 0)
    return 1;
  else if(strcmp(c, "-") == 0)
    return 2;
  else if(strcmp(c, "*") == 0)
    return 3;
  else if(strcmp(c, "/") == 0)
    return 4;
  else
    return 0;

}



it seems to me that the placement of strptr = strtok(NULL, " "); in the while(strptr != NULL){ loop is what makes all the difference. As you can see it used to be near the end of the loop (where it is commented out). With it in that former position, the program would run, but the last operation in each line of input would not be performed (i.e. if the line were "1 3 4 5 + + +", the program output would be "9 12" when it should have been "9 12 13") and no matter how much I racked my brain I could not figure out why this was happening, so I moved it to the beginning of the while loop, and now I get a segfault error when trying to run the program. I sincerely hope that the solution to all of this is painfully obvious. It's clear to me that i won't make progress if I stare at my screen any longer, I need some fresh eyes. Sorry for being so longwinded with this. Just wanted to get everything in. Thanks!

This post has been edited by nazzan1: 15 Dec, 2007 - 04:05 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:43PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month