• C

c/c++ function

Hi , can some one help me with the following code, i need to  add a function to this co de , were
do i put the function , the function is called at holderval[counter_A] =

holderval[counter] holds the value return by the function

function would be stringTonum,
eg :  case "A"
          return 1
        case "B"
        return 2


#include <stdio.h>
#include <string>
 
JNIEXPORT jstring JNICALL
Java_generateKey_generateNextKey(JNIEnv *env, jobject obj, jstring key)
{
    
    
    char buf[7]; //Max key length is 7 characters
   
    const char *str = (env)->GetStringUTFChars(key, 0);
    strcpy(buf, str);
    char position_six_A[1];
    char position_five_A[1];
    char position_four_A[1];
    char position_three_A[1];
    char position_two_A[1];
    char position_one_A[1];
    char holderval[7];
    int counter_A;
    (env)->ReleaseStringUTFChars(key, str);
    //Make whatever changes are required to generate the new key
    position_six_A[0]=buf[5];
    position_five_A[0]=buf[4];
    position_four_A[0]=buf[3];
    position_three_A[0]=buf[2];
    position_two_A[0]=buf[1];
    position_one_A[0]=buf[0];
    
    for (counter_A = 0; counter_A <= 5; counter_A++){
        holderval[counter_A] =   {{{{ I want t call Function from here}}}}
    }
        
    return (env)->NewStringUTF(buf);
     
}

Open in new window

skywakerAsked:
Who is Participating?
 
mrjoltcolaConnect With a Mentor Commented:
@sky: No offense, I feel that Kent is right in that you may be used to coding in a different language and may not understand how to approach the problem in C. That is what we are here for, but I will let Kent finish with you as I do not have more time today.

@Kent: Yes, you can still buy Jolt! :)

@Kent, I am still uncomfortable with what she is asking; I have the gut feeling I am answering her question, but that she may be asking the wrong question for the overall task, just as you pointed out. Hopefully you can help her find a solution by understanding her task, I wish I could stick around.
0
 
mrjoltcolaCommented:
Are you asking how to write the function as well, or do you already have stringTonum as a function?

To call it, you would change:

>>        holderval[counter_A] =   {{{{ I want t call Function from here}}}}

TO:

        holderval[counter_A] =   strTonum(...);

The ... are there because I don't know what argument you want to pass to it.
0
 
mrjoltcolaCommented:
In C/C++, just like Java, double quotes indicate strings. So your requirement for:

 case "A"
          return 1
        case "B"
        return 2

cannot be done with a case statement, switch/case only works on integral types. Are you sure you want the string "A" or the integral character 'A'?

If you are comparing individual characters, then you can write the switch as below.

But maybe you can explain, overall, what you are trying to accomplish with this JNI function in the first place?

switch(ch) {
   case 'A':  return 1;
   case 'B':  return 2;
}

Open in new window

0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
skywakerAuthor Commented:
hi , also how to write the function , i would pass the value held in buf[counter_A]

ie : if buf[1] = "A" then return 1 this will be stored in holderval[0]

ie: now value of holderval[0] would be 1
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi skywaker,

Your character definitions are a bit clumsy in that you're declaring what are essentially scalar variables as arrays of length 1.  That suggests that you're used to a language where you establish the length in the declaration.  That's not necessary for scalar C variables.

It will certainly be easier navigate if you'll change your declarations to be something like this:

    char buf[7]; //Max key length is 7 characters
    char position_six_A;
    char position_five_A;
    char position_four_A;
    char position_three_A;
    char position_two_A;
    char position_one_A;

C will also let you define things that look very much like variables, but are actually macros the evaluate to a variable.  Instead of moving the data one character at a time, these macros would simply reference the already existing data.

    char buf[7]; //Max key length is 7 characters
#define  position_six_A buf[6]
#define  position_five_A buf[5]
#define  position_four_A buf[4]
#define  position_three_A buf[3]
#define  position_two_A buf[2]
#define  position_one_A buf[1]
etc...

That's a bit of an odd way to use C macros, but for your current project it should allow you to code in the style in which you're already comfortable.


Kent
0
 
mrjoltcolaCommented:
buf[1] cannot equal "A", it can equal 'A'

The difference is important for you to understand. 'A' is a single character value.

So since buf is a character array, buf[1] can only hold one character.
Now the whole buf array can hold "CAFE" ... but that corresponds to buf[0] through buf[3]

So the main question, again, what are you trying to accomplish? I can answer your question, exactly, but I feel you may be approaching your problem wrong. Anway, here is a function that will return as you asked, but I named it charToNum because it is not a stringToNum. stringToNum would be someting like calling:   strToNum("CAFE");
But you are calling strToNum(buf[1]); which is only 1 character at a time.

int charToNum(char ch) {
   switch(ch) {
      case 'A':  return 1;
      case 'B':  return 2;
   }
 
   return 0;
}

Open in new window

0
 
skywakerAuthor Commented:
Thk u kdo, i recified the string declaration.,

i have split the string (key) into and array , eg '1BC3EFG' the buf[0] ='1'; buf[1]='B':

the purpose of the function is to return a numeric value : eg : if buf[0] = '1' then return 10
if buf[1]='B' then return 2

 i am trying to generate the next key in a range based on the lastkey
0
 
skywakerAuthor Commented:
hi i have the code below ,

but when i compile i get

 error: charToNum was not declared in this scope

/*generateKey.cc*/
#include "generateKey.h"
#include <stdio.h>
#include <string>
 
JNIEXPORT jstring JNICALL
Java_generateKey_generateNextKey(JNIEnv *env, jobject obj, jstring key)
{
    
    
    char buf[7]; //Max key length is 7 characters
   
    const char *str = (env)->GetStringUTFChars(key, 0);
    strcpy(buf, str);
    char position_six_A;
    char position_five_A;
    char position_four_A;
    char position_three_A;
    char position_two_A;
    char position_one_A;
    char holderval[7];
    void stringTonumber(char a,int b);
    int counter_A;
    (env)->ReleaseStringUTFChars(key, str);
    //Make whatever changes are required to generate the new key
    position_six_A=buf[5];
    position_five_A=buf[4];
    position_four_A=buf[3];
    position_three_A=buf[2];
    position_two_A=buf[1];
    position_one_A=buf[0];
    
    for (counter_A = 0; counter_A <= 5; counter_A++){
        holderval[counter_A] = charToNum(buf[counter_A]);
    }
        
    return (env)->NewStringUTF(buf);
     
}
int charToNum(char ch) {
   switch(ch) {
      case 'A':  return 1;
      case 'B':  return 2;
   }
 
   return 0;
}

Open in new window

0
 
mrjoltcolaCommented:
Add the function prototype (declaration) at the top, after your #includes but before your main JNI function



int charToNum(char ch);

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Sky,

I don't quite understand your rules for establishing the return value, but if you're not looking for specific data at a specific location, the suggestions made by MrJoltCola (can you even buy Jolt Cola anymore?) work very nicely.

   switch(buf[0]) {
      case '1':  return 1;
      case '2':  return 2;
   }
   switch(buf[1]) {
      case 'A':  return 3;
      case 'B':  return 4;
   }

If you are looking for specific values in specific locations, the switch structure may be difficult, but nested *if* statements work fine.

  if (buf[0] = '1')
    return 1;
  if (buf[1] = 'B')
    return 2;

But I suspect that you're really looking for something else.


Kent
0
 
skywakerAuthor Commented:
Hi , Kent and  Mr joldcola , thank you very much to both of you , both your thoughts helped  , my problem was understanding "A" and 'A'. Use Kents direction and problem solved .



will close shortly
0
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
@Jolt:  I see some irony and/or contradiction here.  Jolt Cola is still available but MrJoltCola isn't?  :)   (Good to know -- I'll look for it.)

@Jolt: "I am still uncomfortable with what she is asking";  She?  She?  Clearly, you have better video drivers than I do.  :)


Hi Sky,

Glad that we could help.....
Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.