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;
}
|