Simple program gone wrong... Problem with devkitpro compiler

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Simple program gone wrong... Problem with devkitpro compiler

Post by Roroy » Tue Sep 25, 2012 2:11 pm

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <iostream>

using namespace std;


static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

int volume(int x, int y) {

	int result;
	
	result = x * y;
	
	return result;

}

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------

	// Initialise the video system
	VIDEO_Init();
	
	// This function initialises the attached controllers
	WPAD_Init();
	
	// Obtain the preferred video mode from the system
	// This will correspond to the settings in the Wii menu
	rmode = VIDEO_GetPreferredMode(NULL);

	// Allocate memory for the display in the uncached region
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	
	// Initialise the console, required for printf
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
	
	// Set up the video registers with the chosen mode
	VIDEO_Configure(rmode);
	
	// Tell the video hardware where our display memory is
	VIDEO_SetNextFramebuffer(xfb);
	
	// Make the display visible
	VIDEO_SetBlack(FALSE);

	// Flush the video register changes to the hardware
	VIDEO_Flush();

	// Wait for Video setup to complete
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();


	// The console understands VT terminal escape codes
	// This positions the cursor on row 2, column 0
	// we can use variables for this with format codes too
	// e.g. printf ("\x1b[%d;%dH", row, column );
	printf("\x1b[2;0H");
	

	printf("   Hello World!");
	
	printf("\n   This is the cheese!\n");
	
	
	int test;
	
	test = 0;
	
	while (test < 5) {
	
		printf("\n   Test is less than 5");
		
		test = test + 1;
		
	
	}
	
	printf("\n   Loop has quit");
	
	int precheck;
	
	precheck = 1;
	
	while(precheck == 1) {
	
		WPAD_ScanPads();
		
		u32 check = WPAD_ButtonsDown(0);
		
		if(check & WPAD_BUTTON_A) {
		
			printf("\n	You pressed the right button!\n");
		
			precheck = precheck + 1;
		
		} else{
		
			precheck = precheck + 0;
		
		}
	
	}
	
	printf("\n	Check complete. You pressed the A Button. \n");
	
	cout << "	This is a test for the cout function of C++" ;
	
	
	int buttoncheck;
	
	buttoncheck = 0;
	
	while (buttoncheck == 0) {
	
		WPAD_ScanPads();
		
		u32 bp1 = WPAD_ButtonsDown(0);
		
		if (bp1 & WPAD_BUTTON_MINUS ) {
		
			int a = 5;
			
			buttoncheck++;
		
		}
	
	}
	
	int buttoncheck2;
	
	buttoncheck2 = 0;
	
	while (buttoncheck2 == 0) {
	
		WPAD_ScanPads();
		
		u32 bp2 = WPAD_ButtonsDown(0);
		
		if (bp2 & WPAD_BUTTON_PLUS) {
		
			int b;

			b = 3;
			
			buttoncheck2++;
		
		}
	
	}
	
	
	cout << "The volume is " << volume(a, b) << "\n";

	
	while(1) {

		// Call WPAD_ScanPads each loop, this reads the latest controller states
		WPAD_ScanPads();

		// WPAD_ButtonsDown tells us which buttons were pressed in this loop
		// this is a "one shot" state which will not fire again until the button has been released
		u32 pressed = WPAD_ButtonsDown(0);
				
		// We return to the launcher application via exit
		if ( pressed & WPAD_BUTTON_HOME ) {
			printf("\n\n   Quitting presently\n\n\n"); 
			exit(0);
		}
		
		if ( pressed & WPAD_BUTTON_B ) {
			printf("\n   You pushed the B button down.");
		}
		
		if ( pressed & WPAD_BUTTON_A) {
			printf("\n   You pressed A");
		} 
		
		if (( pressed & WPAD_BUTTON_A) && ( pressed & WPAD_BUTTON_B)) {
			printf("\n   You pressed A and B simoultaneously!");
		}
		
				

		// Wait for the next frame
		VIDEO_WaitVSync();
	}

	return 0;
}
I get the errors:

Code: Select all

c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp: In function 'int main(int, char**)':
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:128:8: warning: unused variable 'a' [-Wunused-variable]
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:148:8: warning: variable 'b' set but not used [-Wunused-but-set-variable]
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:160:37: error: 'a' was not declared in this scope
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:160:40: error: 'b' was not declared in this scope
It says that I have 2 unused variables (a and b) and that 2 variables (a and b) are not declared?

How is that even possible?

Please help and thanks in advance.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Tue Sep 25, 2012 8:40 pm

Code: Select all

  if (bp1 & WPAD_BUTTON_MINUS ) {
    int a = 5; // you declare a new variable 'a'
    buttoncheck++;
  } // 'a' ceases to exist after this curly brace. it now goes "out of scope"
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:128:8: warning: unused variable 'a' [-Wunused-variable]
You declared 'a' but then never used it before it went out of scope. When you declare a variable inside '{}', then it only exists until you reach the '}', at which point it is destroyed.

Code: Select all

  if (bp2 & WPAD_BUTTON_PLUS) {
    int b; // you declare a new variable 'b'
    b = 3; // now you assign ("set") 'b'
    buttoncheck2++;
  } // hey hey now it's gone and you set but never used it! 'b' is now "out of scope"
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:148:8: warning: variable 'b' set but not used [-Wunused-but-set-variable]
Look exactly the same problem happened here.

Code: Select all

  cout << "The volume is " << volume(a, b) << "\n"; // hmm this 'a' and 'b' don't exist anymore
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:160:37: error: 'a' was not declared in this scope
c:/users/n**/desktop/devkitpro/examples/wii/template/source/template.cpp:160:40: error: 'b' was not declared in this scope
Now this is really a side-effect of what happened above. 'a' and 'b' are not in the current scope. This code may help illustrate the point:

Code: Select all

  int a = 0;
  int b = 1;

  printf("a=%d, b=%d\n", a, b); // this will print "a=0, b=1"

  { // create a new scope. it can see outside, but nothing outside can see in
    printf("a=%d, b=%d\n", a, b); // this will print "a=0, b=1"
    int a = 2;
    printf("a=%d, b=%d\n", a, b); // this will print "a=2, b=1"
  } // destroy the scope. the new 'a' gets destroyed along with it

  printf("a=%d, b=%d\n", a, b); // this will print "a=0, b=1"
Names are always resolved to the inner-most scope. Inside the '{}', the first print references the outside 'a' (=0) and 'b' (=1), since they are the innermost names that exist at the moment. The second print inside there is after a new 'a' (=2) is created. This name hides the outside 'a' (=0). The '}' ends the scope, so the 'a' (=2) is destroyed since the scope it resides in no longer exists. Now the outer 'a' (=0) is effectively unhidden.

I hope this helps you understand. This is not a problem with the toolchains, it is a problem with your knowledge of C/C++. When troubleshooting these kinds of issues, it's best to try it out in a different environment (for example, try writing a simple program that runs on your computer instead) that you think will exhibit the same problem. Then at least you can try to narrow down if it's a toolchain problem, or if there is just something you didn't know about C/C++.

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Wed Sep 26, 2012 2:26 am

Thankyou for your knowledge.

So is there a way to keep variables outside scopes and not let them be destroyed once the program gets to the "{"?

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Wed Sep 26, 2012 2:28 am

Of course! Just perform the declarations above (outside of) the if blocks.

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Wed Sep 26, 2012 2:31 am

I can do that but one of the parts to my program is:

Code: Select all

int buttoncheck;
   
   buttoncheck = 0;
   
   while (buttoncheck == 0) {
   
      WPAD_ScanPads();
      
      u32 bp1 = WPAD_ButtonsDown(0);
      
      if (bp1 & WPAD_BUTTON_MINUS ) {
      
         int a = 5;
         
         buttoncheck++;
      
      }
   
   }
What I want to do is if they press the Minus button on the remote, then and only then, set the variable a to equal 5. If I declare it outside the if statement, it would set it to 5 regardless. I need to set it only if a specific condition is true.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Wed Sep 26, 2012 2:35 am

Do int a = 0 on the outside. Then inside do a=5.

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Wed Sep 26, 2012 2:39 am

Thanks, that really helped out! :D

So why does doing this not work:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    int test = 1;



    if(test == 1) {

        int a = 7; 
    }

    cout << a << endl; 

    return 0;
}
Yet this does:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    int test = 1;

    int a = 0;

    if(test == 1) {

        a = 7; 
    }

    cout << a << endl; 

    return 0;
}
???

Confusing to me.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Wed Sep 26, 2012 3:01 am

See my first post. It explains it.

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Wed Sep 26, 2012 9:30 am

I've read it ten times and can't decipher what it says...

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by elhobbs » Wed Sep 26, 2012 10:56 am

You need to read up on "scope" and local variables in C. The { character creates a new scope. Variables created at in a scope go away when the matching } character is reached. These variables are called local variables.

I think you will find learning C/C++ by forum posting to be very frustrating. I do not think you will have a problem finding people willing to answer your questions. But, I think you will get frustrated and give up due to a lack of progress.

Also, homebrew is not a great place to learn C/C++. The lack of a modern IDE (possible to setup, but probably above your skill level at this point) and the lack of a debugger for hardware makes this hard. I think you would be better off to start in windows or Linux until you have a better understanding of the basics.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests