8 Replies - 6043 Views - Last Post: 02 November 2007 - 08:36 AM Rate Topic: -----

#1 Lt_Kernel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

help with undeclared first use this function

Post icon  Posted 30 October 2007 - 12:36 PM

 
#include <iostream>

using namespace std;

int main ()
{
for ( int x = 0; x < 10; x++ ) 
{
		cout<< x <<endl;
  }
  cin.get();
}
 [b]that for the "for loop"[/b]

{

int x = 0;	
  while ( x < 10 ) 
{ 
	cout<< x <<endl;
	x++;			   }
  cin.get();
}
 [b]the while loop 
and the last is do while loop[/b]

{
  int x;

  x = 0;
  do {
		cout<<"Hello, world!\n";
  } while ( x != 0 );
  cin.get();
}


all having the same problem like "in the function main, undeclared, expected before int , each undeclared identifier is only reported once for each function it appears in, expected primary expression before int,
build error-error 1 etc...

all tutorial says it is correct
reference sheet i downloaded here says its true
a tutorial in here by rahulbata says its affirmative
and i recon it is

whats wrong ? please help

(*edit - jjhaag) you need to post code using the closing code tag as well, like this: :code:

This post has been edited by jjhaag: 30 October 2007 - 01:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help with undeclared first use this function

#2 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: help with undeclared first use this function

Posted 30 October 2007 - 01:00 PM

You have a bunch of stray braces in your program, resulting in the early termination of your main() routine - there is a bunch of code following the main that doesn't belong to any function, so that will cause some of your errors.

Once those are taken care of, you have also declared the variable x at three different locations - once in your first for loop, and twice with local scope within main(). You can only declare a variable once at a given variable scope (e.g. global scope, main() function scope, etc). You can assign a variable a different value repeatedly, but you can't redeclare it.

The following is a cleaned up version of what you posted - notice the change in the braces, as well as with the location of the declaration and assignments to your variable x:
#include <iostream>

using namespace std;

int main() {
	int x;
	for (x = 0; x < 10; x++) {
		cout<< x <<endl;
	}
	cin.get();
	
	x = 0;
	while (x < 10) {
		cout<< x <<endl;
		x++;
	}
	cin.get();
	
	x = 0;
	do {
		cout<<"Hello, world!\n";
	} while (x != 0);
	cin.get();
	
	return 0;
}


