M
merrittr
I need some C advice I want to read in string commands from a user
when the user enters a \n
I want to push it on the stac. Then at some point , if the user enters
the word print pop off and print each word (or using another stack
pointer scan the stack printing each string). here is a stub of what i
want to do. (how do I implement this currently my code doesn't work
due to my lack of strings and pointers)
#include <stdio.h>
#include <ctype.h>
#define CMDBUFFER 10
char iItem[10],*x;
int iSp;
void push(char*);
char pop(void) ;
void main()
{
int iCur,iCharCount=0,i1,i2,bExit=0;
char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";
enum eTokens {Exit=0,Add=1,Subtract=2,Multiply=3,Divide=4};
while(bExit==0)
{
cString[CMDBUFFER]=" ";
printf(":");
cVal=getchar();
while(cVal != '\n')
{
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++] ='\0';
iCharCount=0; //reset cString
push(&cString);
*x=pop();
printf ("main %s\n",x);
}
}
void push(char *p)
{
iItem [iSp++]=p;
}
char pop(void)
{
printf("%i\n",iSp);
char *x=iItem [--iSp];
printf ("pop %s\n",x);
return *x;
}
when the user enters a \n
I want to push it on the stac. Then at some point , if the user enters
the word print pop off and print each word (or using another stack
pointer scan the stack printing each string). here is a stub of what i
want to do. (how do I implement this currently my code doesn't work
due to my lack of strings and pointers)
#include <stdio.h>
#include <ctype.h>
#define CMDBUFFER 10
char iItem[10],*x;
int iSp;
void push(char*);
char pop(void) ;
void main()
{
int iCur,iCharCount=0,i1,i2,bExit=0;
char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";
enum eTokens {Exit=0,Add=1,Subtract=2,Multiply=3,Divide=4};
while(bExit==0)
{
cString[CMDBUFFER]=" ";
printf(":");
cVal=getchar();
while(cVal != '\n')
{
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++] ='\0';
iCharCount=0; //reset cString
push(&cString);
*x=pop();
printf ("main %s\n",x);
}
}
void push(char *p)
{
iItem [iSp++]=p;
}
char pop(void)
{
printf("%i\n",iSp);
char *x=iItem [--iSp];
printf ("pop %s\n",x);
return *x;
}