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."<

User SGal
by
6.8k 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

User Ryan Knight
by
7.9k points