You also needed to specify a return value for main(), since you have defined it as returning an integer value (by the structure of the function header int main(). Generally, if a program exits successfully, a return value of zero is specified in the last line to execute in the code.

Hope that helps,
-jjh
Was This Post Helpful? 0
  • +
  • -

#3 Lt_Kernel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: help with undeclared first use this function

Posted 30 October 2007 - 01:06 PM

whats the difference between puting it to the next line or otherwise

and thanks it works

but i still dont understand what is "undeclared first use this function"
and this codes are separated for each type of loops i just type them in one
so the braces are not stray the changes i notice is from this

 int main ()
{
for ( int x = 0; x < 10; x++ ) 
{
		cout<< x <<endl;
  }
  cin.get();
} 


to this

 int main() {
	int x;
	for (x = 0; x < 10; x++) {
		cout<< x <<endl;
	}
	cin.get();
} 


the first and second open braces are put on the next line and you alligned it to int and for lines respectively. whats the difference? does that make the undeclared first use this function error? what is this error message wants me to do? re - align?

This post has been edited by Lt_Kernel: 30 October 2007 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: help with undeclared first use this function

Posted 30 October 2007 - 01:46 PM

Ummm...I'm not quite sure that I get your question. But here goes:

Moving the initial declaration of the variable x to outside of the for-loop header means that you're giving it local scope at the level of the whole main() routine, rather than just within the for loop. This allows you to reuse that variable in your while loop and your do-while loop, without having to declare a new variable. In your original code, if you just get rid of all of the extra braces, x would go out of scope after the for loop, so your declaration of another variable named x prior to the while loop would be syntactically correct. However, you would be redeclaring it just before your do-while loop, which isn't correct.

So to get around this problem, I moved the initial declaration of x from the for loop to the start of the main loop, promoting it to local scope within the whole main() routine - that is, making it accessible from anywhere in main(). Since x is now accessible anywhere in main(), this allows you to get rid of the other declarations of x later in the code, and change them to a simple assignment to an appropriate value (zero in this case).

You may want to take a look at this page - it has some good info on the declaration of variables and an explanation of variable scope.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 Smarf   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: help with undeclared first use this function

Posted 31 October 2007 - 06:15 AM

Let me see if I can help explain it:

The problem is in your first part of the code, I'll comment each brace so you can understand it better. Also if you indented it might help you see what's going on.

int main ()
{ // This brace starts the main function

for ( int x = 0; x < 10; x++ ) 
{ // this brace starts the for loop block

		cout<< x <<endl;
  } // this brace ends the for loop block

  cin.get();

} // THIS IS THE PROBLEM HERE. This brace shouldn't be here
  // and it basically ends your main function. So when the 
  // compiler see's any further braces, like your if else 
  // statements, it thinks you're trying to define a function. 


{

This post has been edited by Smarf: 31 October 2007 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 Smarf   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: help with undeclared first use this function

Posted 31 October 2007 - 06:28 AM

Here is a cleaned up version with all the extra braces removed. I'm not sure what IDE you're using but for my classes we use DEV C++ by Bloodshed. There is an option that let's you highlight matching braces, and it helps ALOT. Also since your code isn't very long, it helps to line them up instead of trying to put them at the end of a previous line each time.

#include <iostream>
using namespace std;
#include <limits> // this is for the snippet at the end that holds open
						  // the console window after execution.

int main ()
{ // starts the main function

for ( int x = 0; x < 10; x++ ) 
  { 
   cout<< x <<endl;

  } // end for loop
  

  int x = 0; // this needs to be declared here because 
			 // it previously only existed in that for loop above

  while ( x < 10 ) 
   { 
	 cout<< x <<endl;
	 x++;			   
   } // end while loop
   


  x = 0; // you don't need to declare it again, just reset it to zero
  do
   {
	cout<<"Hello, world!\n";
   } // end do while loop
  while ( x != 0 );
 


//Clean the stream and ask for input to hold console window open
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();
return 0;

} // end main

Was This Post Helpful? 0
  • +
  • -

#7 Lt_Kernel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: help with undeclared first use this function

Posted 31 October 2007 - 12:39 PM

thanks i finally figured out

the problem lies on declaring a variable with a semi colon and not the braces.
i just don't see it sorry
Was This Post Helpful? 0
  • +
  • -

#8 Smarf   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: help with undeclared first use this function

Posted 01 November 2007 - 08:54 AM

OK, I'll comment each brace to show you what was wrong. It was DEFINITELY the braces.

View PostLt_Kernel, on 30 Oct, 2007 - 12:36 PM, said:

 
#include <iostream>

using namespace std;

int main ()
{ // STARTS MAIN () function
for ( int x = 0; x < 10; x++ ) 
{ // STARTS FOR LOOP
		cout<< x <<endl;
  } // ENDS FOR LOOP
  cin.get();
} // THIS SHOULD NOT BE HERE AND IT ENDS YOUR MAIN FUNCTION
 [b]that for the "for loop"[/b]

{ // THIS SHOULD NOT BE HERE AND THE COMPILER THINKS YOU'RE
  // DEFINING A FUNCTION

int x = 0;	
  while ( x < 10 ) 
{ // STARTS WHILE LOOP
	cout<< x <<endl;
	x++;			   }
  cin.get();
} // ENDS WHILE LOOP
 [b]the while loop 
and the last is do while loop[/b]
 
{ // THIS SHOULD NOT BE HERE AND THE COMPILER THINKS YOU'RE
  // DEFINING A FUNCTION

  int x;

  x = 0;
  do { // STARTS A DO WHILE LOOP
		cout<<"Hello, world!\n";
  } while ( x != 0 ); // ENDS A DO WHILE LOOP
  cin.get();
} // THIS SHOULD NOT BE HERE AND THE COMPILER THINKS YOU'RE
  // ENDING THE DEFINING OF A FUNCTION




Was This Post Helpful? 0
  • +
  • -

#9 Lt_Kernel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Re: help with undeclared first use this function

Posted 02 November 2007 - 08:36 AM

my saving of space is misunderstood
those code should be like this separated from each types of loops

 
//for the "for loop
#include <iostream>
using namespace std;
int main ()
{					   // there is supposed to be like this "int x;" in here
   for ( int x = 0; x < 10; x++ ) //this is wrong & its like this "for (  x =0; x < 10; x++ )"
{
		cout<< x <<endl;
  } 
  cin.get();
}  //and why is this wrong ?



the only reason i type them all in one is to avoid re typing code] &[/code]
that would be three times coz there a three types of loops.

my question is why undeclared? so i reconed, from the first reply, my mistake.

thanks for those explanation it is giving me additional knowledge

This post has been edited by Lt_Kernel: 02 November 2007 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1