I'm trying to figure out a way I can change this program so that I could have a semi-colon ; instead of \n to indicate the end of a command, like this:

a ; b
The command is: a
The command is: b
a
The command is: a

How can I do this?

/* Read a command, skipping over leading blanks and any trailing
 * characters.
*/

#include <stdio.h>
#define FLUSH while(getchar() != '\n');
#define DEBUG

/* Declare functions */
int skipBlanks(void);
int skipOverRestOfCommand(void);

int main()
{
  /* Variable declarations */
  int cmd;

        /* Input is one character  */
        cmd = skipBlanks();
        /* While cmd is not equal to end of file */
        while (cmd != EOF)
        {
                /* Print output result */
                printf("The command is: %c\n", cmd);
                skipOverRestOfCommand();

                /* Update loop */
                cmd = skipBlanks();

        }
} /* End main */

/* Function has no parameters */
int skipBlanks(void)
{
  /* Variable declarations */
  int c;

        /* Return character from standard input */
        c = getchar();
        #ifdef FLUSH    
        /* While c is equal to one character */
        while (c == ' ')
        {
                /* Return char from standard input */
                c = getchar();

        }

        #endif

        #ifdef DEBUG
        printf("debug: input = '%c'\n", c);
        #endif

        /* Return c */
        return c;
}

/* Function has no parameteres */
int skipOverRestOfCommand(void)
{
  /* Variable declarations */
  int c;

        /* Return char as int */
        c = getchar();

        /* While c is a new line */
        while (c != '\n')
        {
                /* Return char as int */
                c = getchar();
        }

        /* Return c */
        return c;
}

Recommended Answers

All 10 Replies

I don't think I understand the problem.

Are you looking for a '\n' right now? Where?
Do you want to use a ';' instead?
Isn't this fairly obvious?

yes, I want ; to be considered a newline character aside from \n
if a user were to enter:
a ; b
I want the output to be:
The command is: a
The command is: b

is it possible to do this?

Yes it is. Engage brain and think. How can you look for a ';' instead of '\n'? As I asked before

Are you looking for a '\n' right now? Where?

If you can find that spot, think of the change necessary.

I know that I need to edit somewhere in the skipBlanks function.
But I just can't seem to think straight anymore. I was thinking of declaring both \n and ; as characters. then setting them to equal each other. Is that good enough?

I know that I need to edit somewhere in the skipBlanks function.
But I just can't seem to think straight anymore. I was thinking of declaring both \n and ; as characters. then setting them to equal each other. Is that good enough?

HINT: Make a change in line 70
ANOTHER HINT: If I have a number in a variable x, how do I check if the number is not equal to 2 or 3

if(x!=2 && x!=3)

HINT: Make a change in line 70
ANOTHER HINT: If I have a number in a variable x, how do I check if the number is not equal to 2 or 3

if(x!=2 && x!=3)

thank you very much. I really appreciate it. It works now, but I have one more question. If I were to enter this at input:
; b
I want to get this at output:
Error: missing command.
The command is: b

So I added a ';' to my IS_WHITE_SPACE macro, on line 11.
The problem was partially solved, but at output, I am only getting:
Error: missing command.
It's not reading the new line after ;
How can I fix this?

/* Read a command, skipping over leading blanks and any trailing
 * characters.
*/

#include <stdio.h>
/* #define DEBUG
*/
#define FLUSH while(getchar() != '\n');
#define IS_LOWER(c) ((c) >= 97 && (c) <= 122)
#define IS_UPPER(c) ((c) >= 65 && (c) <= 90)
#define IS_WHITE_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == ';')

/* Declare functions */
int skipBlanks(void);
int skipOverRestOfCommand(void);

int main()
{
  /* Variable declarations */
  int cmd;

        /* Input is one character  */
        cmd = skipBlanks();
        /* While cmd is not equal to end of file */
        while (cmd != EOF)
        {
                /* If input is lower case or uppercase */
                if (IS_LOWER(cmd) || IS_UPPER(cmd))
                {
                /* Print output result */
                printf("The command is: %c\n", cmd);
                }

                /* If input is not a letter */
                else
                {
                        /* If there is white space in the command */
                        if (IS_WHITE_SPACE(cmd))
                        {
                         printf("Error:  missing command.\n");
                        }
                        /* If input is not a letter or white space */
                        else
                        {
                        printf("Error: %c is not a letter.\n", cmd);
                        }
                }
                /* Call function to skip after command */
                skipOverRestOfCommand();

                /* Update loop */
                cmd = skipBlanks();

        }
} /* End main */

/* Function has no parameters */
int skipBlanks(void)
{
  /* Variable declarations */
  int c;
  char colon;
  char newline;
/* 
        colon = ';'; 
        newline = '\n';

        colon = newline; 
*/
        /* Return character from standard input */
        c = getchar();
        #ifdef FLUSH    
        /* While c is equal to one character */
        while (c == ' ')
        {
                /* Return char from standard input */
                c = getchar();


        }

        #endif

        #ifdef DEBUG
        printf("debug: input = '%c'\n", c);
        #endif

        /* Return c */
        return c;
}

/* Function has no parameters */
/* Gives blank like after print output */
int skipOverRestOfCommand(void)
{
  /* Variable declarations */
  int c;

        /* Return char as int */
        c = getchar();
        #ifdef FLUSH    
        /* While c is a new line */
        while (c != '\n' && c != ';')
        {
                /* Return char as int */
                c = getchar();
        }
        #endif
        /* Return c */
        return c;
}

the reason is because your different functions individually parse each character without regard to what the other is doing. Specifically, your "SkipOverRestOfCommand" function disposes of the ';' character without properly accounting for it to the "SkipBlanks" function.

in the even someone enters a ';' before any commands, your "SkipBlanks" function finds the ';', sees that it is not a space character, and returns.

then you go into "SkipOverRestOfCommand" function, and the semicolon found in the last function is not accounted for, but instead you just blaze on through and look at the next character. this next character is either a blank or a command, but this function is only looking for a '\n' or a ';', and therefore the entire next command is ignored.

try entering "a;b;c", then enter ";b;c" and you'll see what i mean.

i would never have written a string parser this way, so it's difficult for me to "fix" it without giving into the urge to significantly rewrite it.

.

sure enough, i can't give into the urge:

#include <stdio.h>
#include <string.h>

#define MAX_COMMAND_LEN		32


int main(void)
{
	char input[MAX_COMMAND_LEN+2], *iptr;
	size_t len;
		
	do {
		printf("input commands (max %d chars) : ", MAX_COMMAND_LEN);
		fflush(stdout);
	
		fgets(input,MAX_COMMAND_LEN+2,stdin);   // +2 allows /newline and NULL
		len = strlen(input);
		if (input[len-1] == '\n')				// get rid of /newline
			input[len-1] = '\0';
		len = strlen(input);
	} 
	while (len < 1 || len > MAX_COMMAND_LEN);   // force user input to MAX_COMMAND_LEN

	iptr = strtok(input,";");					// parse tokens
	while (iptr != NULL)
	{
		
		while(*iptr<0x41 || *iptr>0x5E && *iptr<0x61 || *iptr>0x7E)	// skip leading non-alpha
			iptr++;
		
		printf("command : %c\n",*iptr);
		iptr = strtok(NULL,";");
	}
	
	return 0;
	
}

thank you so much.
but I haven't learned about fgets or many of those other code you used, so I can't use it...
what exactly did you do to fix the problem?

thank you so much.
but I haven't learned about fgets or many of those other code you used, so I can't use it...
what exactly did you do to fix the problem?

well... i fixed it by completely rewriting it and using strtok to separate the string into tokens based on the semicolon delimiter. but no, i guess it doesnt help you much if i do your homework for you using functions you haven't even learned, yet.

the problem with your code is this: the SkipOverRestOfComand *assumes* that a semicolon has not yet been found. this assumption breaks down if SkipBlank actually finds an out-of-place semicolon and passes control to SkipOverRestOfCommand -- because SkipOverRestOfCommand is only looking for a '\n' or a ';' -- which will continue to drop all characters (ie, the next command) until it finally finds a semicolon or newline. And therefore that entire next command is ignored.

so in the case where you find an out-of-place semicolon (or a non-alpha character), you need to run "SkipBlanks" again, before you move onto SKipOverRestOfCommand.

you can accomplish this very easily with two lines of code. at the end of your "else" statement, add a line to recall SkipBlanks function, followed by a "continue" statement that works with your "while" loop. if you don't yet know about "continue" , you need to learn it, because it's flow control that's a fundamental part of conditional statements like "while", "do/while", and "for"


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.