Don't understand what's wrong

This is a discussion on Don't understand what's wrong within the C++ Programming forums, part of the General Programming Boards category; hi Code: #include <iostream> using namespace std; int main() { int a,b; for(a=10;a<21;a++){ b=0; while(a>0){ b=b*10+a; a=a/10; } cout<<b<<endl; } ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Don't understand what's wrong

    hi

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {   
        int a,b;
        for(a=10;a<21;a++){
                          b=0;
                          while(a>0){
                                     b=b*10+a;
                                     a=a/10;
                                     }     
                           cout<<b<<endl;
                           }    
         system("pause");
    }

    I have this program.When I run is shows only 111111111111...
    why?
    normally must output 1,21,31,41,51,61,71,81,91,2 no?

    thx

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    go through the program in your head or on paper, carefully following the steps. write down the values of a and b at every step. you will find the problem.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    A second hint. What is the value of anything multiplied by zero?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are trying to use a as a loop counter, at the same time setting it to 0 inside the loop.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 01-18-2009, 11:53 PM
  2. I don't understand what is wrong...
    By flaran in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2006, 06:46 AM
  3. Can't understand what I'm doing wrong.
    By agentxx04 in forum C Programming
    Replies: 11
    Last Post: 10-22-2004, 08:53 PM
  4. help......i dont understand what i am doing wrong?
    By Grifftt in forum C Programming
    Replies: 2
    Last Post: 10-19-2002, 12:37 AM
  5. I don't understand what i'm doing wrong..
    By knutso in forum C Programming
    Replies: 4
    Last Post: 03-15-2002, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21