Char* to int - can't get it to work!

Hello everyone!

So my code is below; the issue is I want to make it so that the user enters a bajillion numbers for birthday but only the first two numbers are taken into consideration. But I have no idea how to convert char to int @_@ ! If you know an easier way to do this please do tell me and thanks for any help in advance

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <windows.h> // Needed to set cursor positions
#include <string>
using namespace std;

struct Birthday
{
	string name;
	char birthday[1];
	char birthyear[1];
	int result;
	int numofpres;
	int moneyavail;
};

void placeCursor(HANDLE, int, int); // Function prototypes
void displayPrompts(HANDLE);
void getUserInput(HANDLE, Birthday&);
void displayData(HANDLE, Birthday);

int main()
{
	Birthday input; 
	

	
	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

	displayPrompts(screen);
	getUserInput(screen, input);
	displayData(screen, input);
	system("pause >nul");
	return 0;
}


void placeCursor(HANDLE screen, int row, int col)
{ // COORD is a defined C++ structure that
	COORD position; // holds a pair of X and Y coordinates
	position.Y = row;
	position.X = col;
	SetConsoleCursorPosition(screen, position);
}


void displayPrompts(HANDLE screen)
{
	placeCursor(screen, 3, 25);
	cout << "******* Birthday Form *******" << endl;
	placeCursor(screen, 5, 25);
	cout << "Name: " << endl;
	placeCursor(screen, 7, 25);
	cout << "Birthday: " << endl;
	placeCursor(screen, 7, 37);
	cout << "/" << endl;
	placeCursor(screen, 9, 25);
	cout << "How many presents do you want?: " << endl;
	placeCursor(screen, 11, 25);
	cout << "How much money can you spend on your party?: " << endl;
}

void getUserInput(HANDLE screen, Birthday &input)
{

     placeCursor(screen, 5, 31);
	getline(cin, input.name);

	placeCursor(screen, 7, 34);
	cin.getline(input.birthday,1);

	placeCursor(screen, 7, 38);
	cin.getline(input.birthyear, 1);

	placeCursor(screen, 9, 56);
	cin >> input.numofpres;
	placeCursor(screen, 11, 69);
	cin >> input.moneyavail;

	
}


void displayData(HANDLE screen, Birthday input)
{
	
	placeCursor(screen, 15, 0);
	cout << "Here is the data you entered.\n" << endl;
	cout << "Name : " << input.name << endl;
	cout << "Birthday : " <<  input.birthday << "/";
	cout <<  input.birthyear << endl;
	cout << "Presents Wanted : " << input.numofpres << endl;
	cout << "Money Available : " << input.moneyavail << endl;
}


Last edited on
I kinda need to know how you expect char to int to work to give you an exact answer, because you can do
1
2
char letter = 'A';
int number = letter;

how ever the variable number will equal 65.
1
2
int number = 65;
char letter = number;

doing this will make letter equal 'A'.

wish to read up on the ascii tables this maybe a good start http://en.wikipedia.org/wiki/ASCII

also char* to int works as will as char* to char, i bet char* to int* will work much better for you.
Last edited on
OP wants to know how to interpret an array of chars as a string representing a number in the decimal numbering system, not how to treat an array of chars as an array of numbers.
in that case something like this may work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
#include <math.h>

int CharToInt(char *cstring){
	int length = strlen(cstring);
	int CharAsInt = 0;
	for(int i=0;i<=length;i++)
		CharAsInt += pow(256,length-i-1)*cstring[i];
	return CharAsInt;
}

int main(int argc, char **argv){
	fprintf(stdout,"%i",CharToInt("test"));
	return 1;
}

though this will not work for sentences.
Are you doing it on purpose? I'm seriously asking, because I have a hard time finding a single line there that I can't complain about.

