Thread: Complex user input checking

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    Complex user input checking

    I have a program that requires asking for user input on what to do. For example I have a function
    Code:
    add(int id, char name [8000],int dep)
    and the user must type 'add 1 "Name1" 20´' to run this function.
    I also some other methods like
    Code:
    duration(int dep)
    or
    Code:
    depend()
    .

    My first problem is in the add function, as you see in the example the name has to be between " ", if not the command is not valid, I'm having a really hard time figuring out how to check this. The other functions, duration and depend are easier, you only have to type 'duration 2' for example, and 'depend'.

    I'm having problems on which scanning funtion is the best, is it scanf, fgets or getChar?

    I have this from a previous work using fgets but I don't know if it works well in this particular situation:

    Code:
    char input[90], col [6], na [80];
        unsigned long a, b;
        
        while(strcmp(input[0], "exit") != 0){
          if(fgets(input, sizeof(input), stdin)){
              if(input[0] == 'p' && input[1] == '\n' && input[2] == '\0'){
                list(matrix);
            }
            else if(input[0] == 'i' && input[1] == '\n' && input[2] == '\0'){
                carac(matrix);
            }
            else if((sscanf(input, "%c %lu %lu %lf\n", &input[0],
             &a, &b ,&c) == 4)){
                adds(a, b, c, matrix);
            }
            else if(input[0] == 'l' && (sscanf(input, "%*c %lu\n", &a) == 1)){
                printLine(a , matrix);
            }
            else if(input[0] == 'c' && (sscanf(input, "%*c %lu\n", &a) == 1)){
                printColumn(a , matrix);
            }
            else if(input[0] == 'z' && (sscanf(input, "%*c %lf\n", &c) == 1)){
                zero(c , matrix);
            }
            else if(input[0] == 'o' && input[1] == '\n' && input[2] == '\0'){
                sortLine(matrix);
            }
            else if(input[0] == 'o' && (sscanf(input, "%*c %s\n", col) == 1) && strcmp(col, "column") == 0){
                sortColumn(matrix);
            }
            else if(input[0] == 'w' && input[1] == '\n' && input[2] == '\0'){
                file(matrix);
            }
            else if(input[0] == 'w' && (sscanf(input, "%*c %s\n", na) == 1) ){
                newFile(na, matrix);
            }
        }
    Does anyone have advice on how to check if the name is between " " and what scanning function is best?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I believe that I already told an ungrateful loser how to do this in another post.
    Unknown length of console input
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking input from user
    By telmo_d in forum C Programming
    Replies: 5
    Last Post: 07-07-2015, 02:09 PM
  2. Checking User Input
    By Dusterdoo in forum C Programming
    Replies: 9
    Last Post: 06-25-2011, 03:04 PM
  3. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 10:03 PM
  4. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 05:33 PM
  5. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 10:32 AM

Tags for this Thread