im starting a sub proceedure to test program once its worked through earlier processes. however to test the program, i have copied and pasted pieces of the program before hand, and because ive used the same variables they need to be declared again. but since things are stored in these variables, if i declare them again will they be overwriten or something or will the values that were there before be saved?

Recommended Answers

All 6 Replies

If you mean you are using variable names in other functions, that's no problem. The names will be only visible within the function in which declared.

Look up "scope" in relation to C++ variables.

it gives me errors saying that each variable hasnt been declared. but when i declare them at the start of the next function it just overwrites them with other values. so im a bit stuck.

void test_config () 
{
    float pix1;          // selector of pattern to be taugt
    float pix2 ;
    float W1A ;
    float W1B ;
    float W2A ;
    float W2B ;
    float OUTA;
    float OUTB;
    float WAC ;
    float WAD ;
    float WBC ;
    float WBD ;
    float OUTC;
    float OUTD;
    const float e = 2.718281828;
    float ERRORC;
    float ERRORD ;
    float ERRC ;
    float ERRD ;
    float pattern;
    float target1;
    float target2;
    char test;
       
    cout << endl << " to test recognition, please select pixels " ;
    cout << endl << "pixel 1 ";
    cin >> pix1;
    cout << endl << pix1 << endl << "pixel 2 ";
    cin >> pix2;
    cout << pix2;
    
    OUTA = 1/(1 + pow(e, -((W1A * pix1) + (W2A * pix2))));
    cout << endl << "output from neuron A: " << OUTA;
     
    OUTB = 1/(1 + pow ( e, -((W1B*pix1)+(W2B*pix2))));
    cout << endl << "output from neuron B: " << OUTB;
   
    OUTC = 1/(1 + pow(e, -((WAC * OUTA) + (WBC * OUTB))));
    cout << endl << endl  << "output from neuron C: " << OUTC; 
    
    OUTD = 1/(1 + pow(e, -((WAD * OUTA) + (WBD * OUTB))));
    cout << endl << "output from neuron D: " << OUTD; 
    
    ERRORC =   (OUTC)*(1 - OUTC)*(target1 - OUTC );
    ERRC = pow(ERRORC,2);
    ERRC = sqrt(ERRC); 
        
        
    ERRORD =  (OUTD)*(1 - OUTD)*(target2 - OUTD );
    ERRD = pow(ERRORD,2);
    ERRD = sqrt(ERRD);
    
    if  ( (ERRC + ERRD) < 0.5 )
    {
        cout << "congratulations it has learned!!!" << endl 
        << "would u like to try another set of pixels? ";
        cin >> test;
        if ( test == 'Y' )
        {
            test_config ();
        }
        if ( test == 'N' )
        {
             cout << "end of testing";
             return;
        }
    }     
    else
    cout << "unlucky";
    
}

its the variables at the start that need to be declared, but these dont work, how can i keep the values into this funciton.

Please post the full program (or enough of it to see just where this conflict is arising.) Your problem is not evident in the single function.

On thing that will cause runtime problems is your method of repeating the process:

if  ( (ERRC + ERRD) < 0.5 )
    {
        cout << "congratulations it has learned!!!" << endl 
        << "would u like to try another set of pixels? ";
        cin >> test;
        if ( test == 'Y' )
        {
            test_config ();
        }
        if ( test == 'N' )
        {
             cout << "end of testing";
             return;
        }
    }

This will recursively call the function as long as the user keeps selecting Yes - which can cause your program to eventually crash when the stack overflows. I would have the function simply do one iteration of the calculations, and at the level where you called this from, have a loop that continues to call the test_config( ) until user elects to quit. You're giving this function too much to do.

here is my full program, sorry that my comments and stuff may be wrong, the first function basically runs through and corrects the variables so that the errors are less than a certain amount, then i want to test the variables to make sure that the out puts ( c and D are the important ones) are still both near what the target is, even if the pixels are changed to not quite be the same as the targets. like a slightly noisey or imperfect signal. this is done in the second function, as its the only way i could think it would work, as you cant tell it to just loop back, or can you??

//------------------------------------------------------------------

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#pragma hdrstop

//-------------------------------------------------------------------

using namespace std;

void test_config (void);

int main ()

{
    srand( time(0) );
    float pix1;          // selector of pattern to be taugt
    float pix2 ;
    float W1A =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float W1B =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float W2A =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float W2B =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float OUTA;
    float OUTB;
    float WAC =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float WAD =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float WBC =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float WBD =  -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
    float OUTC;
    float OUTD;
    const float e = 2.718281828;
    float ERRORA;
    float ERRORB;
    float ERRORC;
    float ERRORD ;
    float ERRC ;
    float ERRD ;
    float pattern;
    float target1;
    float target2;
    char test;
    char testann;
    
    cout << "please select which pattern: ";
    cin >> pattern;   
    
    if  ( pattern == 1 )
        {
            target1 = 1;
            target2 = 0;
        }
    
    if  ( pattern == 2 )
        {
            target1 = 0;
            target2 = 1;
        }
    
    
    cout << endl << "target 1 is: " << target1 
    << endl << "target 2 is: " << target2;
    cout << endl << endl << "please select each pixel in the pattern: " 
    << endl << setiosflags(ios::fixed) << setprecision(2) << "pixel1: " ;
    cin >> pix1;
    cout << endl << "pixel2: ";
    cin >> pix2;
    
    cout << endl << "select each weight (random number between -1 and +1): ";
    cout << endl << "W1A: " << W1A;
    cout << endl << "W1B: " << W1B;
    cout << endl << "W2A: " << W2A; 
    cout << endl << "W2B: " << W2B;
    cout << endl << "WAC: " << WAC;
    cout << endl << "WAD: " << WAD;
    cout << endl << "WBC: " << WBC;
    cout << endl << "WBD: " << WBD;
     
    cout << endl << "forward pass"; 
        
    do
    
    {
    
        OUTA = 1/(1 + pow(e, -((W1A * pix1) + (W2A * pix2))));
        cout << endl << "output from neuron A: " << OUTA;
     
        OUTB = 1/(1 + pow ( e, -((W1B*pix1)+(W2B*pix2))));
        cout << endl << "output from neuron B: " << OUTB;
   
        OUTC = 1/(1 + pow(e, -((WAC * OUTA) + (WBC * OUTB))));
        cout << endl << endl  << "output from neuron C: " << OUTC; 
    
        OUTD = 1/(1 + pow(e, -((WAD * OUTA) + (WBD * OUTB))));
        cout << endl << "output from neuron D: " << OUTD; 
    
//------------ errors --------------------------   
       
        cout << endl << endl << "target 1 is " << target1 << endl << "target 2 is " << target2;
        ERRORC =   (OUTC)*(1 - OUTC)*(target1 - OUTC );
        ERRC = pow(ERRORC,2);
        ERRC = sqrt(ERRC); 
        
        cout << endl << " error from C is " << ERRC;
    
        ERRORD =  (OUTD)*(1 - OUTD)*(target2 - OUTD );
        ERRD = pow(ERRORD,2);
        ERRD = sqrt(ERRD);
        cout << endl << "error from D is " << ERRD;
        
//----------------change weights-----------------    
    
        cout << endl << "changed wieghts: " << endl; 
        WAC = WAC + (ERRORC*OUTA);
        cout << endl << "WAC: " << WAC;
        WAD = WAD + (ERRORD*OUTA);
        cout << endl << "WAD: " << WAD;
        WBC = WBC + (ERRORC*OUTB);
        cout << endl << "WBC: " << WBC;
        WBD = WBD + (ERRORD*OUTB);
        cout << endl << "WBD: " << WBD;
     
    
//----------------hidden layer errors --------------    
        cout << endl << "hidden layer errors " ;
        ERRORA = OUTA*(1-OUTA)*((ERRORC*WAC)+(ERRORD*WAD));
        cout << endl << "ERROR A: " << ERRORA;
        ERRORB = OUTB*(1-OUTB)*((ERRORC*WBC)+(ERRORD*WBD));
        cout << endl << "ERROR B: " << ERRORB;
     
//----------------hidden layer weights -------------

        cout << endl << "changes to hidden weights" ;
       
        W1A = W1A + (ERRORA*pix1);                     
        cout << endl << "W1A: " << W1A;
        W1B = W1B + (ERRORB*pix1);
        cout << endl << "W1B: " << W1B;
        W2A = W2A + (ERRORA*pix2);
        cout << endl << "W2A: " << W2A;
        W2B = W2B + (ERRORB*pix2);
        cout << endl << "W2B: " << W2B;
        
    }        
    while ((ERRC + ERRD) > 0.005);
    
    cout << endl << ERRORC << endl << ERRORD << endl << "outputs after training for pattern: " << pattern << " are " 
    << endl << "OUTC: " << OUTC << endl << "outD: " << OUTD << "   yippppeeee!!";
    
    cout << endl << " do you want to test the ANN? ";
    cin >> testann;
    
    if ( testann == 'Y' )
    {
         test_config ();
    }
    if (testann == 'N' )
    {
         getch();
         return 0;
    } 
   
    getch();
    return 0;

}    
    
    
    
/******************************************************************************
 *
 * Name: test_config
 * Parameters: none
 * Returns: ??
 * Globals:  none
 * Description: tests circuit
 *****************************************************************************/    
    
void test_config () 
{
    float pix1;          // selector of pattern to be taugt
    float pix2 ;
    float W1A ;
    float W1B ;
    float W2A ;
    float W2B ;
    float OUTA;
    float OUTB;
    float WAC ;
    float WAD ;
    float WBC ;
    float WBD ;
    float OUTC;
    float OUTD;
    const float e = 2.718281828;
    float ERRORC;
    float ERRORD ;
    float ERRC ;
    float ERRD ;
    float pattern;
    float target1;
    float target2;
    char test;
       
    cout << endl << " to test recognition, please select pixels " ;
    cout << endl << "pixel 1 ";
    cin >> pix1;
    cout << endl << pix1 << endl << "pixel 2 ";
    cin >> pix2;
    cout << pix2;
    
    OUTA = 1/(1 + pow(e, -((W1A * pix1) + (W2A * pix2))));
    cout << endl << "output from neuron A: " << OUTA;
     
    OUTB = 1/(1 + pow ( e, -((W1B*pix1)+(W2B*pix2))));
    cout << endl << "output from neuron B: " << OUTB;
   
    OUTC = 1/(1 + pow(e, -((WAC * OUTA) + (WBC * OUTB))));
    cout << endl << endl  << "output from neuron C: " << OUTC; 
    
    OUTD = 1/(1 + pow(e, -((WAD * OUTA) + (WBD * OUTB))));
    cout << endl << "output from neuron D: " << OUTD; 
    
    ERRORC =   (OUTC)*(1 - OUTC)*(target1 - OUTC );
    ERRC = pow(ERRORC,2);
    ERRC = sqrt(ERRC); 
        
        
    ERRORD =  (OUTD)*(1 - OUTD)*(target2 - OUTD );
    ERRD = pow(ERRORD,2);
    ERRD = sqrt(ERRD);
    
    if  ( (ERRC + ERRD) < 0.5 )
    {
        cout << "congratulations it has learned!!!" << endl 
        << "would u like to try another set of pixels? ";
        cin >> test;
        if ( test == 'Y' )
        {
            test_config ();
        }
        if ( test == 'N' )
        {
             cout << "end of testing";
             return;
        }
    }     
    else
    cout << "unlucky";
}

So the test function should use the weight values that have been established in main( )?

You need to pass them to the function as parameters. With so many, consider storing them in an array rather than a bunch of discrete variables - makes the passing easier.

thanks, erm im not sure how to set it up with arrays. could you give me and example with some of my variables and that should help me set it up. is there any other way around this? is there a way that i wouldnt need a seperate function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.