What's wrong with my C-code?

2019-09-02 02:43发布

问题:

Working in z/os mainframe, I have the following situation. I can't record a transaction. here is my code, it is written for transaction using jcl. Bur something goes wrong.

/* Include standard C header files */           
 #include <ctype.h>               
 #include <stdio.h>               
 #include <stdlib.h>              
 #include <string.h>                            
 /* Include WebSphere MQ API header file */      
 #include <cmqc.h>                              
 /* Boolean constants */          
 #define TRUE  1                  
 #define FALSE 0                               
 /* Function prototypes */        
 void displayUsage (char *programName);          
 void displayMQError (char *message, MQLONG compCode, MQLONG reason);    
 double parseAmount (char *amountString);          
 /* Main Program                */

int main (int argc, char *argv[])
 {        
   /* Variable definitions */     
   double   paymentAmount;                     /* Payment amount       */

   MQBYTE   msgBuffer[1024];                   /* Buffer for messages  */

   MQCHAR*  qmgrName;  /* Queue manager name   */ 
   MQCHAR*  requestQueueName;                  /* Request queue name   */ 
   MQCHAR*  replyQueueName;                    /* Reply queue name     */ 
   MQCHAR*  userID;    /* Contestant user ID   */ 
   MQCHAR*  paymentDescription;                /* Payment description  */ 

   MQGMO    getMsgOptions = {MQGMO_DEFAULT};   /* Get options          */ 

   MQHCONN  hConn = MQHC_UNUSABLE_HCONN;       /* Connection handle    */ 

   MQHOBJ   requestHObj = MQHO_UNUSABLE_HOBJ;  /* Request queue handle */ 
   MQHOBJ   replyHObj = MQHO_UNUSABLE_HOBJ;    /* Reply queue handle   */ 

   MQLONG   compCode;  /* API completion code  */ 
   MQLONG   openOptions;                       /* Open queue options   */ 
   MQLONG   reason;    /* API reason code      */ 
   MQLONG   replyMsgLength = 0;                /* Reply msg length     */ 

   MQMD     requestMsgDesc = {MQMD_DEFAULT};   /* Message descriptor   */
   MQMD     replyMsgDesc = {MQMD_DEFAULT};     /* Message descriptor   */

   MQOD     requestQueueDesc = {MQOD_DEFAULT}; /* Object descriptor    */
   MQOD     replyQueueDesc = {MQOD_DEFAULT};   /* Object descriptor    */

   MQPMO    putMsgOptions = {MQPMO_DEFAULT};   /* Put options          */

   /*****************************************/   
   /* Initialisation and parameter checking */   
   /*****************************************/   

   printf ("********************************************************\n");
   printf ("Credit card payment unit test tool\n");
   printf ("********************************************************\n");
   printf (" \n");                

   switch (argc)  
   {      
     case 7  : qmgrName         = argv[1];       
               requestQueueName = argv[2];       
               replyQueueName   = argv[3];       
               userID           = argv[4];       

               paymentAmount = parseAmount (argv[5]);                    

               if ( ( paymentAmount < 0.01 )     
                 || ( paymentAmount > 9999.99 ) )
               {                  
                 printf ("The payment amount must be a valid numeric " \ 
 "within the range 0.01 to 9999.99\n");          

                 displayUsage (argv[0]);         
                 return (1);      
               }                  

               paymentDescription = argv[6];     

               if ( ( strlen (paymentDescription) < 1 )                  
                 || ( strlen (paymentDescription) > 35 ) )               
               {                  
                 printf ("The payment description must be 1-35 " \       
 "characters\n");        

                 displayUsage (argv[0]);         
                 return (1);      
               }                  

               break;             

     default : printf ("Incorrect usage!\n");    
               displayUsage (argv[0]);           
               return (1);        
   }      

   printf ("You have requested a payment of %.2f with description " \    
           "'%s' for contestant %s\n", paymentAmount,                    
           paymentDescription, userID);          

   printf (" \n");                

Can you help me? I don't know how to write my Parm parameters...

回答1:

When you have a problem, try to track all the outputs back to the inputs via the program.

From one of your other questions:

********************************************************                        
Credit card payment unit test tool                        
********************************************************                        

Incorrect usage!                                                                

Program Usage                                                                   
-------------                                                                   

PAYMENT <queue manager> <cics request queue> <reply queue> <userid> <payment value> <payment description>

The payment description has a maximum length of 35 characters.                  

The monetary value must be within the range 0.01 - 9999.99

The first two lines are easy to explain. The last two lines, not so. They are there to blinker us, as that exact text appears in the program. However, it seems that the exact same text is used in the displayUsage function.

It is bad to display identical text in different error circumstances. That's why we use message numbers - the message may then be the same, but the number will identify exactly where it has come from.

A clue is that you can't even get a very simple PARM to be accepted.

A further clue is that Incorrect usage!.

When does that message get printed? When there are other than seven parameters. Err... but you only supply six.

So, if there are supposed to be six parameters, the program is wrong. If you should be supplying seven parameters, your PARM is wrong.

You can demonstrate this with

// PARM='A,B,C,D,1,F,G'

That will work.

As an aside, why was switch used instead of a simple if anyway? Using if would have made the problem more obvious.