3 Replies - 464 Views - Last Post: 11 April 2010 - 10:13 AM Rate Topic: -----

#1 Guest_Chad Springer*


Reputation:

Why won't input file return what I need it to return.

Posted 10 April 2010 - 09:24 PM

I need to make separate header files to add into the main file to make an RPN Calculator. This is the file to take in what the user enters and determine what it is and send it back to main through the return. I have tried for many hours trying to get it to work, but for some reason it won't return what is entered. I would like some hints as to why it won't work, please. And maybe some help to correct it.



/* Get an RPN expression from the keyboard.
 * Accepts q as indictation that user wants to quit. 
 * Skips over invalid characters.
 * Normal end of expression is '='.  
 */

#include <stdio.h>
#include <ctype.h>
#include "inputter.h"
#include "stack.h"

char* get_next_token(void)
{
    char next_ch[1000];
    int i=0;
    char* n;
    do
    {
        *next_ch = getchar();

        if (tolower((int)*next_ch) == 'q')
        {
            // User wants to quit.
            break;
        }

        if ((*next_ch == '\n'))
        {
            break;
        }

       if (isdigit(*next_ch) ||
            (*next_ch == '+') ||
            (*next_ch == '-') ||
            (*next_ch == '*') ||
            (*next_ch == '/') ||
            (*next_ch == '=')    )
        {
            printf("entered is %s\n", next_ch);
	    ++*next_ch;
            i++;
        }

        if(tolower((int)*next_ch) == 'c')
	{
	    clear_stack();
        }

        if(tolower((int)*next_ch) == 'p')
	{
	    pop();
        }

        if(tolower((int)*next_ch) == 't')
	{
	    int m =(int)*next_ch * -1;
            *next_ch = m;
            ++*next_ch;
            i++;
        }

//        if ((*next_ch == '+') && (*next_ch == 'p') &&
//            (*next_ch == '-') && (*next_ch == 't') &&
//            (*next_ch == '*') && (*next_ch == 'c') &&
//            (*next_ch == '/') && (*next_ch == '=') &&
//            (isdigit(*next_ch))                       )
//        {
//            continue;
//        }
//	else
//      {
//	    printf("Wrong input\n");
//            *n='q';
//            break;
//      }
        n=next_ch;

    } while (*next_ch!= ' ');

    //n = 0;     // Provide null terminator.

    return n;
}





If you could please email me what you think I should do to xxxx@yahoo.com.

This post has been edited by Martyn.Rae: 10 April 2010 - 09:28 PM
Reason for edit:: Emial removed


Is This A Good Question/Topic? 0

Replies To: Why won't input file return what I need it to return.

#2 Guest_Guest*


Reputation:

Re: Why won't input file return what I need it to return.

Posted 10 April 2010 - 09:29 PM

These are the other parts of the program if you want to see what this program is trying to do.
This is the code to calculate:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"
#include "calculate.h"


static void do_plus()
{
	double op2 = pop();
	double op1 = pop();
	push (op1 + op2);
}

static void do_minus()
{
	double op2 = pop();
	double op1 = pop();
	push (op1 - op2);
}

static void do_multiply()
{
	double op2 = pop();
	double op1 = pop();
	push (op1 * op2);
}

static void do_divide()
{
	double op2 = pop();
	double op1 = pop();
	push (op1 / op2);
}

static void do_equal()
{
        double op1 = pop();
        printf("equals %f\n", op1);
        push(op1);
}


static void do_operation(const char* ch)
{
	//printf ("do_operation called for %c\n", ch);
	switch(*ch)
	{
	case '+':	do_plus(); break;
	case '-':	do_minus(); break;
	case '*':	do_multiply(); break;
	case '/':	do_divide(); break;
        case '=':       do_equal();break;
	}
}



void calculate(const char* token)
{
    if(isdigit((int)*token))
    {
        double operand=0.0;
        sscanf(token,"%lg",&operand);
        push(operand);
    }
    else
    {
        do_operation(token);
    }
}




This is the code for stack:
#include <assert.h>
#include "stack.h"

static char stack[STACK_SIZE]; 

static int stack_top = 0;

