Thread: Scanning chars, ints, doubles and char arrays

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    16

    Scanning chars, ints, doubles and char arrays

    So i have a program that asks the user a command (a letter) to run a specific method, it can be just the letter, a letter and some numbers, or a letter and another word, right now i have this:

    Code:
    char com;
    char col [6];
    char name [MAXELEMENTS];
    int a, b;
    double c;
    
      while(scanf(" %c", &com) == 1 && com != 'q'){
        if(com == 'p'){
            list(matrix);
            }
        else if(com == 'a'){
             scanf("%d %d %lf", &a, &b, &c);
                 adds(a, b, c, matrix);
        }
        else if(com == 'i'){
             carac(matrix);
        }
        else if(com == 'l'){
             scanf("%d", &a);
             printLine(a, matrix);
        }
        else if(com == 'c'){
             scanf("%d", &a);
             printColumn(a, matrix);
        }
        else if(com == 'z'){
             scanf("%lf", &c);
             zero(c, matrix);
        }
        else if(com == 'o'){
             if((scanf("%s", col)) == 1)
            sortColumn(matrix);
        else if((scanf("%s", col)) == 0)
             sortLine(matrix);
        }
        else if(com == 'w'){
             if((scanf("%s", name)) == 1)
             ficheiro(name, matrix);
            else if((scanf("%s", name)) == 0)
             ficheiroNovo(matrix);
        }
        }
    So iīm having trouble in the commands "o" and "w". On "o" you are supposed to have the option to just type "o" and it should run the method sortLine or you type the "o column" and it runs sortColumn. On "w" the same concept, you just type "w" and it runs ficheiroNovo or you type "w" plus a name and it should run ficheiro. The thing is i canīt just type "o" or "w", it always asks for another input and always runs the sortColumn or ficheiro. Does anyone know whatīs wrong anc can someone help me?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    That happens because scanf is called twice inside 'o' and 'w'. If you want to check the return value of scanf, call it once and then use the saved value on multiple if statements.
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Although, it still wouldn't work as you wanted, because scanf would not return zero unless it failed to read anything (for example, when the user pressed Ctrl-D or Ctrl-Z).

    What you really want to do is read the whole line beforehand and scan it manually, with sscanf.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    I got to this point:

    Code:
    char input[82];
    char col [6];
    char name [MAXCARAC];
    unsigned int a, b;
    double c;
    
    if(fgets(input, sizeof(input), stdin)){
            if(input[0] == 'q' && input[1] == '\n' && input[2] == '\0'){
                return -1;
            }
          
            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 %u %u %lf\n", &input[0], &a, &b ,&c) == 4)){
                adds(a, b, c, matrix);
            }
            else if(input[0] == 'l' && (sscanf(input, "%u\n", &a) == 1)){
                printLine(a , matrix);
            }
            else if(input[0] == 'c' && (sscanf(input, "%u\n", &a) == 1)){
                printColumn(a , matrix);
            }
            else if(input[0] == 'z' && (sscanf(input, "%u\n", &a) == 1)){
                zero(a , matrix);
            }
            else if(input[0] == 'o' && input[1] == '\n' && input[2] == '\0'){
                sortLine(matrix);
            }
            if(input[0] == 'o' && (sscanf(input, "%s\n", col) == 1) ){
                sortColumn(matrix);
            }
            else if(input[0] == 'w' && input[1] == '\n' && input[2] == '\0'){
                file(matrix);
            }
            if(input[0] == 'w' && (sscanf(input, "%s\n", name) == 1) ){
                newFile(name, matrix);
            }
    
          
    
                 
        }
    But im struggling with the "q" command that is supposed to exit the program... any advice on how i should do it?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Get rid of the [first] if and change to using a loop.
    I would suggest an while or do/while loop

    I would also add a statement before the fgets() call setting input[0] to zero.
    But, that is partly a style code decision.
    Another partly a style code decision is to use a switch statement instead of a big if statement.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Unless you have a specific reason for avoiding them, you should use the string functions, strcmp() in particular, when comparing multiple characters in a row.

    About your question, there are many ways to exit the program, it depends on what you want to do.
    *) exit() function
    *) return from main
    *) return a specific value from the function to check for
    *) if inside a loop, set the looping flag to false
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    Like this?

    Code:
    char input[82];
    char col [6];
    char name [MAXCARAC];
    unsigned int a, b;
    double c;
    
    if(fgets(input, sizeof(input), stdin)){
         while(input[0] != 'q'){
            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 %u %u %lf\n", &input[0], &a, &b ,&c) == 4)){
                adds(a, b, c, matrix);
            }
            else if(input[0] == 'l' && (sscanf(input, "%u\n", &a) == 1)){
                printLine(a , matrix);
            }
            else if(input[0] == 'c' && (sscanf(input, "%u\n", &a) == 1)){
                printColumn(a , matrix);
            }
            else if(input[0] == 'z' && (sscanf(input, "%u\n", &a) == 1)){
                zero(a , matrix);
            }
            else if(input[0] == 'o' && input[1] == '\n' && input[2] == '\0'){
                sortLine(matrix);
            }
            if(input[0] == 'o' && (sscanf(input, "%s\n", col) == 1) ){
                sortColumn(matrix);
            }
            else if(input[0] == 'w' && input[1] == '\n' && input[2] == '\0'){
                file(matrix);
            }
            if(input[0] == 'w' && (sscanf(input, "%s\n", name) == 1) ){
                newFile(name, matrix);
            }
    
    
    
    
        }
    I already tried it, but the first command i type keeps running non stop

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I think the while loop should be outside/above the if statement.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    Thanks a lot, the "w" and "o" commands are working, but the "l", "c" and "z" arenīt doing anything, is something wrong with those ifs?

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, it's the hazards of copying and pasting. You need to skip the first character in those scanf calls.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-24-2018, 04:31 PM
  2. Only Accepting Ints and Doubles
    By Erik Ingvoldsen in forum C++ Programming
    Replies: 24
    Last Post: 02-20-2015, 04:52 PM
  3. A lottery program - arrays, ints, chars etc
    By Glauber in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2008, 10:48 AM
  4. ints and doubles problem
    By iLLiCiT in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 03:42 PM
  5. Problem with chars and char arrays
    By Zagaberoo in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 08:47 AM

Tags for this Thread