Write a program which will accept two character inputs. You will then print the
ID: 3677215 • Letter: W
Question
Write a program which will accept two character inputs. You will then print the characters as shown below, from the first character you typed to the last character you typed, including all characters in between. The program will then produce a table with 4 columns formatted as shown in the table below:
Enter a character: A
Enter a second character: H
Character Decimal Octal Hexadecimal
A 65 101 41
B 66 102 42
C 67 103 43
D 68 104 44
E 69 105 45
F 70 106 46
G 71 107 47
H 72 108 48
The user may enter anything he/she can type in any order. You must program for them typing 9, 1 as well as 1, 9. You must use the static_cast operator for this program. Failure to do so will result in a 10 point deduction. Chapter 15 will be helpful for this exercise. You must use setw or other stream manipulators to format the output. Do not use or blank spaces to get the right spacing. Change the format to print to the screen the format of the number being printed – the decimal, octal or hexadecimal format. This is also in Chapter 15. 2 After that works, TO ADD: Use an outer for loop (making nested loops. The loop and all the code you have written for the ascii sequence program to produce the table will be inside of this for loop. You can develop the first program with the table loop first (incremental development)). Allow the user to enter the number of character sets he wants to printout and use that number to determine how many times the for loop will execute making the outer loop a count controlled loop. Remember that the user is just asked to enter two characters, the first and last character and that you must account for them entering the characters in either order.
Explanation / Answer
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
char first;
char second;
cout<<"Enter a character: ";
cin>>first;
cout<<"Enter a second character: ";
cin>>second;
// if second < first then swap
if(second < first){
char temp = first;
first = second;
second = temp;
}
int diff = second - first;
cout <<"Character"<<setw(15)<<"Decimal"<<setw(18) <<"Octal"<<
setw(20)<<"Hexadecimal"<<endl;
for(int i=0; i<=diff; i++){
cout <<char(first+i)<<setw(20)<<dec<<(first+i)<<setw(20) <<oct<<(first+i)<<
setw(20)<<hex<<(first+i)<<endl;
}
return 0;
}
/*
Output:
Enter a character: 1
Enter a second character: 9
Character Decimal Octal Hexadecimal
1 49 61 31
2 50 62 32
3 51 63 33
4 52 64 34
5 53 65 35
6 54 66 36
7 55 67 37
8 56 70 38
9 57 71 39
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.