#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Increment a Single value in a String by keeping other values as constant


    Hi team, I'm New to C Language, & to this Forum,

    I'm working in C Script: My code is

    int a[100];

    a[1]=GetTagBit("m1") // Where m1 is the Tag name
    a[2]=GetTagBit("m1") // Where m2 is the Tag name
    .
    .
    a[100]=GetTagBit("m1") // Where m100 is the Tag name

    if(m1=TRUE)
    {
    SetTagFloat("external_value1",GetTagFloat("internal_value1"));
    }

    if(m2=TRUE)
    {
    SetTagFloat("external_value2",GetTagFloat("internal_value2"));

    Te above program is working fine, i want to reduce the number of lines of program, by using for loop by incrementing the numbers inside the ""..

    Please help me to solve this issue.As i need to program for 100000 machines.

    Regards
    Ganesan
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    22
    Rep Power
    0
    What you need is for loop. Which loops through the set of code for N numbern of times. But from what you have shown iun your original post it looks like the N is 100. Which corresponds to something as follow as a sample.

    php Code:
     
       int index;
       char tag[25];
       int value[ 100 ];
     
       for( index=0; index < 100; index++ )
       {
          sprintf( tag, "m%d", &index );
          value[ index ] = GetTagBit( tag );
     
          if( value[ index ] == TRUE )  
            SetTagFloat( ... )
       }


    ssharish2005
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Thank you...


    Thank you.....


    Originally Posted by ssharish2005
    What you need is for loop. Which loops through the set of code for N numbern of times. But from what you have shown iun your original post it looks like the N is 100. Which corresponds to something as follow as a sample.

    php Code:
     
       int index;
       char tag[25];
       int value[ 100 ];
     
       for( index=0; index < 100; index++ )
       {
          sprintf( tag, "m%d", &index );
          value[ index ] = GetTagBit( tag );
     
          if( value[ index ] == TRUE )  
            SetTagFloat( ... )
       }


    ssharish2005
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Compare inputed String with other strings


    Hi friends

    I wanted to compare a string is equal to the list of Strings

    main()
    {
    string a;
    printf("Enter the string\n");
    gets(a);

    if(a=="G2-1A1"||"G2-1A2"||"G2-1A3"....||"G2-1A100")
    {
    output=100;
    }
    output=200;
    }



    in the above program, i have to check my input string with 100 strings, how can i do this...

    pleasehelp me.

    Regards
    Ganesan
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    First, read this, and never use gets() again

    > if(a=="G2-1A1"||"G2-1A2"||"G2-1A3"....||"G2-1A100")
    1. C doesn't do == for strings (or char* pointers, or char[] arrays).
    2. "G2-1A1"||"G2-1A2" doesn't compare a with all the things you list.
    So you would need to write
    if ( strcmp(a,"G2-1A1")==0 || strcmp(a,"G2-1A2") == 0 || strcmp(a,"G2-1A3") == 0

    Now, that's a stupid thing to write out, so perhaps you have something like this
    Code:
    char *matches[] = {
        "G2-1A1",
        "G2-1A2",
        "G2-1A3",
    };
    Or better yet, you have them in a file which you can read in when the program starts.

    With all your matches in an array, you can do
    Code:
    for ( i = 0 ; i < n ; i++ ) {
        if ( strcmp(a,matches[i]) == 0 ) break;
    }
    if ( i != n ) {
        // success
    }
    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

IMN logo majestic logo threadwatch logo seochat tools logo