function with variable arguments

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.

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
//functions with variable number of arguments
#include <iostream>
using namespace 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;
Also, just to comment, I got a whole slew of errors in running this. Call me a newbie or what have you, but I need help.
What exactly were the errors?
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
If you want to write a function like "printf" which accepts variable number of arguments.
google va_start and va_end.
you should look up arguments in C++

1
2

int main(int argc,char *argv[])


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.

Last edited on
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, const double list[]);  // notice, don't put an array size here

int main()
{
  int number = /*get number of args from the user */
  double* list = new double[ 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)
Topic archived. No new replies allowed.