So I have to make a program that accepts any number of arguments the user enters, then has the user enter numbers. The function will then determine if the list is ascending. naturally, when I planned my soltuion, all seemed logical, but compiling it ended up with me feeling dumbfounded. Please look over my code and advise on where I went wrong and how I can improve. Thanks in advance.
//functions with variable number of arguments
#include <iostream>
usingnamespace std;
bool ordered(int numArgs, double list[numArgs]);
int main(){
bool isOrdered;
double list[numArgs];
cout << "This program will check if a series of any number of numbers you enter is ascending." << endl;
cout << "Enter the number of numbers you want to enter: ";
cin >> numArgs;
cout << "Enter the numbers: ";
for (int i = 0; i < numArgs; i++)
cin >> list[i];
bool ordered(numArgs, list[numArgs]);
if (isOrdered == true)
cout << "Your list is ascending." << endl;
else
cout << "Your list is not entirely ascending, if at all." << endl;
}
bool ordered(numArgs, list[numArgs]){
bool isOrdered = true;
for (int ele = 0; ele < numArgs; ele++) { //declare first element to be min
double min = list[i]
for (int n = 1; n <= i; n++){ //if anything after 1st is min, array is not ordered
if (min < list[n]){
bool isOrdered = false;
break;
}
}
}
return isOrdered;
one of the big errors were the issue it was having with numArgs. The compiler wants it to be constant because it's used as an array size, but I don't know how to let a user input a constant
Additionally, your not returning a integer. int main always returns 0; to show successful termination.
or 1 - 196 ? about that to show various other error messages.
return isOrdered;
is not part of any block or class.
Variable arguments (va_start, elipsis, etc) have ugly syntax, are not typesafe, are very easy to misuse, and lead to all sorts of other problems. Plus they don't even work for what you're trying to do anyway.
The problem with using an array for something like this is that an array needs to have a fixed size. Since the user can input the size at runtime, there's no way for you to know the size at compile time. This is why what you're trying to do doesn't work.
You have two options:
1) make a dynamically allocated array
2) use a dynamically resizable container class (like vector)
If you want to go the array route:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool ordered(int numArgs, constdouble list[]); // notice, don't put an array size here
int main()
{
int number = /*get number of args from the user */
double* list = newdouble[ number ];
// fill 'list' here
ordered( number, list );
delete[] list; // don't forget to delete[] things created with new[]
}
Or the vector route:
1 2 3 4 5 6 7 8 9 10 11 12
bool ordered(const std::vector<double>& list); // don't need to pass the size, because you can call list.size() to get the size
int main()
{
int number = /*get number of args from the user */
std::vector<double> list(number); // create it to have 'number' elements initially
// fill 'list' here
ordered(list);
}
return isOrdered;
is not part of any block or class.
He's just missing one final closing brace for his ordered() function, possibly just an error in his post and not his sources, but perhaps not. (There are 4 open braces as part of that function call, but only 3 closing braces)