In: Computer Science
C++ code won't run. Fix?
//==========================================================
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose
functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to
"cout"
//Converting hexadecimal to binary
int main()
{
char binarynum[65], hexa[17];
//Using long int because it has greater capacity
long int i = 0;
printf("Enter the hex value that needs to convert
to binary: ");
scanf_s("%s", hexa);
printf("\n The converted hex value in binary is:
");
while (hexa[i])
{
//These are the different cases for
the binary that will be converted from hex. Case is the hex code
and printf is the binary output.
//It scans the inputs and then
compares it to switch case that is below.
switch (hexa[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'F':
printf("1111");
break;
case 'a':
printf("1010");
break;
case 'b':
printf("1011");
break;
case 'c':
printf("1100");
break;
case 'd':
printf("1101");
break;
case 'e':
printf("1110");
break;
case 'f':
printf("1111");
break;
//
default:
printf("\n
Invalid hexa digit entered ", hexa[i]);
return 0;
}
i++;
}
cout << "Press any key to
exit ..." << endl;
_getch();
}
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose
functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to
"cout"
//Converting hexadecimal to binary
int main()
{
char binarynum[65], hexa[17];
//Using long int because it has greater capacity
long int i = 0;
printf("Enter the hex value that needs to convert to binary:
");
scanf("%s", hexa); // Earlier scanf_s() was
written which is incorrect as there is no method like scanf_s(),
here scanf() is //used for input from console
printf("\n The converted hex value in binary is: ");
while (hexa[i])
{
//These are the different cases for the binary that will be
converted from hex. Case is the hex code and printf is the binary
output.
//It scans the inputs and then compares it to switch case that is
below.
switch (hexa[i])
{
case '0':
printf("0000"); break;
case '1':
printf("0001"); break;
case '2':
printf("0010"); break;
case '3':
printf("0011"); break;
case '4':
printf("0100"); break;
case '5':
printf("0101"); break;
case '6':
printf("0110"); break;
case '7':
printf("0111"); break;
case '8':
printf("1000"); break;
case '9':
printf("1001"); break;
case 'A':
printf("1010"); break;
case 'B':
printf("1011"); break;
case 'C':
printf("1100"); break;
case 'D':
printf("1101"); break;
case 'E':
printf("1110"); break;
case 'F':
printf("1111"); break;
case 'a':
printf("1010"); break;
case 'b':
printf("1011"); break;
case 'c':
printf("1100"); break;
case 'd':
printf("1101"); break;
case 'e':
printf("1110"); break;
case 'f':
printf("1111"); break;
//
default:
printf("\n Invalid hexa digit entered %d ",
hexa[i]);
// '%d' was missing in previous code, which is required to print
hexa[i]
return 0;
}
i++;
}
cout << "\nPress any key to exit ..." << endl;
getch(); // there is no method like _getch(),
which was there in previous code which is incorrect.
}
OUTPUT: