-2

Given one integer N, for each digit from the end of N, print a line of stars which has number of stars equal to that digit C++

Example Input: 2863

Example Output:

***
******
********
**

my code work in inputs like 12345,..,etc. but not work in 2863 !! i dont why and how can i edit my code to handle this problem !

#include <stdio.h>

int main()
{
    int b,d,j=0,num,num1,length=0,length1,x[100];
    float a,c;

    scanf("%d",&num);

    num1=num;

    while(num!=0){
        num/=10;
        length++;
    }

    length1=length;

    for(int i=0; length!=0; i++){
        a=(float)num1/10;
        b=num1/10;
        c=a-(float)b;
        d=c*10;
        x[i]=d;
        length--;
        num1=b;printf("%d\n",d);
    }

    while(length1!=0){
        while(x[j]!=0){
            printf("*");
            x[j]--;
        }
        length1--;
        j++;
        printf("\n");
    }
}
barak manos
  • 29,648
  • 10
  • 62
  • 114

2 Answers2

0

Try this:

void print(int num)
{
    while (num > 0)
    {
        int digit = num%10;
        for (int i=0; i<digit; i++)
            printf("*");
        printf("\n");
        num /= 10;
    }
}

int main()
{
    int num;
    scanf("%d",&num);
    print(num);
    return 0;
}
barak manos
  • 29,648
  • 10
  • 62
  • 114
0

My five cents.

#include <iostream>
#include <iomanip>


int main() 
{
    std::cout << std::setfill( '*' );

    while ( true )
    {
        const unsigned int Base10 = 10;

        std::cout << "Enter non-negative number (0 - exit): ";
        unsigned int n = 0;
        std::cin >> n;

        if ( !n ) break;

        std::cout << std::endl;

        do
        {
            std::cout << std::setw( n % Base10 + 1 ) << '\n';
        } while ( n /= Base10 );

        std::cout << std::endl;
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Nifty way of making the standard library do the work. I'm a little surprised that `setw` affects `operator<<(char)`, though. – Ben Voigt Jul 07 '14 at 18:21
  • Looking at OP's code, I would say that he or she are kind of... well... not quite ready for it just yet. Perhaps something a little more basic would be a little more suitable to the current knowledge at hand... – barak manos Jul 07 '14 at 18:23
  • @barak manos When I show a more basic code then at once some idiots appear that down vote my post because it as they think is primitive. – Vlad from Moscow Jul 07 '14 at 18:29
  • Wow, I could not have agreed with you more!!! Especially with regards to using functions like `printf` when the tag says C++. As if that's the main issue of the question at hand. – barak manos Jul 07 '14 at 18:49
  • Here is one such example when I gave an answer to a question which although was tagged as C++, you could easily notice that OP's issue had nothing to do with a specific syntax unique to C++, but something completely functional. I gave this answer: http://stackoverflow.com/a/21246119/1382251, and used `printf` just in order to give a usage example. Then, as you may see in the comments, one of those idiots that you mention immediately started putting me off about it being C not C++ (including down-votes and everything). – barak manos Jul 07 '14 at 18:51
  • The answer was accepted eventually, but sometimes it's just better to ignore those users who think they know C++ because they use some STL function or whatever. The actual point of the question at hand is sometimes a lot more important than the tag, whether it's C or C++. People should be able to make that distinction, and understand the cases where both C and C++ could be equivalently used without any difference. Unfortunately, as you mentioned above, some users here are... well... not very bright... Oh, and thank you for letting me take it all out :) – barak manos Jul 07 '14 at 18:55