asked 155k views
5 votes
Read a 4 character number. Output the result in in the following format, Input 9873, Output 3 *** 7 ******* 8 ******** 9 ********* If one of the numbers is not a digit, then put a ? mark Input = 98a3, Output 3 *** a ? 8 ******** 9 ********* Prompt the user before the input with, cout<<"Create a histogram chart."<

asked
User SGal
by
7.6k points

1 Answer

4 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

char numbers[4];

cout<<"Create a histogram chart."<<endl;

cout<<"Enter your number as 4 characters: ";

cin >> numbers;

for (int i=3; i>=0; i--) {

int ascii_value = numbers[i];

int number = numbers[i] - '0';

if (ascii_value >=48 && ascii_value <=57) {

cout << number;

for (int j=number-1; j>=0; j--) {

cout << "*";

}

}

else {

cout << "a ?";

}

cout << endl;

}

}



Step-by-step explanation:

Declare a character array with length 4,

Ask user to enter values,

Create a nested for loop; first loop holds the values and their ASCII values for each character,

Check the ASCII values for each character, if they are between 48 and 57, that means they are numbers. In this case, print the number and go to the inner loop and print the stars accordingly,

If ASCII values are not in the range, print a ?,

Go to the new line after each character

answered
User Ryan Knight
by
8.8k points

Related questions

asked Mar 27, 2017 214k views
Innerpeacer asked Mar 27, 2017
by Innerpeacer
7.9k points
1 answer
3 votes
214k views
asked Oct 17, 2021 3.2k views
Kunalbhat asked Oct 17, 2021
by Kunalbhat
7.4k points
1 answer
0 votes
3.2k views