Am I Declaring Functions Correctly?

  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    Vectors
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to declaring and using vectors to find the largest and smallest values from user input. Participants explore syntax errors and the proper inclusion of libraries, as well as the implications of function declaration syntax in C++.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares a code snippet and reports compile errors related to vector usage.
  • Another participant suggests that the error may be due to incorrect #include statements and points out the issue with the vector declaration syntax, indicating that parentheses should not be used.
  • A later reply acknowledges the need to include the library, reinforcing the importance of proper library inclusion in C++.
  • There is a discussion about the ambiguity in C++ syntax regarding function declarations and variable definitions, with examples provided to illustrate the point.
  • One participant questions whether the for loops should start at 1 instead of 0, indicating a potential oversight in the loop indexing.

Areas of Agreement / Disagreement

Participants generally agree on the issues with the code, particularly regarding the vector declaration and library inclusion. However, there are differing views on the starting index for the for loops, indicating that this aspect remains unresolved.

Contextual Notes

The discussion highlights limitations in understanding C++ syntax, particularly regarding function declarations and variable definitions. There are also unresolved questions about loop indexing that could affect the program's output.

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() {}
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
17
Views
2K