Console (display window) stops working after entering a value.

Good day, people!

I'm working on a homework/project on C++ on conversion of decimal (base-10) values to hexadecimal values. Right now, I have 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream.h>
int main ()
{	int n, p, y, h;
	char r;
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl; // Banner
	cout << "   <<<<<<    <<< DECIMAL-HEXADECIMAL CONVERTER <<<    <<<<<<  " << endl;
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl;
	cout << "    This machine converts decimal (base-10) values into hexa-" << endl; // Introduction
	cout << "  decimal (base-16) values." << endl;
	cout << "    The Hexadecimal numeral system with a base of 16 (compare" << endl;
	cout << "  it with the common Decimal numeral system of base 10).  The" << endl;
	cout << "  HEX has broad applications, especially in programming.  The" << endl;
	cout << "  'digits' used in HEX are the numbers 0-9, A, B, C, D, E and" << endl;
	cout << "  F." << endl << endl;
	do // Program loop starts
	{	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl;
		cout << "  Enter a value that you want to convert to HEX:  ";
		cin >> n; // User enters a value
		cout << endl;
		cout << "  The HEX of " << n << " is ";
		if (n<0) // In case user enters a negative value
		{	n = -1*n;
			cout << "-";}
		p = -1;
		y = 1;
		while (y<n)
		{	p++;
			y = y*16;}
		do
		{	h = 0;
			if (p<n)
				do
				{	n = n - p;
					h++;}
				while (p<n);
			switch (h) // Converting h into a HEX value...
			{	case 10:
					cout << "A";
					break;
				case 11:
					cout << "B";
					break;
				case 12:
					cout << "C";
					break;
				case 13:
					cout << "D";
					break;
				case 14:
					cout << "E";
					break;
				case 15:
					cout << "F";
					break;
				default:
					cout << h;
					break;}
			p--;}
		while (p>=0);
		cout << "." << endl << endl;
		do // Program-loop confirmation
		{	cout << "  Do you wish to repeat the program?n  Y - for Yesttt|tN - for Non  ";
			cin >> r;
			cout << endl;
			if (!(r=='y'||r=='Y'||r=='n'||r=='N'))
				cout << "a  Invalid response!!!  oTL" << endl;}
		while (!(r=='y'||r=='Y'||r=='n'||r=='N'));}
	while (r=='y'||r=='Y');
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl; // Termination
	cout << "  Thank you for using our program!  :Dnn  ";
	return 0;}


