How can I read in numbers and output them individually?

closed account (28UMizwU)
???
Last edited on
You treat them as individual characters, or treat them collectively as a string.

Then you process each character at a time:
1. convert it to a number
2. print the character (or number)
3. use the number to determine how many * to print
4. write the new line character
here's a hint:


First use While loop (what should be the condition?)
while (temp ...){ // you have to iterate until temp = temp / 10 does not leave behind any quotient

Use a temporary variable and assign number to it (don't think it's necessary in this case but why not?
temp = number;

Use the following logic:
digit = temp % 10; // gets the last digit which would be 2 in your example
temp = temp / 10; // makes sure 2 is omitted for next iteration


cout << digit;
for() // what should be here?
cout<<'*';
cout<<'\n';


Try fixing the missing bits and let us know if you didn't get it ;)
closed account (28UMizwU)
This is what I have so far. I didn't really understand what you guys were saying, so I just brute forced it I think. However I need to output a "?" when a non number is entered as well as the non number itself.
for example
Input:11f9
Output
9 *********
f ?
1 *
1 *
I don't know how to do this because of the way i did it.


#include <iostream>

using namespace std;


int main(int argc, char** argv) {
//Declare all Variables Here
unsigned short usrnum, num, first, second, third, last;

//Input or initialize values Here
cout<<"Create a histogram chart."<<endl;
cout<<"Input 4 digits as characters."<<endl;
cin>>usrnum;

//Histogram Here
first=usrnum/1000;
last=usrnum%10;
third=usrnum%100-last;
second=usrnum%1000-third-last;

switch (last){
case 0:
cout<<"0"<<endl;
break;
case 1:
cout<<"1 *"<<endl;
break;
case 2:
cout<<"2 **"<<endl;
break;
case 3:
cout<<"3 ***"<<endl;
break;
case 4:
cout<<"4 ****"<<endl;
break;
case 5:
cout<<"5 *****"<<endl;
break;
case 6:
cout<<"6 ******"<<endl;
break;
case 7:
cout<<"7 *******"<<endl;
break;
case 8:
cout<<"8 ********"<<endl;
break;
case 9:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (third){
case 0:
cout<<"0"<<endl;
break;
case 10:
cout<<"1 *"<<endl;
break;
case 20:
cout<<"2 **"<<endl;
break;
case 30:
cout<<"3 ***"<<endl;
break;
case 40:
cout<<"4 ****"<<endl;
break;
case 50:
cout<<"5 *****"<<endl;
break;
case 60:
cout<<"6 ******"<<endl;
break;
case 70:
cout<<"7 *******"<<endl;
break;
case 80:
cout<<"8 ********"<<endl;
break;
case 90:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (second){
case 0:
cout<<"0"<<endl;
break;
case 100:
cout<<"1 *"<<endl;
break;
case 200:
cout<<"2 **"<<endl;
break;
case 300:
cout<<"3 ***"<<endl;
break;
case 400:
cout<<"4 ****"<<endl;
break;
case 500:
cout<<"5 *****"<<endl;
break;
case 600:
cout<<"6 ******"<<endl;
break;
case 700:
cout<<"7 *******"<<endl;
break;
case 800:
cout<<"8 ********"<<endl;
break;
case 900:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (first){
case 0:
cout<<"0"<<endl;
break;
case 1:
cout<<"1 *"<<endl;
break;
case 2:
cout<<"2 **"<<endl;
break;
case 3:
cout<<"3 ***"<<endl;
break;
case 4:
cout<<"4 ****"<<endl;
break;
case 5:
cout<<"5 *****"<<endl;
break;
case 6:
cout<<"6 ******"<<endl;
break;
case 7:
cout<<"7 *******"<<endl;
break;
case 8:
cout<<"8 ********"<<endl;
break;
case 9:
cout<<"9 *********"<<endl;
break;
default:
cout<<" ?"<<endl;
break;
}
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main()
{
    int number ;
    std::cout << "enter a positive number: " ;
    std::cin >> number ;

    if( number > 0 ) // sanity check
    {
        // for each digit in the number, from right to left
        while( number > 0 )
        {
            const int digit = number%10 ; // right most digit
            number /= 10 ; // the number after removing the right most digit

            std::cout << digit << ' ' ; // print the digit

            // print as many asterisks as the value of the digit
            for( int i = 0 ; i < digit ; ++i ) std::cout << '*' ;

            std::cout << '\n' ; // and a new line at the end
        }
    }
}
closed account (28UMizwU)
sorry, but I don't really understand how yours works. Could you explain how you get it to display all of the digits. Also how would I display a "s ?" if s was entered and if 0 is entered how could I make that display? I tried adding another if statement as well as a while but had no luck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <sstream>
#include <string>


int main()
{
    std::string number ;
    std::cout << "enter a positive number: " ;
    std::cin >> number ;
    
    std::stringstream sstr(number);
    int zahl;
    sstr >> zahl;


    if( zahl > 0 ) // sanity check
    {
        // for each digit in the number, from right to left
        while( zahl > 0 )
        {
            const int digit = zahl%10 ; // right most digit
            zahl /= 10 ; // the number after removing the right most digit

            std::cout << digit << ' ' ; // print the digit

            // print as many asterisks as the value of the digit
            for( int i = 0 ; i < digit ; ++i ) std::cout << '*' ;

            std::cout << '\n' ; // and a new line at the end
        }
    }
    else
    {
        std::cout << number << "?" << std::endl;
    }
}


I added the code of JLBorges with a stringstream.
You need a string to read numbers and letters, if you want to get the number you have to convert the string to an int, thats why i'm using this stringstream
> Could you explain how you get it to display all of the digits.

5432%10 == 2 (the rightmost digit)
5432/10 == 534 (the remaining digits)

543%10 == 3 (the next rightmost digit)
543/10 == 54 (the remaining two digits)

etc.


> Also how would I display a "s ?" if s was entered

To also accept non-digit characters, read the input into a string.
https://cal-linux.com/tutorials/strings.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string str ;
    std::cout << "enter some alphanumeric characters: " ;
    std::cin >> str ;

    const int nchars = str.size() ; // number of characters in the string

    // for each character in the string, from right to left
    // note: the rightmost character is at position nchars-1
    for( int i = nchars-1 ; i >= 0 ; --i )
    {
        const char ch = str[i] ; // get the character

        if( std::isdigit(ch) ) // if it is a decimal digit
        {
            const int digit = ch - '0' ; //  '6' - '0' == 0 etc.

            std::cout << digit << ' ' ; // print the digit

            // print as many asterisks as the value of the digit
            // note that if digit == 0, no asterisks are printed
            for( int j = 0 ; j < digit ; ++j ) std::cout << '*' ;

            std::cout << '\n' ; // and a new line at the end
        }

        else if( std::isalpha(ch) ) // if it is a letter
        {
            std::cout << ch << " ?\n" ; // print the letter and a question mark
        }
    }
}
If you want the output in reverse order then swap lines 9 and 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

void plotDigits( unsigned long long N )
{
   if ( !N ) return;
   int digit = N % 10;
   plotDigits( N / 10 );
   cout << digit << " " << string( digit, '*' ) << '\n';
}

int main()
{
   plotDigits( 135792468 );
}


1 *
3 ***
5 *****
7 *******
9 *********
2 **
4 ****
6 ******
8 ********
Last edited on
Topic archived. No new replies allowed.