modifying variables via functions

Hello.
I've been learning C++ as a hobby for a few months now on and off, and my current project is making a "sorter" program, designed for users to input numbers, and the program outputs the numbers in big-to-small criteria.

the program isn't long(what i have at least).
okay so before you go and read the code, let's talk about what's wrong:

1)this is a dynamic memory program, the user is asked to enter "how many
numbers" he wants to insert, this value is an unsigned int. How ever, if the
user inserts a huge number, causing an overflow, the program might act not as
intended, i have not found a way to catch the user's input, no matter how
large, and do a value check. as any int has a limit, is there a real
way of checking for user input?
i do know how to check if there is
enough memory, but that still depends on the value of "how many numbers".
that value is wrong when given a large enough number.

2)My main problem right now is changing an array using a function.
i have an array called "numbers[]", it's size is the "how many numbers" var
stated in the previous section. so in short, i want to take numbers[], insert
it inside a function, change it values(for this matter, swap two locations
numbers[a] and numbers[a-1])
my problem is that it doesn't save the
changes done to the pointer numbers(refering to numbers as a pointer to
numbers[0] )

i hope i'm clear enough, anyhow, this is the code.. i hope it's clear enough.
sorry for the long post, but i tried to make this as detailed as possible.



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
58
59
60
61
62
63
64
65
66
67
68
//this program should ask the user for how many numbers he wants to enter(int only)
//then enter the numbers, result is how many numbers and the order from biggest to smallest.
#include <iostream>
#include <sstream>
#include <new>
using namespace std;

void sort(int * pointer, int a ) //this functions should swap two locations in a given pointer.
{  // opens sort function
     int x;
     for (a-1 ; a-1==0; a--)
        {  // opens for loop in function sort
           if(pointer[a-1]>pointer[a]){  // opens if
                                       x = pointer[a-1];
                                       pointer[a-1] = pointer[a];
                                       pointer[a] = x;
                                       cout << pointer[a-1];
                                       cout << pointer[a];
                                       }; // closes if            
          } // closes for loop in function sort
} //closes sort funcion

int main()
{
    string temp;
    unsigned int n;
    int * numbers;
    int check;
    int *point; // ignore for now
    check = 0;
    while (check==0) {  //opens a while loop 
    
        cout << "how many numbers do you want to insert? \n";
        cout << "0 - 4,294,967,295\n";
        getline(cin, temp);
        stringstream(temp) >> n;
        numbers = new (nothrow) int[n];
       
                      if (numbers == 0) //opens if/else 
                           { 
                           cout <<"you do not have enough memory\n";
                           }
                     else {
                     check = 1; // stops the loop of memory allocation check
                              for(int i=0; i<n; i++) // for loop #1
                              { cout << "now entering value of number " << i+1 << "\n";
                              cin >> numbers[i];
                              };// ends the for loop #1
                               point = numbers;
                     cout << "you have entered " << n << " numbers\n";
                     cout << "the numbers are: ";
                              for(int i=0; i<n;i++) //for loop #2
                              {cout <<numbers[i] << ", ";
                              }; // ends the for loop #2
                              
                     cout << "\nand sorted :\n";
                     sort(numbers, n);
                     
                   
                            for (int i=0; i<n; i++)
                           {
                             cout << numbers[i] << ", ";
                           }     
                        }; //the end of the if/else
                  }; //the end of the while loop
cin.get();
return 0;
}
Last edited on
There are code tags for that purpose.
I'd suggest that you switch to std::vector and pass beg/end iterators. That's going to get rid of all the issues concerning array passing and sizing. But if you must use arrays, here's the page you should read: http://www.cplusplus.com/doc/tutorial/arrays/
And what do you mean, is there a real way of checking for user input? Of course there is.
1
2
3
4
5
6
7
8
cout << "Number from 1 to 10: ";
int num;
cin >> num;
while (num < 1 || num > 10)
{
    cout << "1 to 10 please! ";
    cin >> num;
}
I quickly made something similar to what you'd want. Except I did smallest to biggest. And the numbers you enter are randomly generated. See if you can reverse the sorting algorithms. I gave you two of them to play with. And two different ways to use an array with them.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <windows.h>
#include <time.h>

// Insertion Sort
// http://en.wikipedia.org/wiki/Insertion_sort
int* InsertionSort( int* numbers, int size )
{
	for( int i = 1; i < size; ++i )  
    {  
        int value = numbers[i];  
        int j = i - 1;  
  
        while( j >= 0 && numbers[j] > value )  
        {  
            numbers[j + 1] = numbers[j];  
            j = j - 1;  
        }  
        numbers[j + 1] = value;  
    } 

	return numbers;
};

// Selection Sort
// http://en.wikipedia.org/wiki/Selection_sort
// http://www.codeguru.com/cpp/cpp/cpp_mfc/pointers/article.php/c4089/
// Go to the above link to read about *&
void SelectionSort( int*& numbers, int size )
{
	for( int i = 0; i < size - 1; ++i )  
    {  
        int min = i;  
        for( int j = i + 1; j < size; ++j )  
        {  
            if( numbers[j] < numbers[min] )  
                min = j;  
        }  
        int swap = numbers[i];  
        numbers[i] = numbers[min];  
        numbers[min] = swap;  
    } 
}

// Print an array of numbers
void PrintArray( int* numbers, int size )
{
	std::cout << "Elements : "; 
	for( int i = 0; i < size; i++ )
	{
		std::cout << numbers[i] << " ";
	}
	std::cout << "\n";
}

int main()
{
	// Seed the random number generator
	srand((unsigned)time(NULL));

	int* numbers;
	int size = 0;

	int maxArraySize = 10;
	while( (size < 1 || size > maxArraySize) && size != -1 )
	{
		std::cout << "How many numbers would you like to insert? [-1 to exit]\n >> ";
		std::cin >> size;
	}

	if( size != -1 )
	{
		std::cout << "Creating an array with " << size << " elements.\n";
		numbers = new int[size];
		
		for( int i = 0; i < size; i++ )
		{
			// Add a random number between 0 - 49;
			numbers[i] = rand() % 50;
		}
		
		PrintArray(numbers, size);

		// You can use either sort - comment out the one you don't want to use.
		numbers = InsertionSort(numbers, size);
		//SelectionSort(numbers, size);

		std::cout << "\nAfter Sort...\n";
		PrintArray(numbers, size);

	}
	else
	{
		std::cout << "You Enterd -1, now exiting...\n";
	}

	return 0;
}
@tummychow
1) i didn't know where to find the code tags, sorry for the inconvinience.
edit - nm, found it :)
2)i read about arrays in the tutorial before posting here, i couldn't find any relevent advice on
how to use it with functions that have pointers as input datum.
2) for checking integers, what i mean is this.

1
2
3
4
5
6
7
8
9
10
11
12
 cout << "how many numbers do you want to insert ";
int num, *ptr;
cin >> num;
ptr = new (nothrow) int[num]
//i'm ignoring the check of available memory for this example.
cout >> num;

while{(num>4294967295) // this is the integer range
cout << "number too large";
}
}
  


note that if you try to insert a huge number, it's value won't be inserted inside the integer.
infact, if you enter a big enough number, the overflow will cause the value to be distorted, and it will actually fit into the while loop parameters. resulting that if you enter a number larger than the number i gave, it would not result that it is indeed larger.
or at least that's what i'm getting.
the problem is that any INT can only be 8/16 bits long, and it cannot handle a number bigger than 16 bits.
at least that's what i know, please correct me if i'm wrong.

@Mythios
that is WAY too advanced for me, as i don't know what half of the statements you wrote mean. guess it's back to reading and learning :)
Forcing to bump it so i don't post a new topic.
if there is no answer to my question then please post that.
that way an admin can lock it.
thanks.
Topic archived. No new replies allowed.