I have already compiled and built this code (and my compiler (MVSC 98. Yah. 98.) said that the code had no errors or faults. I executed the program:


>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>
   <<<<<<    <<< DECIMAL-HEXADECIMAL CONVERTER <<<    <<<<<<
>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

    This machine converts decimal (base-10) values into hexa-
  decimal (base-16) values.
    The Hexadecimal numeral system with a base of 16 (compare
  it with the common Decimal numeral system of base 10).  The
  HEX has broad applications, especially in programming.  The
  'digits' used in HEX are the numbers 0-9, A, B, C, D, E and
  F.

>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

  Enter a value that you want to convert to HEX:

(fine so far)

Then I entered a value to convert (variable n) [I entered 15] and this is what I got:

>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>
   <<<<<<    <<< DECIMAL-HEXADECIMAL CONVERTER <<<    <<<<<<
>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

    This machine converts decimal (base-10) values into hexa-
  decimal (base-16) values.
    The Hexadecimal numeral system with a base of 16 (compare
  it with the common Decimal numeral system of base 10).  The
  HEX has broad applications, especially in programming.  The
  'digits' used in HEX are the numbers 0-9, A, B, C, D, E and
  F.

>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

  Enter a value that you want to convert to HEX:  15


(Just that.)

I checked the code for any fault in it but (to my knowledge) I can't find one.

Why does the console 'hang' after I entered a value? Could this be a run-time error? How can I fix it?

---

That would be all. Thank you for reading my query. I'm enthusiastically waiting for your reply(ies)! :D
closed account (D80DSL3A)
You don't even get the output from line 20?

I see a problem with the case you are using (n=15)
Starting at line 24:
1
2
3
4
5
p = -1;// why -1? Is this supposed to count hex digits?
y = 1;
while (y<n)
{	p++;
	y = y*16;}


I think that will exit after 1 iteration with y=16 and p=0.
Next, from line 31:
1
2
3
4
5
if (p<n)// true because 0 < 15
	do
	{	n = n - p;// with p=0 the value of n will not change
		h++;}
	while (p<n);// this condition won't be met so the loop is "infinite" 


There may be other problems (I'm having trouble understanding your program as a whole due to variables with meaningless names and no explanatory comments about the code).
Try fixing that issue and see what happens.
Thanks for the response! :D

Ya. That actually was the one thing that triggered me to ask it here. XD.

With regards to the identifiers:

n = is supposed to be the number to be converted into HEX
p = is supposed to be the pth digit from the 'units' place of the HEX equivalent.
y = is supposed to be the value of 16^p (my intention was to subtract y from n several times until I arrive at the state where y is greater than n, giving the program the cue that it should proceed to calculate the next HEX digit)
h = is the HEX digit at p.

---

I just realized that I overlooked at the statements from (31). Thanks for cuing me that! :D Originally I am supposed to use a function for this and at some point I trashed that idea and came up with that code. (And yes, I forgot to modify that part... *facepalm*).

[And as of this typing I just realized that I forgot to include the part where I should reduce the values of p and y to satisfy my goal. *doublefacepalm*]

Again, thanks for your answer. I'm modifying the code and am continuously rechecking the code. Thanks for the help!!! :D:D:D
Finally!!! XD

Just got my code fixed: [All editions are in bold:]

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
#include <iostream.h>
int main ()
{	int n, p, y, h;
	char r;
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl; // Banner
	cout << "   <<<<<<    <<< DECIMAL-HEXADECIMAL CONVERTER <<<    <<<<<<  " << endl;
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl;
	cout << "    This machine converts decimal (base-10) values into hexa-" << endl; // Introduction
	cout << "  decimal (base-16) values." << endl;
	cout << "    The Hexadecimal numeral system with a base of 16 (compare" << endl;
	cout << "  it with the common Decimal numeral system of base 10).  The" << endl;
	cout << "  HEX has broad applications, especially in programming.  The" << endl;
	cout << "  'digits' used in HEX are the numbers 0-9, A, B, C, D, E and" << endl;
	cout << "  F." << endl << endl;
	do // Program loop starts
	{	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl;
		cout << "  Enter a value that you want to convert to HEX:  ";
		cin >> n; // User enters a value
		cout << endl;
		cout << ">>>> The Hexadecimal equivalent of " << n << " is "; 
		if (n<0) // In case user enters a negative value
		{	n = -1*n;
		cout << "-";} // Negative sign is printed
		p = 0; // (p + 1) is the number of digits in the HEX equivalent;  It also represents the exponent in 16^p
		y = 1; // A value for 16^p
		while (y<=n) 
		{	p++;
			y = y*16;} //The loop will repeat until y>n.  After which...
		p--; // the values of p and y will be reduced accordingly.
		y = y/16;
		do
		{	h = 0;
			if (y<=n)
				do
				{	n = n - y; // Continuously subtracting y from n ...
					h++;} // to find a value for h which will be converted into a HEX digit
				while (y<=n);
			switch (h) // Converting h into a HEX value...
			{	case 10:
					cout << "A";
					break;
				case 11:
					cout << "B";
					break;
				case 12:
					cout << "C";
					break;
				case 13:
					cout << "D";
					break;
				case 14:
					cout << "E";
					break;
				case 15:
					cout << "F";
					break;
				default:
					cout << h;
					break;}
			y = y/16; // reducing the value of y to a lower power of 16
			p--;} // will let the program to proceed to compute for the next HEX digit
		while (p>=0);
		cout << "." << endl << endl;
		do // Program-loop confirmation
		{	cout << "  Do you wish to repeat the program?n  Y - for Yesttt|tN - for Non  ";
			cin >> r;
			cout << endl;
			if (!(r=='y'||r=='Y'||r=='n'||r=='N'))
				cout << "a  Invalid response!!!  oTL" << endl;}
		while (!(r=='y'||r=='Y'||r=='n'||r=='N'));}
	while (r=='y'||r=='Y');
	cout << ">>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>" << endl << endl; // Termination
	cout << "  Thank you for using our program!  :Dnn  ";
	return 0;}


It does not 'hang' when I tried to execute it and I got the output I expected.

>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>
   <<<<<<    <<< DECIMAL-HEXADECIMAL CONVERTER <<<    <<<<<<
>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

    This machine converts decimal (base-10) values into hexa-
  decimal (base-16) values.
    The Hexadecimal numeral system with a base of 16 (compare
  it with the common Decimal numeral system of base 10).  The
  HEX has broad applications, especially in programming.  The
  'digits' used in HEX are the numbers 0-9, A, B, C, D, E and
  F.

>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>>>    >>>>

  Enter a value that you want to convert to HEX:  15

>>>> The Hexadecimal equivalent of 15 is F.

  Do you wish to repeat the program?
  Y - for Yes                   |       N - for No


Thank you so much! Maraming salamat po sa inyo, fun2code!!!
Last edited on
closed account (D80DSL3A)
You're welcome. Glad you got it working.
Topic archived. No new replies allowed.