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.