Char to int (MinGW)

Jan 28, 2016 at 8:01pm
I don't know if it matters but I'm using MinGW compiler. I have created a GUI program using Win32 API and have come very far. What I just need to know to be able to complete my program is how I can convert my char I grab from and edit control and turn it into an integer so i can pass it into my random string generator. Everything else works fine. I've tested it with another integer and everything works as it should except when I try to capture input from an edit control and make it into an int. I have tried "atoi" but it works weird and doesn't seem to capture all the numbers. Please explain it pretty easily since I'm quite a beginner. Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void GeneratePW(HWND hwnd) {

    const char* pw;
    int length;
    char length_char[256];
    int leng;

    char number[500];

    string pw_string;
    string chara = "abcdefghijklmnopqrstuvwxyz";

    leng = GetWindowTextLength(hwndEdit2);
    GetWindowText(hwndEdit2, length_char, leng);

    length = atoi(length_char);
    itoa(length, number, 10);
    MessageBox(NULL,number,"Password Generator",MB_ICONWARNING | MB_OK);

    if(length > 100) {
        MessageBox(NULL,"Too many characters!","Password Generator",MB_ICONWARNING | MB_OK);
    } else {

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX)) {
            chara += "0123456789";
        }

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX2)) {
            chara += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        pw_string = random(length, chara);
        pw = pw_string.c_str();


        SetWindowText(hwndEdit, pw);
    }

}
Last edited on Jan 28, 2016 at 8:08pm
Jan 29, 2016 at 5:58am
Try this.

1
2
3
4
5
6
7
// char to int
int ctoi(const char c)
{
	// converts the character to
	// its corresponding ASCII value
	return (int)c - (int)'0';
}
Jan 29, 2016 at 1:52pm
I get these errors:
|128|error: invalid conversion from 'char*' to 'char' [-fpermissive]|
|97|error: initializing argument 1 of 'int ctoi(char)' [-fpermissive]|

With this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

// char to int
int ctoi(const char c)
{
	// converts the character to
	// its corresponding ASCII value
	return (int)c - (int)'0';
}

void GeneratePW(HWND hwnd) {

    const char* pw;
    int length;
    char length_char[256];
    int leng;

    //char number[500];

    string pw_string;
    string chara = "abcdefghijklmnopqrstuvwxyz";

    leng = GetWindowTextLength(hwndEdit2);
    GetWindowText(hwndEdit2, length_char, leng);

    length = ctoi(length_char);

    //length = atoi(length_char);
    /*itoa(length, number, 10);
    MessageBox(NULL,number,"Password Generator",MB_ICONWARNING | MB_OK);*/

    if(length > 100) {
        MessageBox(NULL,"Too many characters!","Password Generator",MB_ICONWARNING | MB_OK);
    } else {

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX)) {
            chara += "0123456789";
        }

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX2)) {
            chara += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        pw_string = random(length, chara);
        pw = pw_string.c_str();


        SetWindowText(hwndEdit, pw);
    }

}
Jan 29, 2016 at 1:56pm
Your ctoi function takes a char. You're giving it a char array.
Last edited on Jan 29, 2016 at 1:58pm
Jan 29, 2016 at 1:59pm
So what should I do? (sorry I'm not the most professional C++ programmer I know)

EDIT: If I make length_char to char* length_char the ctoi function doesn't seem to work.
Last edited on Jan 29, 2016 at 2:07pm
Jan 29, 2016 at 2:08pm
What you should do depends on how you want to do things. Do you want to convert all the contents of the array into integers? Then you can just send in the entire array and loop through it, converting each of the characters.

1
2
3
4
5
6
 length = ctoi(length_char);

int ctoi(char c[], int size)
{
    //Code goes here
}


Or you can have the function the way it is, and instead call it over and over again, sending in one char at a time.

What exactly do you want?
Last edited on Jan 29, 2016 at 2:08pm
Jan 29, 2016 at 2:10pm
I want to grab the input from an edit control and make it to an int so I can use it for my random function. What I'm grabbing from the edit control is the length of the string I'm generating from the random function.
Last edited on Jan 29, 2016 at 3:21pm
Jan 29, 2016 at 3:23pm
Anyone got any idea how to do this or is it impossible?
Jan 29, 2016 at 5:23pm
Impossible?
Jan 29, 2016 at 5:42pm
I want to grab the input from an edit control and make it to an int

Do you mean a string of characters converted to a single int?
At the start of this thread, I think the impression was received (rightly or wrongly) that you wanted to convert a single character to an int.

Maybe you could give an example of the text or char you start with, and what resulting integer you'd like to end with.
Jan 29, 2016 at 5:45pm
Sure. (sorry for not making myself clear). So, I've made a pretty simple password generator. The thing is that I want the user to be able to select the length of the password and doing that by typing the numbers into an edit control. Then I would like those numbers to be transfered into one single integer so I can use it for my random string generator.
Last edited on Jan 29, 2016 at 5:47pm
Jan 29, 2016 at 5:59pm
Wouldnt something like this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){

	char password[256] = "25";

	int myNumber = std::atoi(password);

	cout << myNumber;

	return 0;
}



output:
25
Last edited on Jan 29, 2016 at 5:59pm
Jan 29, 2016 at 6:01pm
As I said. Atoi does'nt seem to work with my char. If I type the number 12 it just returns 1 for some reason.
Jan 29, 2016 at 6:05pm
That's probably because it's only returning the first character, which is 1, and skipping over the second character, which is 2. You're most likely doing something wrong. Would be useful if you could provide an example of the problem that compiles.

Alternativly, you can use sscanf whic his in #include "stdio.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "stdio.h"
using namespace std;

int main(){

	char password[256] = "25";

	int myNumber;
	sscanf(password, "%d", &myNumber);
		
	cout << myNumber;

	system("PAUSE");
	return 0;
}
Last edited on Jan 29, 2016 at 6:08pm
Jan 29, 2016 at 6:08pm
Yes. But if I type 120 it prints out 12 for some reason.
Last edited on Jan 29, 2016 at 6:46pm
Jan 29, 2016 at 6:10pm
If you mean the code, this is a code that compiles with atoi function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void GeneratePW(HWND hwnd) {

    const char* pw;
    int length;
    char length_char[256];
    int leng;


    char number[500];

    string pw_string;
    string chara = "abcdefghijklmnopqrstuvwxyz";

    leng = GetWindowTextLength(hwndEdit2);
    GetWindowText(hwndEdit2, length_char, leng);

    //length = ctoi(length_char);
    length = atoi(length_char);

    itoa(length, number, 10);
    MessageBox(NULL,number,"Password Generator",MB_ICONWARNING | MB_OK);

    if(length > 100) {
        MessageBox(NULL,"Too many characters!","Password Generator",MB_ICONWARNING | MB_OK);
    } else {

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX)) {
            chara += "0123456789";
        }

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX2)) {
            chara += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        pw_string = random(10, chara);
        pw = pw_string.c_str();


        SetWindowText(hwndEdit, pw);
    }

}
Jan 29, 2016 at 6:32pm
Got the same result as with atoi???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void GeneratePW(HWND hwnd) {

    const char* pw;
    int length;
    char length_char[256];
    int leng;


    char number[500];

    string pw_string;
    string chara = "abcdefghijklmnopqrstuvwxyz";

    leng = GetWindowTextLength(hwndEdit2);
    GetWindowText(hwndEdit2, length_char, leng);

    //length = ctoi(length_char);
    sscanf(length_char, "%d", &length);

    itoa(length, number, 10);
    MessageBox(NULL,number,"Password Generator",MB_ICONWARNING | MB_OK);

    if(length > 100) {
        MessageBox(NULL,"Too many characters!","Password Generator",MB_ICONWARNING | MB_OK);
    } else {

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX)) {
            chara += "0123456789";
        }

        if(IsDlgButtonChecked(hwnd, ID_CHECKBOX2)) {
            chara += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        pw_string = random(10, chara); //if it will work I will pass the int here
        pw = pw_string.c_str();


        SetWindowText(hwndEdit, pw);
    }

}
Last edited on Jan 29, 2016 at 6:55pm
Jan 29, 2016 at 6:48pm
Guys please help. Is this impossible or why doesn't atoi or sscanf work for me? I'm so confused. The only thing I try to accomplish is to convert the numbers I grab from an edit control and make them into one single integer. Example: I type the number "12" into the edit box. Then I want to convert it into an integer and by that setting the integer equal to "12" and pass it into my random string generator.
Last edited on Jan 29, 2016 at 6:54pm
Jan 29, 2016 at 7:44pm
Solved! Guys the problem was sooooooooo simple and I wasted hours. The problem was that I had to add + 1 to the GetWindowTextLength function. That's why atoi didn't work. Sorry everyone for wasting your time. Code:
 
leng = GetWindowTextLength(hwndEdit2) + 1;
Last edited on Jan 29, 2016 at 7:45pm
Topic archived. No new replies allowed.