Bad array output

Hello. I'm new here and I need your help.

I'm writing a program for my college task. The program is not finished yet. I stucked with a dynamic array. Everything looks fine to me and in my way I understand of what am I doing. But tragically I receive bad results when executing it.

Task: Initial data - three-figure natural set of numbers. At the end of the set is 0 (zero).
The length of the set is not known. Write a program that print the members of the sequence
in non-descending order.

I'm using Codegear C++ Builder 2009.
And i write this in console application.

tthe code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//---------------------------------------------------------------------------
#pragma hdrstop
#include <iostream.h>
#include <tchar.h>
#include <stdio.h>
//---------------------------------------------------------------------------

#pragma argsused
int main ()
{
  char k;
   int *A;
   int sk;
   int n;
   int v = 0;
  cout << " Initial data - three-figure natural set of numbers. \n";
  cout << " At the end of the set is 0 (zero). \n";
  cout << " The length of the set is not known. \n";
  cout << " Write a program that print the members \n";
  cout << " of the sequence in non-descending order. \n";
  cout << " \n";
  cout << " How many members does the set has?: ";
  cin >> n;
  A = new int [n];
  cout << endl;
  cout << "Enter 3-digit " << n << " numbers: " << '\n' <<
  '\n';
  while(v < n-1)
  {
   cout << v+1 << ". ";
   cin >> sk;
	 if(sk > 99 && sk < 1000)
	 {
		v++;
		sk >> A[v];
	 }
  else cout << " The member of the array must be a three-figure natural number!"
   << '\n';
  }
  if(v = n)
  {
	  cout << v << ". ";
	  printf ("%u \n", 0);

  }
  for(int i = 0; i < v; i++)
  cout << A[i] << ", ";
  delete[] A;
  do
	 {
		 cout << '\n';
		 cout << " To finish this press 'q'" << '\n';
		 cin >> k;
	 }
   while (k!=113);
   return 0;
}



...
849168012, 849168012, 0, 0, 0,
 To finish this press 'q'


The command line window with results is here: http://i061.radikal.ru/0910/01/654c2f98766e.bmp

Why the hell it prints 849168012, 849168012, 0, 0, 0, instead of exact numbers from the array?

It is important to check whether the number is 3-figure before entering it to the dynamic array. So it must be between 99 and 1000. If this condition is true then the number is entering to the array. It's index v is increasing.

But when I want to show the contents of the array I use cout << A[i]... but the output results are horrible (849168012, 849168012, 0, 0, 0,)

What the hell? why?

T_T :(

Can you fix this? I would be glad.
There are four problems that I see.

First, line 40 is an assignment, not a comparison. This isn't causing your problem.

Second, line 34, you increment v before storing in the array. So think about it... will A[0] ever
get assigned a value?

Third, line 35 does nothing. It is a bitshift of sk A[v] bits to the right, and discards the value. It
does not store sk into the array. EDIT: This is your problem, but you also need to correct
the other problems as well to get it to run right.

Fourth, your while loop -- while( v < n - 1 ) will read n - 1 numbers, not n numbers as you want.
Last edited on
Ok Jsmith,

1) I've changed assignment to comparison ==

2) I put v++; after the sk >> A[v]

3) How to store that sk to the array? :(

4)I chaged the condition of the loop: while(v < n)
I solved this by myself

In the 35 line: sk >> A[v] does nothing. you were right.

So after cruel endeavours I found this:

1
2
3
4
5
6
7
8
9
10
11
...
while(v < n-1)
  {
   cout << v+1 << ". ";
   cin >> sk;
	 if(sk > 99 && sk < 1000)
	 {
	   A[v] = sk;
		v++;
	 }
...

And Now it shows my array in the output process.

Thanks anyway.
Topic archived. No new replies allowed.