R
Rich Strang
Hi group I am just starting out in C so please go easy on me
I have started to write a small command line interpreter, I have got the
program to break down the commands successfully until I introduce a loop
into the main function. Without the loop the program works fine with the
loop the cout doesn't seem to work, I know I am probably being real stupid
but I would really appreciate some help. Please find the code below:
Thanks Guys
Rich
// **** Rich's Command Line Interpreter ****
// Author: Rich Strang
// Date: 08/10/03
#include <stdio.h>
#include <iostream.h>
#include <ctype.h>
//Set Global
const int iDim1Size=10;
const int iDim2Size=80;
//Function Prototypes:
int GetLine(char[]);
int ParseLine(char[], char[][iDim2Size], int);
int PrintTokens(char[][iDim2Size]);
int main(void)
{
char cInput[iDim2Size];
int iInputSize=0;
char cTokens[iDim1Size][iDim2Size];
int iExit=0;
while(iExit==0)
{
// Prints prompt to screen
printf("$ ");
// Retrieve user input
iInputSize = GetLine(cInput);
if(cInput[0] != 'e' && cInput[1] != 'x' && cInput[2] != 'i' && cInput[3]
!= 't' )
{
// Split user input into tokens
ParseLine(cInput, cTokens, iInputSize);
//Print tokens to screen
PrintTokens(cTokens);
}
else
{
iExit=1;
}
}
return 0;
}
//GetLine Function
int GetLine(char cInput[])
{
int iInputSize=0;
char ch;
for( iInputSize = 0; (iInputSize < 80) && ((ch = getchar()) != EOF) && (ch
!= '\n'); iInputSize++ )
{
cInput[iInputSize] = (char)ch;
}
return iInputSize;
}
//ParseLine Function
int ParseLine(char cInput[], char cTokens[][iDim2Size], int iInputSize)
{
int iIndex=0;
int iA1=0;
int iA2=0;
for( iIndex = 0; (iIndex < iInputSize); iIndex++ )
{
if(cInput[iIndex] != ' ' && cInput[iIndex] != '\t')
{
cTokens[iA1][iA2] = cInput[iIndex];
iA2++;
}
else
{
iA1++;
iA2=0;
}
}
return 0;
}
//Print Tokens Function
int PrintTokens(char cTokens[][iDim2Size])
{
int iIndex1=0;
int iIndex2=0;
for( iIndex1 = 0; (iIndex1 < iDim1Size); iIndex1++ )
{
for( iIndex2 = 0; (iIndex2 < iDim2Size); iIndex2++ )
{
if ( isalpha(cTokens[iIndex1][iIndex2]) ||
isdigit(cTokens[iIndex1][iIndex2]))
{
cout << cTokens[iIndex1][iIndex2];
}
}
cout << "\n";
}
return 0;
}
I have started to write a small command line interpreter, I have got the
program to break down the commands successfully until I introduce a loop
into the main function. Without the loop the program works fine with the
loop the cout doesn't seem to work, I know I am probably being real stupid
but I would really appreciate some help. Please find the code below:
Thanks Guys
Rich
// **** Rich's Command Line Interpreter ****
// Author: Rich Strang
// Date: 08/10/03
#include <stdio.h>
#include <iostream.h>
#include <ctype.h>
//Set Global
const int iDim1Size=10;
const int iDim2Size=80;
//Function Prototypes:
int GetLine(char[]);
int ParseLine(char[], char[][iDim2Size], int);
int PrintTokens(char[][iDim2Size]);
int main(void)
{
char cInput[iDim2Size];
int iInputSize=0;
char cTokens[iDim1Size][iDim2Size];
int iExit=0;
while(iExit==0)
{
// Prints prompt to screen
printf("$ ");
// Retrieve user input
iInputSize = GetLine(cInput);
if(cInput[0] != 'e' && cInput[1] != 'x' && cInput[2] != 'i' && cInput[3]
!= 't' )
{
// Split user input into tokens
ParseLine(cInput, cTokens, iInputSize);
//Print tokens to screen
PrintTokens(cTokens);
}
else
{
iExit=1;
}
}
return 0;
}
//GetLine Function
int GetLine(char cInput[])
{
int iInputSize=0;
char ch;
for( iInputSize = 0; (iInputSize < 80) && ((ch = getchar()) != EOF) && (ch
!= '\n'); iInputSize++ )
{
cInput[iInputSize] = (char)ch;
}
return iInputSize;
}
//ParseLine Function
int ParseLine(char cInput[], char cTokens[][iDim2Size], int iInputSize)
{
int iIndex=0;
int iA1=0;
int iA2=0;
for( iIndex = 0; (iIndex < iInputSize); iIndex++ )
{
if(cInput[iIndex] != ' ' && cInput[iIndex] != '\t')
{
cTokens[iA1][iA2] = cInput[iIndex];
iA2++;
}
else
{
iA1++;
iA2=0;
}
}
return 0;
}
//Print Tokens Function
int PrintTokens(char cTokens[][iDim2Size])
{
int iIndex1=0;
int iIndex2=0;
for( iIndex1 = 0; (iIndex1 < iDim1Size); iIndex1++ )
{
for( iIndex2 = 0; (iIndex2 < iDim2Size); iIndex2++ )
{
if ( isalpha(cTokens[iIndex1][iIndex2]) ||
isdigit(cTokens[iIndex1][iIndex2]))
{
cout << cTokens[iIndex1][iIndex2];
}
}
cout << "\n";
}
return 0;
}