checking a command validity in C

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

57 Replies - 3354 Views - Last Post: 09 June 2017 - 05:49 AM Rate Topic: -----

#1 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

checking a command validity in C

Posted 01 June 2017 - 03:27 AM

im trying to make a loop that asks a command from stdin and checks if it's valid or not in C,
and the command for example will be in this format : command A,24.3,7.8
or it could be : command A, 24.3, 7.8
with 1 space between the command and the letter, while ignoring all the other spaces, i'd just do it with sscanf().
but i got stuck on checking the format and the commas..
  int i;
  char s[100]={0};
  char *comma=",";
  char *cp;
  printf("Enter a command:\n");
  gets(s);
  cp=strtok(s,comma);
  for(i=0;i<2,i++)
  cp=strtok(NULL,comma)

how do i check werther there are commas missing in the input ...?

This post has been edited by AdmDTR: 01 June 2017 - 03:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: checking a command validity in C

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: checking a command validity in C

Posted 01 June 2017 - 03:32 AM

Is
A,,
legal?

One thing strtok doesn't handle at all well is consecutive delimiters.

If the first parameter is a single letter, then I would try
char myCmdLetter;
double f1, f2;
if ( sscanf(s," %c ,%lf,%lf", &myCmdLetter, &f1, &f2 ) == 3 ) {
  // success
}


Was This Post Helpful? 0
  • +
  • -

#3 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 01 June 2017 - 03:43 AM

View PostSalem_c, on 01 June 2017 - 03:32 AM, said:

Is
A,,
legal?

One thing strtok doesn't handle at all well is consecutive delimiters.

If the first parameter is a single letter, then I would try
char myCmdLetter;
double f1, f2;
if ( sscanf(s," %c ,%lf,%lf", &myCmdLetter, &f1, &f2 ) == 3 ) {
  // success
}



no its not legal the command should be exactly in the format "command, 1 space at least, a letter, comma, double, comma , double"
(thats y i want to check if there are missing commas or not) and yea about that 1 space between the command and the letter...
other than at least 1 space between the command and the letter should ignore all other spaces.

the way you used sscanf equals 3 does that mean "if there are 3 parameters scanned" ?
so if it got only 2 or more than 3 it'll be false ?

thanks for fast respond !!

This post has been edited by AdmDTR: 01 June 2017 - 03:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 19,861
  • Joined: 05-May 12

Re: checking a command validity in C

Posted 01 June 2017 - 06:35 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

You are contradicting yourself. In your OP you said that this was a valid command:
command A, 24.3, 7.8



Yet in your post #3, you make no mention that spaces after the comma are allowed. Which is it?

Do not use gets(). It is so unsafe due to all the potential of buffer overruns that it has been deprecated.

You'll want to input the entire line as input using fgets().

Take a look at the documentation, all the information that you need is there to make it work.
Was This Post Helpful? 0
  • +
  • -

#5 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 01 June 2017 - 12:29 PM

View PostSkydiver, on 01 June 2017 - 06:35 AM, said:

You are contradicting yourself. In your OP you said that this was a valid command:
Yet in your post #3, you make no mention that spaces after the comma are allowed. Which is it?


i did say that all other spaces are ignored though... meant by that that they are allowed and will be ignored if they're there
only but one space between the command and the letter im sorry for my bad language,

and the reason i want to do that is, well here's 1 function of mine
void readN(complex *compNum, double a, double B)/>/>
{
	compNum->x = a;
	compNum->y = b;
}

the complex is a struct that has 2 members.
and so i want to call it with the input that i get from stdin , the "letter" is meant to be "which struct" and the two doubles are the members of it.
so i'd be sending it to which struct it should fill those doubles
i'll be using fgets then thanks for noting that!
will be very glad if i'd get some help in the method of how to do it...

This post has been edited by AdmDTR: 01 June 2017 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 01 June 2017 - 12:51 PM

i've come to this till now..
but i get this :

main.c:31:3: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘struct complex *’ [-Wformat]

