Problem with assigning a value to a array

Discussion in 'C Programming' started by atk0309, Nov 12, 2006.

  1. atk0309

    atk0309

    Joined:
    Nov 12, 2006
    Messages:
    3
    Hi, I am a new in C/C++ , and this years if my first in programming in this langauges.

    I have this assignement: Write a program that after you enter a certain number, it writes it backwards
    Examples:
    Input: 1234567
    Output: 7654321
    Preferrably use arrays. (The lessos is about arrays)

    This is what i wrote:

    void main ()
    {
    cout<<"\nEnter a number :";
    int num;
    cin>>num;
    cout<<"\nThe number you have entered is : "<<num<<"\n";

    int masiv[40];

    int tempnum;
    int i;
    i = 0;
    tempnum = num;
    while (tempnum != 0)
    {
    masiv = tempnum%10;
    tempnum = tempnum/10;
    i = i+1;
    cout<<masiv<<" "<<tempnum<<"\n";
    };

    }

    I hope you get what the idea is.
    But as soon as i run the program this is what i get:


    Enter a number :1234567

    The number you have entered is : 1234567
    -858993460 123456
    -858993460 12345
    -858993460 1234
    -858993460 123
    -858993460 12
    -858993460 1
    -858993460 0
    Press any key to continue

    To explain why i do this loop: It is intended in order to check if i get the correct values in the correct places. For example if the masiv actually gets a single number in every incrementation of the loop and if this number is correct with what it should be.
    Now the question is why instead of the different numbers i should be getting as elements of the array "masiv" i get this "-858993460" seamingly random number as output.

    I hope i explained myself good enough :)
     
    atk0309, Nov 12, 2006
    #1
    1. Advertising

  2. atk0309

    atk0309

    Joined:
    Nov 12, 2006
    Messages:
    3
    Oh well, after 3 hours looking at it ... sorry for bothering, i found my problem.
     
    atk0309, Nov 12, 2006
    #2
    1. Advertising

  3. atk0309

    xthor2005

    Joined:
    Nov 12, 2006
    Messages:
    1
    Location:
    Houston, TX
    How did you fix it?

    Can you show how you fixed it for a newbie?:shake:
     
    xthor2005, Nov 12, 2006
    #3
  4. atk0309

    atk0309

    Joined:
    Nov 12, 2006
    Messages:
    3
    while (tempnum != 0)
    {
    masiv = tempnum%10;
    tempnum = tempnum/10;
    i = i+1;
    cout<<masiv<<" "<<tempnum<<"\n";
    };


    In here i am cout-ing an element in the array that still has no value assigned to it.

    If you note that i first increment i = i+1 and now the pointer is on the next element but as it still has no value assigned, it shows the strange stuff.
    And this is how it works just fine:
    while (tempnum != 0)
    {
    masiv = tempnum%10;
    tempnum = tempnum/10;
    cout<<masiv<<" "<<tempnum<<"\n";
    i = i+1;
    };
     
    atk0309, Nov 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dd
    Replies:
    0
    Views:
    290
  2. Richard Lionheart
    Replies:
    27
    Views:
    403
    Jean-Hugues ROBERT
    May 4, 2004
  3. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
  4. bintom
    Replies:
    11
    Views:
    657
    Luca Risolia
    Oct 15, 2012
  5. Myth__Buster
    Replies:
    13
    Views:
    377
    Anand Hariharan
    Feb 26, 2013
Loading...

Share This Page