Thread: simple error check

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    
        if(strcmp(command ,"new")==0)
        {
            getString(string);
        }
        else if(strcmp(command, "list")==0)
        {
            printString(string);
        }
        else if(strcmp(command, "rev")==0)
        {
            reverseString(string, reverse);
        }
        else    //if any command entered besides these 3
        {
            printf("Not valid command"); //ERROR CHECK #1
        }
    Fact - Beethoven wrote his first symphony in C

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    210
    I mentioned this above but could you also tell me what is wrong if I print this in my main function. So if anything besides new, list or rev is written as a command an error is printed.
    Code:
    else {  //if any command entered besides these 3
     printf("Not valid command"); //ERROR CHECK #1
    }
    

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That looks okay to me. If there is an error, then it is likely that it will be fixed by Click_here's suggestion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Can you show your newest code with good Indentation?
    Fact - Beethoven wrote his first symphony in C

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    210
    Oh I didn't see Click_Here's reply thank you to the both of you. here is my attempt at indenting the code properly
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 200 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10/*the maximum size of a command*/
    
    
    void getString( char* );
    void printString (const char *);
    void reverseString (const char *str, char *reverse);
    
    
    int main(void)
    {
    char command[MAX_CMD_LEN];
    char string[SIZE] = "";
    char *p;
    char newstr[SIZE];
    char reverse[SIZE];
    int flag=1;
     
     do {
        printf("cmd> ");
    
    if(fgets(command,  MAX_CMD_LEN, stdin) != NULL){
          if((p = strchr( command, '\n')) != NULL)
                *p = '\0';
                                                                              }
    
    if(strcmp(command ,"new")==0) {
          getString(string);
                                                   }
    
    
    else if(strcmp(command, "list")==0) {
          printString(string);
                                                        }
    
    else if(strcmp(command, "rev")==0) {
          reverseString(string, reverse);
                                                         }
    else {  //if any command entered besides these 3
             printf("Not valid command"); //ERROR CHECK #1
         }
    
    
    }
    
    while (flag==1);
    
    return 0;
    }
    
    
    void getString(char *string)
    {
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    void printString(const char *string)
    {
      if (string && string[0])
        {
          printf("\n\nHere is the text you entered:\n%s\n", string);
        }
      else
        {
          printf("No string entered");
        }
    }
    
    void reverseString(const char *string, char *reverse)
    {
    int len =0, i, j =0;
    if (string && string[0])
        {
      while(string[len])
        len++;
      for(i=len-1;i>=0;i--)
        {
          reverse[j]=string[i];
          j++;
        }
      reverse[j] = '\0';
      printf("%s", reverse);
    }
      else  {
        printf("No string entered");
      }
    }

    So if the user writes new and then just presses enter. I would want the program to consider this also empty. How do I make the program ignore the enter sign. I know this is a simple loop i just kind of forgot it.

    Thanks guys!

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kiwi101
    How do I make the program ignore the enter sign. I know this is a simple loop i just kind of forgot it.
    You are already doing this with your use of strchr after the fgets call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    210
    But thats only so everytime I press enter cmd> comes up
    if I write "new" press enter then write "list" I don't get invalied command because the program reads the enter sign. But if I run the program and write "list" then I get invalid command.
    so should I do another strchr after every command?

  8. #23
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    here is my attempt at indenting the code properly...
    A good sense of humour is not lost on me

    Here is a link - Indent style - Wikipedia, the free encyclopedia

    I'm surprised that laserlight responded without saying anything!
    Fact - Beethoven wrote his first symphony in C

  9. #24
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    if I write "new" press enter then write "list" I don't get invalied command because the program reads the enter sign. But if I run the program and write "list" then I get invalid command.
    Your description doesn't match the program's behaviour. If I run the code from post #20 and follow your instructions I get the following output (my input in green):
    Code:
    $ ./test
    cmd> new<ENTER>
    Please enter your stringlist
    cmd> ^C
    $ ./test
    cmd> list
    No string enteredcmd> ^C
    In the first case when I enter "new" + <ENTER> I get the prompt to enter a new string. After entering "list" the program prompts me for a new command. Why do you want to get an "invalid command" error message in this situation?
    In the second case I don't get "invalid command" but "No string entered".

    Could you please describe more accurately your problems.

    Bye, Andreas

  10. #25
    Registered User
    Join Date
    Nov 2012
    Posts
    1
    hey im not sure if this is the right forum. I'm writing a program that simulates the game of Nim basically it goes between user and computer and each have a choice to take away a number from a set counter this happens until one is forced to pick the last number. my problem is with error checking against letter input i already error checked against wrong number input
    Code:
    ..
    
     
    		{
    			 printf("\n\nYour turn\n");
    			
    
    
    		
    		do										// do while loop for foreign number inputs
    
    		 { 
    				
    				
    				
    				printf("\n\n please make a selection between 1 and 3 to take away from counter\n\n");
    				scanf_s("%d",&take);
    				
    
    				
    				system("cls");
    
    	     }while ((take < 1) || (take > 3));
    		 
    				printf("\n\n you picked %d to be taken away from the counter\n\n", take);
    				counters = counters - take;
    			    printf("\n There is %d counters left",counters);
    
    
    		
    				if (counters == 1)										// win conditions 
    				{
    					printf("\n\nyou win\n\n");
    		
    					fp = fopen(fname,"a");
    					time_t now;
    					now= time((time_t*)NULL);							// save win time 
    					fprintf(fp,"\nuser wins at %s\n",ctime(&now));
    					system("pause");
    					break;
    				}
    		}

  11. #26
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by filipsons11 View Post
    hey im not sure if this is the right forum.
    This is the right forum, but you should start a new thread rather than highjacking someone else's thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple check.
    By Mike Beal in forum C++ Programming
    Replies: 23
    Last Post: 07-23-2012, 12:49 AM
  2. Simple check/deposit exercise in C
    By edishuman in forum C Programming
    Replies: 10
    Last Post: 09-15-2011, 10:49 AM
  3. Simple check program
    By Shadow in forum C Programming
    Replies: 3
    Last Post: 06-05-2002, 06:20 PM