void clear_stack (void)
{
	stack_top = 0;
}

double stack_is_empty(void)
{
	return (stack_top == 0);
}

double stack_is_full (void)
{
	return (stack_top == STACK_SIZE);
}

void push(char c)
{
	assert(stack_top < STACK_SIZE);
	stack[stack_top++] = c;
}

char pop(void)
{
	assert(stack_top > 0);
	return stack[--stack_top];
}




And this is the code for the main file:
#include "defines.h"

#ifdef RPN

#include <stdio.h>
#include "inputter.h"
#include "calculate.h"

int main(void)
{
    char* next_token="";
    printf("This is the RPN calculator\n"); 

    next_token=get_next_token();
    while(*next_token!='q')
    {
        printf("entered is %s\n", next_token);
//        calculate(next_token);
        next_token=get_next_token();
    }
    printf("Bye\n");
    getchar();
    return 0;
}


#endif



The reason why the main is inside an ifdef endif is because it is for testing purposes with a seperate test file in the define.h header file.

Thank you for your help
Was This Post Helpful? 0

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why won't input file return what I need it to return.

Posted 11 April 2010 - 12:24 AM

View PostMartyn.Rae, on 11 April 2010 - 12:32 AM, said:

You are returning n but n is never assigned anything.

:?: (Not sure why Martyn.Rae said that.)


A few problems are apparent. First, in main:
int main(void)
{
    char* next_token="";
    printf("This is the RPN calculator\n"); 

    next_token=get_next_token();
// ...

Note that you are assigning the pointer next_token to a variable that is local to get_next_token(), so it is out of scope as soon as that function finishes, i.e. the value that next_token points to can vanish at any moment. To fix this:
in main: declare next_token as a char, not a char*, and
in get_next_token(): return the value of the token instead of a pointer (and change the return type of the function accordingly).


Next problem, here:
#include <stdio.h>
#include <ctype.h>
#include "inputter.h"
#include "stack.h"

char* get_next_token(void)
{
// ...

It looks like this file is inputter.h, so you shouldn't have #include "inputter.h" at the top of it.

Next:
char* get_next_token(void)
{
    char next_ch[1000];

I already mentioned above that you should change the return type of this function. Also, why are you declaring next_ch to be a 1000-element array when it is supposed to get only 1 token at a time?

Next:
    do
    {
        *next_ch = getchar();

        if (tolower((int)*next_ch) == 'q')
        {
            // User wants to quit.
            break;
        }

        if ((*next_ch == '\n'))
        {
            break;
        }


Immediately after *next_ch = getchar(); you should put an extra getchar(); to retrieve and discard the newline character, and remove the block beginning with if ((*next_ch == '\n')). Also, there is no reason to cast *next_ch to an int in the call to tolower(). (You perform this unnecessary casting in a number of other places as well.)


Next:
       if (isdigit(*next_ch) ||
            (*next_ch == '+') ||
            (*next_ch == '-') ||
            (*next_ch == '*') ||
            (*next_ch == '/') ||
            (*next_ch == '=')    )
        {
            printf("entered is %s\n", next_ch);
            ++*next_ch;
            i++;

here, if an operator is entered, you are discarding it instead of returning it. Why?


I also noticed that at the beginning of the file that seems to be calculate.h, you have (erroneously) #include "calculate.h".


edit: Most of my comments above are based on the assumption that you want to retrieve just 1 char at a time since that's what you seem to be doing. If, on the other hand, you want to be able to retrieve several chars at once (e.g. strings of digits), then the return type should be neither a char nor a char*. I would suggest that you make it return an int, representing the number of chars of input, AND you should create a char array in main, AND pass that array to get_next_token and store the user's input in it. In other words, in that case the function signature should look like this:
int get_next_token( char* token )

This post has been edited by r.stiltskin: 11 April 2010 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Guest*


Reputation:

Re: Why won't input file return what I need it to return.

Posted 11 April 2010 - 10:13 AM

Thanks. That makes sense when I look at it now. I will give it try later today. But those were the .c files the .h files were seperate and I didn't include them here.
Was This Post Helpful? 0

Page 1 of 1