Lines 1-3: Poor style. Should use C++ headers.
Line 5: Parameter should be const char *.
Line 6: Redundant array traversal.
Line 8: Relational operator should be <.
Line 9: pow() used in an integer-only operation.
Line 14: Implicit cast drops constness.
Line 14: fprintf() is being used.
Line 14: Poor style. fprintf() is being used with stdout.

How about
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <sstream>
#include <algorithm>

int to_age(const std::string &s){
    std::stringstream stream(s.substr(0, std::min(s.size(), 2)));
    int ret;
    if (!(stream >> ret))
        // Conversion failed, return nonsensical value.
        return -1;
    return ret;
}
Last edited on
To convert a character string to an integer, you could use atoi(). Alternatively, since this is C++, use a stringstream.

Here's an example - it's very primitive, you could add additional checks to see for example that day is in the range 1 to 31 etc.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <windows.h> // Needed to set cursor positions
#include <string>
#include <sstream>

using namespace std;

struct Birthday
{
	string name;
	char birthday[11];
	int dd;
	int mm;
	int yyyy;

	int result;
	int numofpres;
	int moneyavail;
};

void placeCursor(HANDLE, int, int); // Function prototypes
void displayPrompts(HANDLE);
void getUserInput(HANDLE, Birthday&);
void displayData(HANDLE, Birthday);

int main()
{
	Birthday input;

	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

	displayPrompts(screen);
	getUserInput(screen, input);
	displayData(screen, input);
	system("pause >nul");
	return 0;
}

void placeCursor(HANDLE screen, int row, int col)
{ 
                        // COORD is a defined C++ structure that
	COORD position; // holds a pair of X and Y coordinates
	position.Y = row;
	position.X = col;
	SetConsoleCursorPosition(screen, position);
}

void displayPrompts(HANDLE screen)
{
	placeCursor(screen, 3, 25);
	cout << "******* Birthday Form *******" << endl;
	placeCursor(screen, 5, 25);
	cout << "Name: " << endl;
	placeCursor(screen, 7, 25);
	cout << "Birthday: dd mm yyyy" << endl;
	placeCursor(screen, 9, 25);
	cout << "How many presents do you want?: " << endl;
	placeCursor(screen, 11, 25);
	cout << "How much money can you spend on your party?: " << endl;
}

void getUserInput(HANDLE screen, Birthday &input)
{
    placeCursor(screen, 5, 31);
    getline(cin, input.name);

    bool ok = false;
    do {
        placeCursor(screen, 7, 25);
        cout << "Birthday: dd mm yyyy" << endl;

    	placeCursor(screen, 7, 35);
    	cin.getline(input.birthday, sizeof(input.birthday));

        istringstream ss(input.birthday);
        ss >> input.dd >> input.mm >> input.yyyy;
        ok = ss;
    } while (!ok);


	placeCursor(screen, 9, 57);
	cin >> input.numofpres;
	placeCursor(screen, 11, 70);
	cin >> input.moneyavail;
}

void displayData(HANDLE screen, Birthday input)
{
	placeCursor(screen, 15, 0);
	cout << "Here is the data you entered.\n" << endl;
	cout << "Name : " << input.name << endl;
	cout << "Birthday : " << input.dd << '/'
                          << input.mm << '/'
                          << input.yyyy << endl;
	cout << "Presents Wanted : " << input.numofpres << endl;
	cout << "Money Available : " << input.moneyavail << endl;
}
@helios
Lines 1-3: I had assumed #include <stdio.h> with c++ while #include <cstdio> was c.
Line 5: I had assumed by using const char* would use more memory than char*.
Line 6: It may have been redundant but I saw it as being the easiest way to finish the function without much thought, and have it do as I thought he wanted.
Line 8: agreed, didn't think about it too much and didn't notice it was wrong with the last loop because it keeps adding 0.
Line 9: not sure how else i'm going to use powers.
Line 14.0: no idea what an "Implicit cast drops constness" is.
Line 14.1: unsure what is wrong with using fprintf().
Line 14.2: prefer printf()?
Thank you everyone for your help! :)
Topic archived. No new replies allowed.