Having trouble with some code

I'm having trouble with this code. What case 9 is supposed to do is ask user for an index from a string and give out the ASCII integer representation of the character at the given index. While case 10 is suppose to give back instead of the default base of 10 prints out the hexadecimal representation of the character at the given index.

Here is how I have it in my main

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
const int SIZE = 80;
  char ca[SIZE];
  char * pc = ca;
  int fPrints = 0;
  int bPrints = 0;
  int lengthChecks = 0;

case '9':
	{
	int length = 0;
	GetStringLength(pc, &length);
	for(int i = 0; i < length; ++i)
	{

		int i_ascii = GetIntegerValueOfIndex(pc, i);

		std::cout << pc[i] << "=" << i_ascii << std::endl;
		}
	}
case '10':
	{
	int length = 0;
	GetStringLength(pc, &length);
	for(int i = 0; i < length; ++i)
	{
		std::cout << "HEX=";
		PrintHexValueAtIndex(pc, i);
		std::cout << std::endl;
	}
	}





int GetIntegerValueOfIndex(char * c, int index)
{
   int i = c[index];
   return i;
}
void PrintHexValueAtIndex(char * c, int index)
{
std :: cin >> index;
std :: cout << "HEX= " << index << (int)c;
}
Last edited on
What case 9 is supposed to do is ask user for an index from a string and give out the ASCII integer representation of the character at the given index. While case 10 is suppose to give back instead of the default base of 10 prints out the hexadecimal representation of the character at the given index.


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
case '9':
	{
	int length = 0;
	GetStringLength(pc, &length);
	for(int i = 0; i < length; ++i)
	{

		int i_ascii = GetIntegerValueOfIndex(pc, i);

		std::cout << pc[i] << "=" << i_ascii << std::endl;

                // get value of i from user
                // check that it is valid, or do that in the GetIntegerValueOfIndex function
                      // your code

		std::cout << pc[i] << "=" << GetIntegerValueOfIndex(pc, i) << "\n";
		}
	}


Does this work? I didn't test it.

Not sure why you were looping there.

Also, it is better style to have each case call a function, unless it is a one liner.

Variables like index should be unsigned, std::size_t is a fairly normal type to use.
Topic archived. No new replies allowed.