Need Help w/ the itoa Object

Well... I was practicing on my C++ skills and I need some assistance with the itoa object. First of all, here is my code; Side Note: This code is not finished yet due to that fact I wanted to test it before I continued:
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Plain Practice for Myself ~ PopEye */

#include <iostream>
#include <stdlib.h>

using namespace std;

struct Students
{
	char Name [80], Grade [2];
	short testScore [3];
};

Students*worms1 = NULL;

void changeSettings (void);
void assignCharacteristics (short);
short MAX = NULL;

int main (void)
{
	short amountStudents;

	cout << "Enter # of students: ";
	cin >> amountStudents;
	cin.ignore ();

	cout << "Amount of students = " << amountStudents << endl << endl;

	MAX = amountStudents;
	assignCharacteristics (amountStudents);
	changeSettings ();

	return 0;
}

void assignCharacteristics (short totalStudents)
{
	Students*worms = new Students [totalStudents];
	worms1 = worms;
	
	for (short x = 0; x < MAX; x++)
	{
		cout << "Student #" << x + 1 << "'s name is: ";
		cin.getline (worms [x].Name, 80);
		cout << endl;
		cout << worms [x].Name << ";" << endl;
		
		for (short y = 0; y < 3; y++)
		{
			cout << "Test score #" << y + 1 << ": ";
			cin >> worms [y].testScore [y];
			cin.ignore ();

			if (worms [y].testScore [y] > 100)
				worms [y].testScore [y] = 100;
			else if (worms [y].testScore [y] < 0)
				worms [y].testScore [y] = 0;
		}

		cout << endl;
	}
}
void changeSettings (void)
{
	char Choice;
	bool Continue = false;

	do
	{
		cout << "*****MENU*****" << endl;
		cout << "1 - Change Names" << endl;
		cout << "2 - Change Grades" << endl;
		cout << "3 - Continue" << endl;
		cout << "Default - Continue" << endl;
		cout << "Choice: ";
		cin.get (Choice);

		if (! (Choice == '\n'))
			cin.ignore ();

		cout << endl;
		
		switch (Choice)
		{
		case '1':
			cout << "**'Change Names' selected**" << endl;
			cout << "Choose 1 Student from below:" << endl;
			
			for (short x = 0; x < MAX; x++)
				cout << x + 1 << " - " << worms1 [x].Name << endl;

			cout << "Choice: ";
			cin.get (Choice);

			if (! (Choice == '\n'))
				cin.ignore ();

			char*numLetter = NULL;

			for (short x = 0; x < MAX; x++)
			{
				itoa (x + 1, numLetter, 10);

				if (Choice == *numLetter)
				{
					cout << "'" << worms1 [x].Name << "' selected" << endl;
					cout << "Type new name for this student: ";
					cin.getline (worms1 [x].Name, 80);
					cout << "New Name for student #" << x << "is " << worms1 [x].Name << endl;
				}
			}

			delete [] numLetter;
			break;
		}
	}
	while (Continue != true);
}


Now... the actual problem problem occurs here; the last part of the changeSettings function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char*numLetter = NULL;

			for (short x = 0; x < MAX; x++)
			{
				itoa (x + 1, numLetter, 10);

				if (Choice == *numLetter)
				{
					cout << "'" << worms1 [x].Name << "' selected" << endl;
					cout << "Type new name for this student: ";
					cin.getline (worms1 [x].Name, 80);
					cout << "New Name for student #" << x << "is " << worms1 [x].Name << endl;
				}
			}


From seeing the code above you can tell I was trying to use the itoa object to let the 'for loop' know what student I chose. Since there's no way of knowing how many students were actually going to be inputted I had no other choice but to use a 'for loop'. Does the section of the code look correct? I can't seem to figure out the problem. If you have any questions related to the code then ask away. Thank you.
I do not thing that C++ has "itoa" ... here is a link that could help
http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
@TAZO

Ugh! I just tried 'itoa' on a plain new project and that seems to be causing the crash. 'itoa' is there but crashes the program. Anyone know another way to convert a value (number) into a ASCII?
so what are you trying to do exactly in your for loop?
Last edited on
@TAZO

In the loop, I'm trying to tell char Choice; what I selected. The reason I'm doing it in a loop is because there is no way of telling exactly how many students the user will input. So... it will say:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (Choice)
		{
		case '1':
			cout << "**'Change Names' selected**" << endl;
			cout << "Choose 1 Student from below:" << endl;
			
			for (short x = 0; x < MAX; x++)
				cout << x + 1 << " - " << worms1 [x].Name << endl;

			cout << "Choice: ";
			cin.get (Choice);

			if (! (Choice == '\n'))
				cin.ignore ();

The code above will display all the students inputted. Then it helps you out by telling you what number corresponds to them. After inputting the number in char Choice, it's pretty hard to determine which student is which since you can't determine how many students will be present from the beginning of the program. This is where the loop comes in:
1
2
3
4
5
6
7
8
9
10
11
12
13
char*numLetter = NULL;

			for (short x = 0; x < MAX; x++)
			{
				itoa (x + 1, numLetter, 10);

				if (Choice == *numLetter)
				{
					cout << "'" << worms1 [x].Name << "' selected" << endl;
					cout << "Type new name for this student: ";
					cin.getline (worms1 [x].Name, 80);
					cout << "New Name for student #" << x << "is " << worms1 [x].Name << endl;
				}

The reason for creating a constant pointer is because the itoa object needs one of its arguments to be a constant holder. Now... the loop starts. itoa (x + 1, numLetter, 10)... in this case, x = 0. So... 1 will be inputted into the pointer in binary mode. So... then if numLetter == Choice, it'll be able to change the name of the student I selected before the loop. The loop continues until it finds the correct number. The only problem is... when the itoa section starts, the program crashes. I couldn't get past this part because somehow itoa is incompatible with C++. Is there a different method or am I missing something?
Last edited on
Well, first: itoa is not an object. It's a function.

Second: itoa() expects a buffer that's large enough to store the resulting string. But you're providing just a null pointer.

The declaration of 'numLetter' is supposed to look like that: char numLetter[20];
why don't you use your x to point out the right selection.

make choice a short , once your program outputs the all the students with corresponding #'s, the user will input a number which gets stored in choice and your change function will look like something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
short x = 0;
case '1':
    cout << "**'Change Names' selected**" << endl;
    cout << "Choose 1 Student from below:" << endl;
    for (; x < MAX; x++)
	cout << x + 1 << " - " << worms1 [x].Name << endl;

    cout << "Choice: ";
    cin.get (Choice);
    if (Choice < x+1){
    	cout << "'" << worms1 [Choice - 1].Name << "' selected" << endl;
	cout << "Type new name for this student: ";
	cin.getline (worms1 [Choice - 1].Name, 80);
	cout << "New Name for student #" << x << "is " << worms1 [Choice - 1].Name << endl;
    }
    break;
@coder777

I tried your method but the second argument in itoa needs to be a pointer.

@Tazo

I also tried your method but it seems that a char cannot equal a short.

After some snooping around on the cplusplus.comwebsite, I came across sprintf. After skimming the way the function is suppose to look like, I created a function that applies characters into my array. Check it out:
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
void assigncsFunction (char []);
void assignCharacteristics (short);
void changeSettings (void);
short MAX = NULL;
char*csFunctionPtr = NULL;

int main (void)
{
	short amountStudents;

	cout << "Enter # of students: ";
	cin >> amountStudents;
	cin.ignore ();

	cout << "Amount of students = " << amountStudents << endl << endl;

	char*csFunction = new char [amountStudents];
	csFunctionPtr = csFunction;
	MAX = amountStudents;

	assigncsFunction (csFunction);
	assignCharacteristics (amountStudents);
	changeSettings ();

        delete [] csFunction;

	return 0;
}

I added the csFunction and the global pointer, csFunctionPtr. Now the assigncsFunction is where it will apply the number characters ('1', '2', etc.) into the csFunction array:
1
2
3
4
5
void assigncsFunction (char function [])
{
	for (short x = 0; x < MAX; x++)
		function [x] = sprintf (function, "%d", (x + 1));
}

Now... this program compiles good with 2 warnings that basically say that "sprintf is not safe". I get this every time I use something from C. Anyways, there is a problem... When it comes up to let the char Choice; know which student I picked:
1
2
3
4
5
6
7
8
9
10
for (short x = 0; x < MAX; x++)
			{
				if (Choice == (*csFunctionPtr + x))
				{
					cout << "'" << worms1 [x].Name << "' selected" << endl;
					cout << "Type new name for this student: ";
					cin.getline (worms1 [x].Name, 80);
					cout << "New Name for student #" << x + 1 << " is " << worms1 [x].Name << endl;
				}
			}

It seems that only pressing 2 works. To make this a little bit more clear, here is what I mean:
1
2
3
4
5
6
7
8
If I press 2 for char Choice;
The loop will decide that it's for the 1st student when I want it to be for the 2nd

If I press 1 for char Choice;
The loop will ignore it and go back to the main menu when I want it to be for the 1st student

If I press 3 for char Choice;
The loop will ignore it and go back to the main menu when I want it to be for the 3rd student 

Any help? Is there a different way of doing this? This is all from 1 book I read so I don't know the majority of the codes yet. Thanks.
Im sorry, i meant to change CHOICE from char to short.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
short Choice= 0; // change choice to short.
short x = 0;
case '1':
    cout << "**'Change Names' selected**" << endl;
    cout << "Choose 1 Student from below:" << endl;
    for (; x < MAX; x++)
	cout << x + 1 << " - " << worms1 [x].Name << endl;

    cout << "Choice: ";
    cin >> Choice;
    if (Choice < x+1){
    	cout << "'" << worms1 [Choice - 1].Name << "' selected" << endl;
	cout << "Type new name for this student: ";
	cin.getline (worms1 [Choice - 1].Name, 80);
	cout << "New Name for student #" << x << "is " << worms1 [Choice - 1].Name << endl;
    }
    break;

this should work with selecting the right student that the user chooses
Last edited on
@TAZO

Thanks man. This method seems to be working properly now. But I'm staying with the loop. I understand it better.
Topic archived. No new replies allowed.