edited
edited
Last edited by cannsyl; 12-05-2008 at 08:55 PM. Reason: too much code
Ok so too much code.
Simple question
which of my headers is deprecated or antiquated?
and what is STK_ERROR'?'Code:#include <iostream> #include <fstream> #include <cstdlib> // for exit(..) #include <cstdio> #include "stack.h" #define STK_ERROR '?' #include "stack.h" /* Private data: */ #define MAX 50
and how does stk_top work?
Thanks
Well, you fixed cstdio.h (which just plain doesn't exist) and got back to cstdio, so that's good. What do you mean "how to use stk_top"? I would type the symbols "s", "t", "k", "_", "t", "o", and "p" in order. It gives you the top of the stack, i.e., the symbol that will come off the stack next.
First I would like to thank tabstop for the very helpful reply. We haven't gone over this in class yet and you were able to clear it right up. I never would have thought toIf someone else could help with my next question?type the symbols "s", "t", "k", "_", "t", "o", and "p" in order.
I was wondering if anyone sees a problem with this block...I have been looking so long I don't see anything and dev is telling me I am missing a ; somewhere.
thanks
Code:else if (c == ')') { if( c == ')' && stk_top() == '(' ) { stk_pop(); } else if ( c == ')' && stk_top() == '\n' ) { stk_pop(); } else { stk_push(c); // pop stack, if empty have missing ( }
Sorry
I am just in a crunch and have been working on this for a while...with little instruction in class.
Here is my question now...Can you read in a .txt file in C the way you do in C++?
and I need to process all expressions in the input file, echo the expression,
and correctly report one of the following for each expression:
• parentheses match
• missing )
• missing (
I think I am to the point whereI need to echo the expression but where should I cout the .cpp or the main? do you cout in C?
and thank you for your help.Code:#include <iostream> #include <fstream> #include <cstdlib> // for exit(..) #include <cstdio> #include "stack.h" int main() { char c; int k; //initialize stk_init(); c = getchar(); putchar(c); // loop through file one char at a time while (c!=EOF) { // process one character // push (, pop if ), // check stack empty if \n, // ignore others // if error, move to next line if (c == '(') { stk_push(c); } else if (c == ')') { if( c == ')' && stk_top() == '(' ) { stk_pop(); } else if ( c == ')' && stk_top() == '\n' ) { stk_pop(); } else { stk_push(c); } // pop stack, if empty have missing ( } else if (c == '\n') { if ( stk_top() == ')' ) { stk_pop(); } // check on missing ) // set up for next line stk_init(); } if (stk_error()) //skip rest of line { stk_init(); } else // get next char { } } // endwhile return 0; }
You printf in C. The f stands for formatted, which means you have to provide a format string (what to print and how to print it). Man printf will get you perhaps too much information.
I have no idea what "the .cpp or the main" means. Since this is C you don't have a .cpp anyway. I would print it out as/directly after you read it in.
how do you read in a txt file into a c program?
I have
How would I use printf?Code:int main() { char c; int k; //initialize stk_init(); c = getchar(); putchar(c); when I put printf here I get an error // loop through file one char at a time while (c!=EOF) { // process one character // push (, pop if ), // check stack empty if \n, // ignore others // if error, move to next line when I put printf (c); here I get an error if (c == '(') { stk_push(c); } else if (c == ')') { if( c == ')' && stk_top() == '(' ) { stk_pop(); } else if ( c == ')' && stk_top() == '\n' ) { stk_pop(); } else { stk_push(c); } // pop stack, if empty have missing ( } else if (c == '\n') { if ( stk_top() == ')' ) { stk_pop(); } // check on missing ) // set up for next line stk_init(); } if (stk_error()) //skip rest of line { stk_init(); } else // get next char { } } // endwhile return 0; }
I am in a c++ class and I don't know why we are doing c...
I have to read in a txt file, match the parentheses, echo each character, and then say whether it matches or not.
I don't know what I am doing?
That's why you type "man printf" to find out more about printf (or look it up in the back of the book, etc.) Again, printf requires a format string that says what it is printing and how to print it. So to print a character you would do
In C, you open a file with fopen and close it with fclose and can read in from it with fgetc.Code:printf("%c", character_variable_to_be_printed);
So if I have
why doesn't it print anything when it runs? It compiles fine but runs a blank screen.Code:int main() { char c; int k; FILE *fp; fp=fopen("input.txt", "r"); int fgetc (FILE *fp); //initialize stk_init(); c = getchar(); putchar(c); // loop through file one char at a time while (c!=EOF) { // process one character // push (, pop if ), // check stack empty if \n, // ignore others // if error, move to next line printf("%c", c); if (c == '(')
Given that you've managed to make several dozen function calls correctly, writing this
makes no sense. Did you specify a return type when you called fopen? stk_init? getchar? putchar? printf? stk_pop? stk_push? Why do so here? Did you specify the types of the parameters when you called getchar? putchar? printf? etc.? Why do so here?Code:int fgetc (FILE *fp);
I don't know...I was following the template I was given...Like I said this was a c++ class and this is the only C I have ever done. I have no idea why we were thrown this curve at the end of the semester.
So I just needand that should read in one character at a time.Code:int fgetc ();
and shouldn'tread out one char at a time?Code:if (c == '(') { stk_push(c); printf("%c", c); }
I still get a blank running screen
So, are you aware of functions in general, and how they work? I have a feeling that your instructor thinks the answer is "no", hence the assignment, where you are literally given every function you could ever need, if only you knew how to call them. If I tell you that there is a function called "fgetc", which requires a parameter that is a FILE*, and returns an int that is the next character in the file pointed to by the parameter, could you use that function?
I honestly don't think so.
Actively cross-posting, eh? Bad form.