I have problem with for(loop)

This is a discussion on I have problem with for(loop) within the C++ Programming forums, part of the General Programming Boards category; Hi all my teacher asked me two things first thing he wants from me write program allow the user to ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    10

    I have problem with for(loop)

    Hi all

    my teacher asked me two things

    first thing

    he wants from me write program allow the user to enter 4 a digits numbers
    and output will be like this

    Enter a 4 digits numbers
    9876
    6
    7
    8
    9
    Press any key to continue

    and this code for program is correct100%

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int n , d , i;
    
        cout << "Enter a 4 digits numbers\n";
        cin >> n;
    
        for(i=1; i<=4; i++)
        {
            d= n % 10;
            n = n / 10;
            cout << d <<endl;
        }
        return 0;
    }
    but where the problem ?

    the problem in second question he wants write other program allow the user to enter any number he wants for example if he entered 987654 out will be like this
    4
    5
    6
    7
    8
    9
    Press any key to continue
    not just 4 digits but every digits the user wants it

    I tried more than 20 times but all my tries failed.

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, the only thing that changes compared to the code you posted, is that instead of "4" in your loop condition you would have "x", where x is the number of digits in your number.

    Now, the only thing you need to do is find the number of digits in a number...

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    and this code for program is correct100%
    but where the problem ?
    If it's 100% correct, what makes you think there's a problem?
    the problem in second question he wants write other program allow the user to enter any number
    Instead of looping a set number of times, just stop when n is 0. Something like 'while ( n != 0 )'.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    and this code for program is correct100%
    ...
    but where the problem ?
    Well if it's correct there is no problem, right? It must not be correct. It looks okay to me.

    Well, to support more than four digits, obviously you need to change this.
    Code:
    for(i=1; i<=4; i++)
    You may have noticed that when you run out of digits n is zero . . .

    [edit] Great minds think alike, though you gave the game away, Noir. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    Quote Originally Posted by KONI
    Well, the only thing that changes compared to the code you posted, is that instead of "4" in your loop condition you would have "x", where x is the number of digits in your number.

    Now, the only thing you need to do is find the number of digits in a number...
    thnx very much KONY You solved my problem in 3 minutes.


    Noir,Dwks, thanx for sharing.

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    though you gave the game away, Noir.
    Not much I can do when it comes to easy problems. Any useful answer gives it away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

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