# Why is n an undeclared identifier

This topic is 3782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone I'm doing a bubble sort and getting an error I don't understand, how do I fix it please? Here's my code:

#include <iostream>
using namespace std;

void BubbleSort(int data[], int n);

int main()
{
int data[10];
int input = 0;
cout << "Enter ten unsorted integers: " << endl;

for(int i = 0; i < 10; ++i)
{
cout << "[" << i << "] = ";
cin >> input;
data[i] = input;
}

cout << endl << "Unsorted List = ";

for(int i = 0; i < 10; ++i)
{
if(i < 9)
{
cout << data[i] << ", ";
}
else
{
cout << data[i];
}
}

cout << endl << "Sorting..." << endl << "Sorted List = ";
cout << BubbleSort(data, n) << endl << endl;
}

void BubbleSort(int data[], int n)
{
int subArrayEnd = 9;

while( subArrayEnd > 0 )
{
int nextEnd = 0;

for(int j = 0; j < 8; ++j)
{
if( data[j] > data[j+1] )
{
int temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
nextEnd = j;
subArrayEnd = nextEnd;
}
else
{
data[j] = data[j];
data[j+1] = data[j+1];
}
}
}
}


I get this error:

------ Build started: Project: 3.7.8, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.8\3.7.8\main.cpp(34) : error C2065: 'n' : undeclared identifier
Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.8\3.7.8\Debug\BuildLog.htm"
3.7.8 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks, Chris.

##### Share on other sites
int main(){	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data[i] = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";        // You are using n here, but look above, you never declared it!	cout << BubbleSort(data, n) << endl << endl; }

##### Share on other sites
I know this may be the simple response, but maybe because you never identified it.

You don't even seem to use it... but the error is caused by you never declaring it (like "int n;") and never defining it either (like "n = 5;")*

Yes I know the question was just answered, but it was answered in code, in a comment, and I didn't know if that was obvious.

*or "int n = 5;" from the start, yes

##### Share on other sites
Hi, thank you for your replies.

I've now adjusted my function so that I'm actually using n, should have realised that one myself lol!

Sorry, I don't think I explained myself in my first post.

I thought I'd already declared it when I originally put it in the function declaration at the top since isn't that global relative to where it comes up as an undeclared identifier?

I realise that I hadn't defined it there but when I put int n = 10 at the start of main, I got another error plus a load of other stuff.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[], int n);int main(){	int n = 10;	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data[i] = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	cout << BubbleSort(data, n) << endl << endl;}void BubbleSort(int data[], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd - 1; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;				subArrayEnd = nextEnd;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}	}}

In the output, I get:

------ Build started: Project: 3.7.8, Configuration: Debug Win32 ------Compiling...main.cppc:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.8\3.7.8\main.cpp(35) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)        c:\program files\microsoft visual studio 8\vc\include\ostream(656): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(703): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(741): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(788): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(912): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(919): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(926): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(933): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(180): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(187): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(194): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(214): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(247): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(267): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(292): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(312): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(332): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(353): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(373): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(394): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(414): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(434): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(454): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        c:\program files\microsoft visual studio 8\vc\include\ostream(474): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'        with        [            _Elem=char,            _Traits=std::char_traits<char>        ]        while trying to match the argument list '(std::ostream, void)'Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.8\3.7.8\Debug\BuildLog.htm"3.7.8 - 1 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

##### Share on other sites
int main() {    // snip except last line    cout << BubbleSort(data, n) << endl << endl;}void BubbleSort(int data[], int n) {// ...

std::cout doesn't know how to << something of type "void".

The solution would be to call BubbleSort, and then print all the elements of the array.

Here is the important part of the error messages:
Quote:
 binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

However I understand that template error messages are extremely difficult to read when you are unused to the language.

##### Share on other sites

Does this involve using function overloading to define a new function that just prints the list out? If so, how do I need to define my function and what do I need it to execute. If not, I'm not sure what you mean.

Thanks,
Chris.

##### Share on other sites
Quote:
 Original post by chockydavid1983Hi, thank you for your reply.Does this involve using function overloading to define a new function that just prints the list out? If so, how do I need to define my function and what do I need it to execute. If not, I'm not sure what you mean.Thanks,Chris.

No, you don't have to do that.

See where you printed the "Unsorted List". Just do the same after you sort the list [smile]. You can move the printing to a separate function too.

##### Share on other sites
A note: just because your function takes an integer named 'n', doesn't mean you have to pass it an integer named 'n' also - 'n' is just how the integer that you pass it is named inside that function. So:

void SomeFunction(int n);int main(){	// Inside SomeFunction, n will be 5	SomeFunction(5);	// Inside SomeFunction, n will be 8 - SomeFunction doesn't even know that	// that 8 came from a variabele named a, and that's just fine. :)	int a = 8;	SomeFunction(a);	// Note that this integer, named n, is a different integer than used inside	// SomeFunction - they have the same name, but are within different scopes	int n = 12;	SomeFuntion(n);}void SomeFunction(int n){	// Blabla}

##### Share on other sites

I seem to have got a bit lost. I am using the right method here? I'm trying to sort the integers using the function and then output them as a list using the for loop.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[], int n);int main(){	int n = 10;	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data[i] = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	BubbleSort(data, n);	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}}void BubbleSort(int data[], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd - 1; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;				subArrayEnd = nextEnd;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}	}}

Thanks,
Chris.

##### Share on other sites
Quote:
 I am using the right method here?

Pretty much.

Does the program do what you want? If so, then you are right on target. [grin]

If not, then what isn't it doing...

A program can always be improved. For example, the code you've used to print the array is used twice. Moving the code into a separate function means there isn't redundant code, and if you want to change anything you only have to make the change in one place.

##### Share on other sites

No, the program is not doing what I want it to do.
When I input an unsorted list of 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
It outputs the sorted list as 9, 10, 8, 7, 6, 5, 4, 3, 2, 1.

It seems that now the BubbleSort is not working as it should be, why is this please? (For code, see my last most recent post to this topic).

Thanks,
Chris.

##### Share on other sites
Hi, thank you for your replies so far everyone.

After much experimenting, I've finally got my program working properly. I would now just like to clarify exactly what is going on and how it's working if that's OK. All this involves the BubbleSort function.

Initialise the variable subArrayEnd to define the end of the working sub array

Continue the loop while this is > 0 since at that point, the array will be sorted

On each pass of the for loop, for all elements in the working sub array, check if 1st > 2nd etc and if so, swap them using the integer temp or just leave them where they are.

By the lat pass of the for loop, everything is sorted and j = 0 so this makes subArrayEnd 0, which exits out of the while loop.

Here's my code:

#include <iostream>using namespace std;void BubbleSort(int data[10], int n);int main(){	int data[10];	int input = 0;	cout << "Enter ten unsorted integers: " << endl;	for(int i = 0; i < 10; ++i)	{		cout << "[" << i << "] = ";		cin >> input;		data[i] = input;	}	cout << endl << "Unsorted List = ";	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}	cout << endl << "Sorting..." << endl << "Sorted List = ";	BubbleSort(data, 10);	for(int i = 0; i < 10; ++i)	{		if(i < 9)		{			cout << data[i] << ", ";		}		else		{			cout << data[i];		}	}	cout << endl << endl;}void BubbleSort(int data[10], int n){	int subArrayEnd = n - 1;	while( subArrayEnd > 0 )	{		int nextEnd = 0;		for(int j = 0; j < subArrayEnd; ++j)		{			if( data[j] > data[j+1] )			{				int temp = data[j];				data[j] = data[j+1];				data[j+1] = temp;				nextEnd = j;			}			else			{				data[j] = data[j];				data[j+1] = data[j+1];			}		}		subArrayEnd = nextEnd;	}}

Thanks,
Chris.

##### Share on other sites
Quote:
 else{ data[j] = data[j]; data[j+1] = data[j+1];}

?!

EDIT: In other words, if the first element is larger than the second, swap them, but in any other case, make sure that the elements stay in place? Weren't they in the correct order then to start with? I think you understand what I mean: that else part is unnecessary, it doesn't do anything usefull.

##### Share on other sites

I've edited my code now to have an empty else statement, didn't realise you could have those, thanks!

Chris.

##### Share on other sites
Quote:
 Original post by chockydavid1983Hi, thank you for your reply.I've edited my code now to have an empty else statement, didn't realise you could have those, thanks!Chris.

You can drop the else completely if you want.

if( condition ) {   // stuff}// no else...

##### Share on other sites

This topic is 3782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.