Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8binConv.cpp (in C++) This program will take 1 command-line input in the form of

ID: 3834055 • Letter: 8

Question

8binConv.cpp (in C++)

This program will take 1 command-line input in the form of an 8-bit number. It will convert that 8-bit BINARY number into a DECIMAL number. For example, if the user runs the program with a command-line argument of 00000101, the result is the decimal number 5.

If the user enters no command-line inputs, then the program will print out an error message. If the user enters more than 1 command-line input, the program will print out that same error message. If the user enters an input that is NOT comprised of an 8-bit binary number, the program will print out that same error message (the program only needs to check the length of that input, not whether the user actually put in a proper binary number).

See example print out (note that the first 2 entries are purposeful mistakes and generate the error message):

You will need to include cstlib, cstring, and cmath (in addition to iostream, of course) libraries.

Some clues that will come in handy:

Recall that argv[x] are strings of characters, more commonly known as C-strings. If you have a C-string, cst, the command strlen(cst) returns the LENGTH of that C-string.

To examine a character in a C-string, say cst, you can use indexing of the C-string, for example, if str=”Hello”, then str[0]=’h’, str[1]=’e’, and so on. Indexing in C/C++ always starts at zero.

To convert a binary number into decimal, you can use the “positional notation” algorithm as illustrated by the example here:

Explanation / Answer

#include <iostream>

#include <stdlib.h>

#include <string.h>

#include <math.h>

using namespace std;

int main(int argc,char *argv[])

{

                int i,Decimal=0;

                char *str = (char *)malloc(8);

              if(argc == 1){

                cout<<"No arguments.You should run this program in terminal with several arguments."<<endl;

                exit(1);

                }

              else{ /*for loop,each loop print a argument once a time.Note that the loop begin with argv[1],

    because argv[0] represent the program's name.*/

   

                if(argc>2){

                                cout<<" Usage: 8binConv <8-bit binary number>";

                                }

                              else{

                                                strcpy(str,argv[1]);

                                              if(strlen(str)>8){

                                                              cout<<" Usage: 8binConv <8-bit binary number>";      

                                              }

                                              else{

                                                              int powerval=7;

                                                              for(i=0;i<strlen(str);i++)               

                                                              {

                                                                              char ch=str[i];

                                                                              if(ch=='1'||ch=='0'){

                                                                                              if (str[i]=='1'){

                                                                                                              Decimal=Decimal + (pow(2,powerval));

                                                                                              }

                                                                                              powerval=powerval-1;

                                                                              }

                                                                              else{

                                                                                              cout<<" In binary only 0 and 1 is allowed";

                                                                                              break;

                                                                              }

                                                                              if(i==7){

                                                                                              cout<<" That's "<<Decimal<<" in decimal.";

                                                                                }

                                                                }                             

                                                }

                                }

                }

                return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote