Am I Declaring Functions Correctly?

  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    Vectors
AI Thread Summary
The discussion centers on troubleshooting a C++ program designed to find the largest and smallest values from ten user-inputted numbers using vectors and loops. Key issues identified include incorrect syntax in the vector declaration, specifically the use of parentheses, which led to the compiler interpreting it as a function prototype rather than a variable declaration. Participants pointed out the necessity of including the vector library and correcting the vector declaration to avoid compilation errors. Additionally, there was a discussion about the starting index of the loops, suggesting that they should begin at 1 instead of 0 to accurately process the input values. The conversation emphasizes the importance of understanding C++ syntax and error messages to effectively debug code.
Lancelot59
Messages
640
Reaction score
1
I'm currently learning C++, and I'm trying to use vectors and some simple loops to find the largest/smallest values out of 10 inputted values:

Code:
#include iostream
#include string

using namespace std;

int main()
{

	vector<double> Values();
	double Largest, Smallest, TempVal;
	int i;
	
	//Getting the input
	while(data.size() < 11)
	{
		cout << "Input a number and press the enter key: ";
		cin >> TempVal;
		Values.push_back(TempVal);
	};
	
	//Finding The Largest Value
	for(i = 0, Largest = Values[0]; i < 10; i++)
	{
		if(Values[i] > Largest)
		{
			Largest = Values[i]
		};
	};
	
	//Finding The Smallest Value
	for(i = 0, Smallest = Values[0]; i < 10; i++)
	{
		if(Values[i] < Smallest)
		{
			Smallest = Values[i]
		};
	};
	
	cout << "The largest value is: " << Largest << endl;
	cout << "The smallest value is: " << Smallest << endl;

	return(0);
};

However I keep getting some random compile errors related to the vector. What am I doing wrong here?
 
Technology news on Phys.org
It would help if you posted the error -- both to save us time, and so we can help teach you to read them.


But anyways, the first thing I notice is that your #include lines are wrong.

Another thing I notice is that I think the declaration
vector<double> Values();
is bad. You shouldn't have the parentheses there. I believe that makes the compiler think you are trying to prototype a function.
 
Possibly that, but I didn't realize vectors had their own library. :p

Thanks, everything works now. I'll be sure to remove the parentheses, and be on the safe side.

Although when you define the vector wouldn't any compiler be looking for the name immediately after?
 
Lancelot59 said:
Possibly that, but I didn't realize vectors had their own library. :p
Oh, I missed the fact you didn't #include <vector>!

Although when you define the vector wouldn't any compiler be looking for the name immediately after?
There is an ambiguity1 in C++ syntax. How would you declare a function whose name is return type is vector<int> and takes zero arguments? It would look like
vector<int> myfunc();
Note that this is exactly the same form as the line you wrote trying to define a variable of type vector<int> to be constructed with the default constructor, as you did with Values.


1: I lied -- the ambiguity is only in the intuitive sense. I'm pretty sure the C++ standard directs the compiler to interpret it as declaring a function.
 
P.S. shouldn't your for loops start at 1 instead of 0?
 
Although when you define the vector wouldn't any compiler be looking for the name immediately after?

The problem stems from the fact that you can declare (but not define) one function inside of another.

We're generally used to seeing this:

Code:
void foo();

int main()
{
  foo();
  return 0;
}

void foo() {}

But this is also just as valid:

Code:
int main()
{
  void foo();
  foo();
  return 0;
}

void foo() {}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top