I am having a problem with the following code: - System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
-
String^ texts = textBox1->Text;
-
char *text = strToChars(texts);
-
String^ shiftcs = domainUpDown1->Text;
-
char *shiftc = strToChars(shiftcs);
-
int shift = atoi(shiftc);
-
char *cipher = encCCipher(text, shift);
-
String^ ciphers = gcnew String(cipher);
-
textBox2->Text = ciphers;
-
}
-
...
-
char* strToChars(System::String^ str) {
-
msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
-
const char* chars = context->marshal_as<const char*>(str);
-
return (char*)chars;
-
}
-
...
-
char* encCCipher (char *plain, int shift) {
-
char lowerAlphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
-
int len = strlen(plain) ;
-
char *cipher = plain;
-
char cchar = NULL;
-
int ccode = 0;
-
for (int i = 0; i < len; i++) {
-
cchar = tolower(plain[i]);
-
ccode = NULL;
-
for (int j = 0; j < 26; j++) {
-
if (cchar == lowerAlphabet[j]) {
-
ccode = j;
-
break;
-
}
-
}
-
if (ccode != NULL){
-
cipher[i] = lowerAlphabet[(ccode + i) % 26];
-
} else {
-
cipher[i] = cchar;
-
}
-
}
-
return cipher;
-
}
-
Entering "Testing cipher" into textbox1 puts out "tfuwmsm krzsqe."
After pressing the button the variables have the following in them:
texts: "Testing cipher"; text: 0x007B6B78 "tfuwmsm krzsqe"; shiftcs: "0"; shiftc: 0x007B6BC8 "0"; shift: 0; cipher: 0x007B6B78 "tfuwmsm krzsqe"; ciphers: "tfuwmsm krzsqe"
So I'm pretty sure the problem is in the converting the input from the textbox to a char* array.
And if you are wondering, this is a Caesar cipher program.
This is c++ code compiled on visual c++ express 2008 on vista.
1 3713
Never mind, I figured it out. The problem was in the for loop. - char* encCCipher (char *plain, int shift) {
-
char lowerAlphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l',' m','n','o','p','q','r','s','t','u','v','w','x','y' ,'z'};
-
int len = strlen(plain) ;
-
char *cipher = plain; //should be "char *cipher = new char; strcpy(cipher, plain);"
-
char cchar = NULL;
-
int ccode = 0;
-
for (int i = 0; i < len; i++) {
-
cchar = tolower(plain[i]);
-
ccode = NULL;
-
for (int j = 0; j < 26; j++) {
-
if (cchar == lowerAlphabet[j]) {
-
ccode = j;
-
break;
-
}
-
}
-
if (ccode != NULL){
-
cipher[i] = lowerAlphabet[(ccode + i) % 26]; //"i" should be "shift"
-
} else {
-
cipher[i] = cchar;
-
}
-
}
-
return cipher;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Trep |
last post by:
Hi there!
I've been having a lot of difficult trying to figure out a way to convert
a terminated char array to a system::string for use in Visual C++ .NET
2003.
This is necessary because I...
|
by: AlexFarokhyans |
last post by:
Hello,
I'm trying to convert String to Char array. I'm getting a string from
user input text box and then I have char firstName.
I need to convert the string that is in the text box to
firstName....
|
by: Yan Vinogradov |
last post by:
There are two ways to convert a char* received from the unmanaged code
into a managed System.String object that I am aware of. The first one
is to simply construct String object passing char* as...
|
by: Yifan |
last post by:
Hi
Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks
Yifan
|
by: Marcus Kwok |
last post by:
Hello, I am working on cleaning up some code that I inherited and was
wondering if there is anything wrong with my function. I am fairly
proficient in standard C++ but I am pretty new to the .NET...
|
by: Alejandro Aleman |
last post by:
Hello!
i know this may be a newbie question, but i need to convert a string from
System::String^ to char*, in the msdn page tells how, but i need to set to
/clr:oldSyntax and i dont want it...
|
by: DaTurk |
last post by:
Hi,
I have several interfaces in CLI that I access via c#. My problem is,
is that down in the unmanaged c++, which the CLI lies on top of, I
have a lot of c_str() happening. But all of my...
|
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= |
last post by:
Hi,
I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper
around some C++ LIBS. I've created some classes that contains a pointer to
the LIB classes and everthing seems to...
|
by: Vivienne |
last post by:
Hi,
I am using a c# library in my c++ project. so I have to convert a
basic string to System::string to make use of a function that takes
System::String as argument. I did it like this:
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |