Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with assigning a value to a array

Reply
Thread Tools

Problem with assigning a value to a array

 
 
atk0309 atk0309 is offline
Junior Member
Join Date: Nov 2006
Posts: 3
 
      11-12-2006
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[i] = tempnum%10;
tempnum = tempnum/10;
i = i+1;
cout<<masiv[i]<<" "<<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[i] 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
 
Reply With Quote
 
 
 
 
atk0309 atk0309 is offline
Junior Member
Join Date: Nov 2006
Posts: 3
 
      11-12-2006
Oh well, after 3 hours looking at it ... sorry for bothering, i found my problem.
 
Reply With Quote
 
 
 
 
xthor2005 xthor2005 is offline
Junior Member
Join Date: Nov 2006
Location: Houston, TX
Posts: 1
 
      11-12-2006
Quote:
Originally Posted by atk0309
Oh well, after 3 hours looking at it ... sorry for bothering, i found my problem.
Can you show how you fixed it for a newbie?
 
Reply With Quote
 
atk0309 atk0309 is offline
Junior Member
Join Date: Nov 2006
Posts: 3
 
      11-13-2006
while (tempnum != 0)
{
masiv[i] = tempnum%10;
tempnum = tempnum/10;
i = i+1;
cout<<masiv[i]<<" "<<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[i] = tempnum%10;
tempnum = tempnum/10;
cout<<masiv[i]<<" "<<tempnum<<"\n";
i = i+1;
};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning an array to another array using C's assignment operator Myth__Buster C Programming 13 02-26-2013 04:29 PM
Problem with assigning 1D array to a 2D array bintom C++ 11 10-15-2012 05:40 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
problems on assigning value to a 3D array and print it out,thx:) dd C Programming 0 03-29-2006 06:38 AM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments