Like Tree1Likes

simple error check

This is a discussion on simple error check within the C Programming forums, part of the General Programming Boards category; so if my string is NULL or empty I want the following errors to print. It's not working. What am ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    simple error check

    so if my string is NULL or empty I want the following errors to print. It's not working. What am I doing wrong.
    Thank you!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    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 newstr[SIZE];
    char reverse[SIZE];
    
     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);
          }
    
    
    
    if(strcmp(command, "list")==0) {
         printString(string);
           }
    
    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 == NULL){
    printf("No string entered"); //ERROR CHECK #2
    }
    else{
     printf( "\n\nHere is the text you entered:\n%s\n", string);
    }
    
    
    }
    
    void reverseString(const char *string, char *reverse)
    {
    
      int len =0, i, j =0;
      if(string == NULL){
        printf("Null string"); //ERROR CHECK #3
      }
    
    
      while(string[len])
        len++;
      for(i=len-1;i>=0;i--)
        {
          reverse[j]=string[i];
          j++;
        }
      reverse[j] = '\0';
      printf("%s", reverse);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    210
    even if I do something like
    Code:
    if(*string == '\0') {
    printf("No string");
    
    OR
    
    
    if (string[0] == '\0') {
    it doesn't work
    Im putting all of this in the reverse function

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    I understand that if you type new and then rev its not going to work because the program stores the enter sign which I would love if someone could just tellme how to tell it not to.
    but when you run the program and the first command you type is rev or list, then something weird is written like ?:& if I use any of the if statements that say its an empty string.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Your code needs to be indented properly and you need to include what your defines are.

    You are missing the definition of *p (can be put at the start of that small scope) and flag (I'm not sure what scope you wanted for that)
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    210
    Sorry I posted it in haste

    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);
        }
    
    if(strcmp(command, "list")==0) {
          printString(string);
        }
    
    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 == NULL){
        printf("No string entered"); //ERROR CHECK #2
      }
      else{
        printf( "\n\nHere is the text you entered:\n%s\n", string);
      }
    }
    
    void reverseString(const char *string, char *reverse)
    {
    int len =0, i, j =0;
    if(string == NULL){
        printf("Null string"); //ERROR CHECK #3
      }
    
    while(string[len])
        len++;
    for(i=len-1;i>=0;i--)
        {
     reverse[j]=string[i];
          j++;
        }
      reverse[j] = '\0';
      printf("%s", reverse);
    }
    Last edited by kiwi101; 10-30-2012 at 09:54 PM.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    What I mean when I say "indent your code" is every time you use a curly brace, indent your code.

    Indentation
    Code:
    int main(void)
    {
        ....
        if (banana != delicious)
        {
            if (apple != delicious)
            {
                if (pear != delicious)
                {
                    you = fussy;
                }
            }
        }
    
    
        return 0;
    }
    You will get a lot more people responding if your code looks neat

    I'm having trouble understanding what you want to do - Are you looking to do an "if" - "else if" - "else if" - "else" where the last "else" is the error?
    Do you want to not check if the string input was unsuccessful?
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    210
    well if I remove the sentences where I have put error check the program runs perfectly. I have already checked it.
    So if the user writes something other than new, list or reverse, I want the program to say "Invalid command". But right now even if I type something in the new string "Invalid command" still pops up.
    Also if the string is empty and the user types in list or rev I want another error to pop up i.e. error 2 and 3
    I made a correction to the code i just wrote on top int flag =1.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    The "if" statements for "list" and "rev" need to be "else if"
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    210
    So you mean like this
    it doesn't really make sense
    Code:
    void printString (const char *string)
    {
       if(string == NULL){
         printf("No string entered"); //ERROR CHECK #2
                                  }
          else if {
             printf( "\n\nHere is the text you entered:\n%s\n", string);
                   }
    }

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    210
    I mean I know that statement is logically incorrect but what more could I add at the else if part?

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    210
    and is the string == NULL part correct?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    It should just be an else, not an else if, unless you have some other condition to check.

    As for indentation, it should be:
    Code:
    void printString(const char *string)
    {
        if (string == NULL)
        {
            printf("No string entered"); //ERROR CHECK #2
        }
        else
        {
            printf( "\n\nHere is the text you entered:\n%s\n", string);
        }
    }
    or:
    Code:
    void printString(const char *string)
    {
        if (string == NULL) {
            printf("No string entered"); //ERROR CHECK #2
        } else {
            printf( "\n\nHere is the text you entered:\n%s\n", string);
        }
    }
    or minor variations thereof, as long as you are consistent.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    210
    This is exactly what I posted and when I deleted what I had and wrote your code for my printstring I still get the same thing printed this is what I get
    Code:
    Here is the text you entered:
    tĚ.N=÷
    WHY?

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    210
    maybe it's the string == NULL part but even if I write (strlen(string) = '/0') i still get the same thing

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    That is because string will not be a null pointer since string in the main function is an array.

    What you should do is initialise string in the main function to be an empty string:
    Code:
    char string[SIZE] = "";
    Then your printString function checks that the pointer is not a null pointer and that the string is not an empty string before printing it:
    Code:
    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");
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21