Thread: Scanning a char then unknow number of ints and doubles

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

    Scanning a char then unknow number of ints and doubles

    So i have a program and the user is supposed to type in the method he wants used. For example if he types "p" it runs a method that has no parameters but he can also type "a 1 1 1.5" thats meant for another method with 2 ints a double as parameters. I was trying to use this:

    Code:
               char com;    
               int a, b;
               double c;
    
               scanf("%c %d %d %lf", &com, &a, &b, &c);
    
               while(com != 'q'){
                    if(com == 'p'){
                       list(matrix);
                     }
                    else if(com == 'a'){
                       adds(a, b, c, matrix);
                     }
                    else if(com == 'i'){
                       carac(matrix);
                    }
              scanf("%c %d %d %lf", &com, &a, &b, &c);     
              }
    
    But he is not running to correct methods when i type the letter. When the user types "q" the program is supposed to end. Can anyone help me to find whatīs wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Read the whole line using fgets()
    Examine the first char, then choose an appropriate sscanf() to extract the params you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    Will that work? I got to be able to type "a 1 1 1.2", i canīt type "a" then ENTER to let to program inspect the char and then ask me for the params. Sorry if its a dumb question, im fairly new at C.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    You could break up your scanf calls.
    Code:
    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);
    	}
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    It works but the first command i type doesnīt do anything, for example i type "p" and nothing happens and the next command does what the p command was supposed to do and so on. Do you know why itīs wrong?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by PTM View Post
    It works but the first command i type doesnīt do anything, for example i type "p" and nothing happens and the next command does what the p command was supposed to do and so on. Do you know why itīs wrong?
    Did you remove the space before the %c ? It needs to be there. It causes scanf to skip initial spaces, including the newline left by the previous scanf, before reading the character. The %d and %lf don't need the space before them because they skip initial whitespace by default. In fact, I believe only %c, %[ and %n don't initially skip whitespace.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading ints and doubles using scanf
    By Gallivant in forum C Programming
    Replies: 8
    Last Post: 04-24-2015, 09:14 AM
  2. Only Accepting Ints and Doubles
    By Erik Ingvoldsen in forum C++ Programming
    Replies: 24
    Last Post: 02-20-2015, 04:52 PM
  3. ints and doubles problem
    By iLLiCiT in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 03:42 PM
  4. Replies: 17
    Last Post: 08-03-2004, 02:16 PM

Tags for this Thread