Wednesday, 15 January 2014

c - getopt isn't working for one argument -


This is just a simple program that I wrote to get some practice with getopt and strokes.

  typedef structure {int age; Float body_fat; } Private; Typedef struct {const char * name; Individual glasses; } person; Int main (int argc, char * argv []) {char c; Person man While ((c = getopt (argc, argv, "n: a: b:"))! = -1) switch (c) {case 'n': guy.name = optarg; break; Case 'A': guy.specs.age = atoi (optarg); break; Case 'B': man. Specs.body_fat = atof (optarg); break; Case '?': If (optopt == 'A') {printf ("missing age! \ N"); } And if (optopt == 'b') {printf ("missing body fat! \ N"); } And if (optopt == 'n') {printf ("missing name! \ N"); } Other {printf ("wrong AGR! \ N"); }             break; Default: return 0; } Printf ("Name:% s \ nAge:% i \ nFat Percentage:% 2.2f \ n", guy.name, guy.specs.age, guy.specs.body_fat); Return 0; }  

Everything works well except the 'B' option. For some reasons it specifies that no one changes anything. It always returns as 0.0 I do not get why it will happen if other logic works fine.

Your example is missing header file which will declare related prototype. Adding these

  #include & lt; Stdlib.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Stdio.h & gt; #include & lt; String.h & gt;  

This works for me I have changed the type from c to int (one letter does not contain -1 ), And

  memset (& amp; man; 0, size (male));  

To ensure only one known value. Compiler Alerts Your friends have used this warning (I used a code called a code named code GCC-General ) to:

  #! / Bin / sh # $ Id: gcc-normal, v 1.4 2014 / 03/01 12:44:54 tom exp $ # These are my usual development options OPTS = "- Wall-West-Prototype -Vemission-Prototype -Wassow -Wocrons "$ {ACTUAL_GCC: -gcc} $ OPTS" $ @ " 

Although the RCS-identifier is quite recent, this is an old script that I use.


No comments:

Post a Comment