when i compile it cuz i need to set it as a complex (struct) type so i could call the function with it, not a character
but idk how...
help pls

 char cmpnum;
  complex *cmpstrct1;
  float num1=0;
  float num2=0;
  fgets(s,100,stdin);
  sscanf(s,"%c %f %f",&cmpnum,&num1,&num2);
  cmpstrct1=&cmpnum;

This post has been edited by AdmDTR: 01 June 2017 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,870
  • Joined: 31-December 10

Re: checking a command validity in C

Posted 01 June 2017 - 02:05 PM

Well we don't have all the code so we don't even know which line it's talking about, we could guess but it would be more helpful if you posted the entire program or at least the smallest example of the program that you can.

I have a question, why did you define a char like this: char *comma=",";???
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 19,861
  • Joined: 05-May 12

Re: checking a command validity in C

Posted 01 June 2017 - 08:18 PM

If you recall, strtok() takes as its second parameter a C string that contains all the delimiters for the tokens. Most people will declare this as char * delimiters = ","; where the identifier is more descriptive.
Was This Post Helpful? 1
  • +
  • -

#9 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 02 June 2017 - 10:36 AM

View Postvividexstance, on 01 June 2017 - 02:05 PM, said:

it would be more helpful if you posted the entire program or at least the smallest example of the program that you can.
I have a question, why did you define a char like this: char *comma=",";???


i defined the char a comma pointer becuase i intended to search for commas and remove them after taking from stdin
and then do a sscanf() and get each part of the input, a command and two doubles

here's my program:
typedef struct 	CompNumber{  /* Structure that holds a complex number*/

  double x;
  double y;

} complex;

/* Inserts a comp number */
void readcomp(complex *compNum, double a, double B)/>/>
{
	compNum->x = a;
	compNum->y = b;
}

/* Prints a comp number */
void printcomp(complex compNum, char num)
{
	printf(" %c = %.4f + (%.4f)i\n",num,compNum.x,compNum.y);
}

int main()
{
  complex A,B,C,D,E,F;
			/*=========== Resets numbers to zero ===========*/
  A.x = A.y = B.x = B.y = C.x = C.y = D.x = D.y = E.x = E.y = F.x = F.y = 0;

			/*=========== Command diagnosing ===========*/
  
  char s[100]={0};  
  char *comma=",";
  char *cp;
  printf("Complex number program start :\nEnter a command:\n");

  cp=strtok(s,comma);   /* with this im trying to do a commas check, as the input should be
                          in this format: Command(space)Letter(comma)double(comma)double
                         with at least 1 space between "Command" and "Letter", ignoring all the other spaces if there are ones */

  /* after done checking the command validity with commas and the space between the command and the letter 
     i'll put this in a switch - case */
  complex cmpstrct1;
  double num1=0;
  double num2=0;
  fgets(s,100,stdin);
  sscanf(s,"%c %lf %lf",&cmpstrct1,&num1,&num2); /* here is where im stuck and confused, 
                                                   how do i get a complex ( struct) type ? */

  readcomp(&cmpstrct1,num1,num2);

  printcomp(A,'A');

  return 0;
}


View PostSkydiver, on 01 June 2017 - 08:18 PM, said:

If you recall, strtok() takes as its second parameter a C string that contains all the delimiters for the tokens. Most people will declare this as char * delimiters = ","; where the identifier is more descriptive.

my only intention for using it is to check werther commas are there or not which is the only delimiter im looking for in this case.
im just coinfused on how should i be doing it..
im quite the beginner you see

This post has been edited by AdmDTR: 02 June 2017 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1990
  • View blog
  • Posts: 5,316
  • Joined: 27-December 05

Re: checking a command validity in C

Posted 02 June 2017 - 04:51 PM

If you use sscanf to validate your input as Salem_c suggested, you don't need strtok at all. Since your input consists of a string, a space, a single char, a comma, a double, a comma, and another double, you'll need to modify the conversion string suggested in Post #2 slightly.

Here's a slightly silly example using sscanf to validate a string. Try it, study it along with the documentation, and then see what happens if you delete the comma from line 6.

#include <stdio.h>

int main ()
{
  char input[]="mary      had , 2.5 lambs";
  char str1[10], str2[10], str3[10];
  double qty;
  int retval;
  
  retval = sscanf (input,"%s %s , %lf %s",str1, str2, &qty, str3);
  if(retval == 4) {
    printf ("who: %s\nwhat: %s\nhow many: %f\n", str1, str3, qty);
  }
  else {
    printf("invalid input\n");
  }
  printf("retval is %d\n", retval);
  
  return 0;
}



Oh, and by the way, your cmpstrct1 can't be both a CompNumber and a char. You need 2 separate variables: a char to take the letter provided by the user's input, and a CompNumber to instantiate the complex number after the input has been read and validated.
Was This Post Helpful? 1
  • +
  • -

#11 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 03 June 2017 - 06:47 AM

thanks guys i'm almost done with my program ,just one thing:
when i do sscanf(), i need to check werther there is a space between the command and the letter,
so my question is, how do i scan a "space" ?
the format is: command(space)letter(comma)double(comma)double

This post has been edited by AdmDTR: 03 June 2017 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#12 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 03 June 2017 - 07:55 AM

or scan a "," alone ?
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1990
  • View blog
  • Posts: 5,316
  • Joined: 27-December 05

Re: checking a command validity in C

Posted 03 June 2017 - 08:09 AM

A blank space in the scanf format string matches zero or more whitespace characters. If your requirement is "at least 1 blank space between the command and the letter" it's easy. For example:
#include <stdio.h>

int main ()
{
  char input[]="square  x , 2.5";
  char ch, str1[10];
  double num;
  int retval;
  
  retval = sscanf (input,"%s %c , %lf",str1, &ch, &num);
  if(retval == 3) {
    printf ("cmd: %s\ntarget: %c\nvalue: %f\n", str1, ch, num);
  }
  else {
    printf("invalid input\n");
  }

  return 0;
}

will read a string (your command), a single char (your letter), and a value. In that example it will accept the input if there are 1 or more spaces between the string and the letter. (If there are no spaces, the letter becomes part of the string so the input is clearly invalid.)

If you require EXACTLY 1 blank space between the command and the letter, you can do something like this:
#include <stdio.h>

int main ()
{
  char input[]="square  x , 2.5";
  char ch, str1[10];
  double num;
  int retval;
  
  retval = sscanf (input,"%s%*c%c , %lf",str1, &ch, &num);
  if(retval == 3) {
    printf ("cmd: %s\ntarget: %c\nvalue: %f\n", str1, ch, num);
  }
  else {
    printf("invalid input\n");
  }

  return 0;
}

In this example the input will be rejected because there are 2 spaces separating the command from the letter. The %*c element in the format string will consume and discard a single character (a single blank space in a valid input) and then the next %c will store the next char (the letter).

I'm not sure what you're asking in your last question

Quote

or scan a "," alone ?
The comma in the format string matches a comma in the input sequence.
Was This Post Helpful? 0
  • +
  • -

#14 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: checking a command validity in C

Posted 03 June 2017 - 08:10 AM

Is it always exactly one space?
if ( sscanf(buff,"%s%c%c,%lf,%lf",command,&spaceChar,&cmdChar,&f1,&f2) == 5 )



Or do you just not care, so long as it's always >= 1 space(s)?
if ( sscanf(buff,"%s %c,%lf,%lf",command,&cmdChar,&f1,&f2) == 4 )



In scanf format strings, every letter stands for itself, except for percents and whitespace.
- a single whitespace represents any unbroken sequence of spaces, tabs, newlines (all those where isspace() returns true)
- a single % character introduces a format conversion specification.
- a double %% represents a literal % of input.
Was This Post Helpful? 0
  • +
  • -

#15 AdmDTR  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 24-December 16

Re: checking a command validity in C

Posted 03 June 2017 - 08:10 AM

View Postr.stiltskin, on 02 June 2017 - 04:51 PM, said:

You need 2 separate variables: a char to take the letter provided by the user's input, and a CompNumber to instantiate the complex number after the input has been read and validated.

i didn't really get what you mean,
so i should do somethign like this : ?
complex CompNumber;
char t; /* user's input */
if (t==A)
    compNumber->A;
else if (t==B)/>/>
    compNumber->B;
/* and so on for A-F */

This post has been edited by AdmDTR: 03 June 2017 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »