Question

In: Computer Science

C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For...

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();

   }

Solutions

Expert Solution

#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:


Related Solutions

fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib>...
fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib> #include <ctime> void insertion_sort(int array[], int size, int start); void heap_sort(int B[], int n); void build_max_heap(int B[], int n); void max_heapify(int B[], int i, int n); void quick_sort(int B[], int p, int r); int partition(int B[], int p, int r); int main() {    int m = 10, Nf = 20000, Ns = 1000, delta = 1000, A[m][Nf];    for (int i = 0;...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream>...
Convert this C++ to js/html and run in browser. ///////////////////////////////////////////////// #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is...
Why won't the program run the code after the first switch statement. It shows Press c...
Why won't the program run the code after the first switch statement. It shows Press c to continue or q to quit, but does not give the user the option to fill it in. Please help to run the code fully. This program is to find the area of shapes the user decides and must give them an option to quit or restart the program. Please provide the explanation and code in JAVA here. Thank you. import java.util.Scanner; import java.lang.Math;...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
Explain the multithreading issue in the following c++ code and fix it. (consider options such as...
Explain the multithreading issue in the following c++ code and fix it. (consider options such as mutex lock). #include <iostream> #include <thread> #include <string> #include <unistd.h> using namespace std; static int tickets = 32; static int sale = 0; class Seller { public: void Main() { for (;;) { if(tickets <= 0){ cout << "In Seller[" << ID <<"] sold out"<< endl; break; }else{ tickets--; sale++; usleep(1); cout << "In Seller[" << ID <<"]"<<sale<< endl; } } } int ID;...
q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5...
q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5 ints from the user //Using those 5 ints, it should construct a linked list of 5 elements //Then it prints the elements of the list using the PrintList function #include <stdio.h> struct Node{ int data; Node* next; }; int main(void){ struct Node first = {0, 0}; struct Node* second = {0, 0}; Node third = {0, 0}; struct Node fourth = {0, 0}; struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT