| Quantum7 (28) | |
| I would like to know how I should go about counting letters in a string. I want to count letters in a string so that I can do math with each letter count and display results of the math in windows. I've already done it in the console and now I want to upgrade to windows. | |
|
|
|
| Filiprei (222) | |||
I am not sure what you mean but this is how I would do it:
and from there you can make whatever you want, examples are, a program that countes spaces, a program that counts words and so on. | |||
|
|
|||
| andywestken (1964) | |
|
A Windows GUI app can use the same code to count the letters in a string as a console app (which you say you've already written). It's the display code that would have to be different. Andy PS The Win32 API does provide alternatives to the standard string processing functions, but normally you stick to the standard ones. | |
|
|
|
| Quantum7 (28) | |
| Cool, thanks. No wonder it compiled alright with the console code. I just needed to figure out how to link the input and output with the code. | |
|
|
|
| Quantum7 (28) | |
| If I have a string variable such as str and str[] already in my console code, how would I go about tying that to an input text box and having the output at least go to a read only text box for starters? I screwed up with that before and I wanna find out the right way to do it. | |
|
|
|
| andywestken (1964) | |
|
Are you using old-school windowing? (i.e. WinMain, WindowProc, RegisterClassEx, CreateWindowEx, SetWindowText, etc.)? Or some other way?? Andy | |
|
Last edited on
|
|
| CatmanFS (8) | |||
|
I use old school char arrays to do all my dirty work. char *phrase = new char[64]; // now you have an array 64 chars long I wrote a function that would check to see how many actual characters were in the array.
Makes things easier to find little shortcuts until a real answer is found. | |||
|
|
|||
| Quantum7 (28) | |
| I'm using a windows form in Microsoft Visual Studio 2010. | |
|
|
|
| andywestken (1964) | |
| So you're using C++/CLI rather than C++ then? | |
|
|
|
| Quantum7 (28) | |
| I guess so. What I want to know is how I can equate this->textBox1-> with char str[4095] = ""; for the text input. I also want to output ten int variables to a read only text box called this->textBox2-> | |
|
|
|
| andywestken (1964) | |||
Setting the text of a text box is straightforward enoughthis->textBox1->Text = L"Hello world";But you can't use a char* for the value of text: it requires a System::String^. So you'll need to convert any char* values, etc you use. Or work with the System::String directly, rather than a char*
Note that the issue isn't Windows GUI vs console. It's because you're swapping to another language (C++/CLI vs C++) when you work with the .Net Form library. Andy | |||
|
Last edited on
|
|||
| Quantum7 (28) | |
| Yes, this-> is what I figured that I needed to do. Hopefully I can get things working good. I'll let you know what happens. Would the output need to be converted to a system string too or is there a way to output int's into a text box? | |
|
|
|
| Quantum7 (28) | |||
|
Maybe I should show you my code so that you can know what I'm doing exactly: Here is Form1.h
| |||
|
|
|||
| Quantum7 (28) | |||
Here is NTS.cpp
| |||
|
|
|||
| andywestken (1964) | |
|
You really need to give it another go, as it's rather repetitive and verbose! The ascii codes for 'A', 'B', 'C', ... are a series. So you know that 'A' - 'A' = 0, 'B' - 'A' = 1, 'C' - 'A' = 2, ... Can you see how that allows you to work with an array? Andy PS Be sure to check that the char you're counting is a letter, rather than a number, punctuation mark, etc. before calculating the offset to 'A'. And don't forget case either! | |
|
|
|
| Quantum7 (28) | |
| I'm working on a numerology program where specific numbers are associated with each letter. The numerology math works a certain way where I have to count the numbers that all the letters cause, to do multiplication with each number type. This code works in the console, cause I tested it. Now I want to do input and output with it in windows. | |
|
|
|
| andywestken (1964) | |||||
|
Well, if you want to use your existing C++ code with a C++/CLI GUI then you'll need to convert the System::Strings back and forth between char* As I showed above, char* to System::String is easy enough: you just construct a System::String from the char*
The reverse, System::String to char*, is a bit more involved:
where you can read the TextBox like: String^ input = this->textBoxInput->Text;Andy PS For more info, see How to convert from System::String* to Char* in Visual C++ http://support.microsoft.com/kb/311259 | |||||
|
Last edited on
|
|||||
| Quantum7 (28) | |
|
Very nice. Thank you. I was just elaborating on the logic of String^ input = this->textBoxInput->Text; and I had a thought that if I want to output into a read only text box it could look like: String^ output = this->textBoxOutput->Text; I'm sure there could be more to it, but that was just a thought I guess. Maybe its different. I'm still learning, but the more that I learn the more that I can elaborate on alterations if needed. | |
|
|
|
| Quantum7 (28) | |
| A very big question right now is: What H files do I need to include to make your conversion code work? | |
|
|
|
| Imadatobanisa (647) | |||||
|
Generally speaking : That's good because I usually work with string & characters. :) Comparing a Uppercase character is always faster than comparing a Lowercase character. (This is a good knowledge & concept) So I often use :
More convenient, to get the total amount of a specific character of a string instantly, I usually use : (My favorite code) :
| |||||
|
Last edited on
|
|||||