taking # as char, producing *

Hello. My problem is to take in a word character by character. With this, any digit (yes, taken in as a character as well) needs to output that many '*' symbols.

For example,

Inputting 4 would output:
****

The only issues I'm having is that, when reading a number as a character, it reports the ASCII value. I can only think of subtracting the user's input by 48 to get the actual value... I am unsure of how to code either section, since I don't know how to "read it". I don't think the assignment calls for just numbers 0 - 9 but I don't know how else to read numbers instead of ASCII values! I've tried static_cast, reading as a char, but nothing seems right. Thanks for any help!

WHAT I CANNOT USE: Arrays, pretty much anything other than if/else statements and loops. If it's an easy, C++ specific way to do it as a unique built-in function, I doubt I can use it.

Thanks again.

What I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool ifDigit(char digit)
{
//I know this isn't right, but I can't think of any other
//way to not read as an ASCII value. I think I need to
//be able to read any digit.

if (choice >= '0' && choice <= '9')
      return true;
else
      return false;
}
// Here is where you make the digit into the secret symbol, *
void secretDigit(char digital)
{
int count = 0;

//The problem here is that it outputs
//the amount in ASCII values.
while (count < choice)
{
count ++;
cout << "*";
}
}
Last edited on
you can subtract that 48 as a letter.
x = yourchar - '0'.
if x < 10
do the stars.
if not, it wasn't a digit.
there is an isdigit function but you said no built in.

or you can be a smart* and avoid the whole problem.
if(yourchar >='0' && yourchar <= '9')
for(char c = '0'; c <yourchar; c++)
cout << '*';

there are other cute ways but most involve things you could get in trouble for learning. Wow. There's a concept, punished for learning something.

oh, you can do this:
unsigned int x;
cin >> x;//reads a number, but it will not do well if user types in string and it will read 123 which is mulit digit. This may or may not be what you tried to ask.
it would not matter -- if they put in 123, you could write 123 * symbols. unclear if that is OK.
then the whole program is just
unsigned int x;
cin >> x;
if(x > 10) cout << "cut it out" << endl;
else
for(i = 0; i < x; i++)
cout << '*' ;


Last edited on
Can I only do that if it is less than 10? A question of mine would be, is there any way to code it so that it would accept ANY number and turn it into the stars?
"if(yourchar >='0' && yourchar <= '9')
for(char c = '0'; c <yourchar; c++)
cout << '*'; "

Seems to be the smartest and best method. So the idea is to use a for loop... It's crazy because I tried that earlier. I think I must have used the wrong relational operator.. Thanks so much!
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
#include <iostream>

void print_stars( int n ) // print n asterisks
{
    for( int i = 0 ; i < n ; ++i ) std::cout << '*' ;
    std::cout << '\n' ;
}

void fancy_print_char( char ch ) 
{
    if( ch >= '0' && ch <= '9' ) // if it is a digit
    {
        // print as many asterisks
        const int n = ch - '0' ; // '7' - '0' == 7 etc.
        print_stars(n) ;
    }

    else std::cout << ch << '\n' ; // otherwise print it as it is
}

int main()
{
    for( char c : { 'a', '0', 'b', '1', 'c', '2', 'd', '5', 'e', '7' } )
    {
        std::cout << c << " => " ;
        fancy_print_char(c) ;
    }
}

http://coliru.stacked-crooked.com/a/cfa686fcb0016cfe
Can I only do that if it is less than 10? A question of mine would be, is there any way to code it so that it would accept ANY number and turn it into the stars?

sure.
read in an integer and print that many, without any conditions, as per my example.
with the constraint (if statement) you can prevent it less than 10.
if you want to convert something awful like abc11def to abc**********def you will have to do a little extra work to get the 11 out of the middle. If its going to be 0-9, I think you have it solved now.
Last edited on
Topic archived. No new replies allowed.