?
Solved

c/c++ function

Posted on 2009-05-01
12
Medium Priority
?
208 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:skywaker
  • 5
  • 4
  • 3
12 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24282032
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24282080
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
 

Author Comment

by:skywaker
ID: 24282128
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24282227
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24282251
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
 

Author Comment

by:skywaker
ID: 24282375
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
 

Author Comment

by:skywaker
ID: 24282406
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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24282435
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24282445
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
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1000 total points
ID: 24282511
@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
 

Author Comment

by:skywaker
ID: 24282591
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
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1000 total points
ID: 24282668
@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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question