Thread: I think i need to use a pointer, some help please...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    I think i need to use a pointer, some help please...

    When i got to the chapter on pointers in my book, i just put it down, i couldnt follow it. As far as i know a pointer is something that holds the location of a variable, correct? Well here's my program, i figured out a login scheme that i think is excellent, i think its irreversible. Ok the problem is i dont want everything to be inside one infinite loop, things get too clustered, so i want the login to be in one while(1){} loop and the actual program to be in another. The problem is i need 2 variables from the 1st loop to be used to access the second. Any help would be greatly appreciated.

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        /* 
        Database program for Blacksquadron
        Auther: Doug Daly
        Version: 1.00
        */
        //Universal Vars
    
        //infinite loop
        while(1)
        {
            //title
            cout<<"**************************\n";
            cout<<"*     Black Squadron     *\n";
            cout<<"*        Database        *\n";
            cout<<"**************************\n\n";
            //login
            //init all vars
            char username[25];
            char password[25];
            char encinfo[25];
            char tempenc[25];
            char tempencI[25];
            char tempencII[25];
            int xleng;
            int i;
            //get information
            cout<<"Username: ";
            cin.getline(username, 25);
            cout<<"Password: ";
            cin.getline(password, 25);
            cout<<"\nPress enter to begin encryption...\n";
            cin.ignore();
            //encrypt
            for(i=0; i<25; i++)
            {
                tempenc[i]=username[i]+password[i];
            }
            xleng = strlen(tempenc);
            if((xleng%2)!=0)
            {
                tempenc[xleng+1]='x';
                xleng = strlen(tempenc);
            }
            for(int j=0; j<(xleng/2); j++)
            {
                tempencI[j]=tempenc[j];
            }
            i=0;
            for(int k=(xleng/2); k<xleng; k++)
            {
                tempenc[i]=tempenc[k];
                i++;
            }        
            for(int l=0; l<(xleng/2); l++)
            {
                encinfo[l]=tempencI[l]^tempencII[l];
            }
            //convert non-standard characters to standard
            for(int m=0; m<(strlen(encinfo)); m++)
            {
                encinfo[m]=char(((int(encinfo[m]))%26)+65);
            }
            //END ENCRYPTION
            //check result with known result
            //get import variables
            char ch;
            char importloc[100]="C:/Documents and Settings/Douglas/My Documents/C++/Test/known.cmc";
            char importcode[16];
            i=0;
            //import
            ifstream fin(importloc);
            while(fin.get(ch))
            {
                importcode[i]=ch;
                i++;
            }
            fin.close(); 
            //evaluate
            //eval. vars
            int evalnum=0;
            int encleng;
            //evaluate both known and new info
            if(strlen(importcode)==strlen(encinfo))
            {
                encleng=(strlen(encinfo));
            }else{
                cout<<"Length Mismatch\n";
                continue;
            }
            for(i=0; i<encleng; i++)
            {
                if(encinfo[i]==importcode[i])
                {
                    evalnum++;
                }
            }                
            if(evalnum==encleng)
            {
                cout<<"Successful Login!\n";
                break;
            }else{
                cout<<"Incorrect Login Information...\n";
            }                        
        }
        //check login correct
        if(evalnum==encleng)
        {
            //another infinite loop
            while(1)
            {
                //Program refresh
                system("cls");
                //title
       cout<<"|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|\n";
       cout<<"|BS|               DATABASE               |BS|\n";
       cout<<"|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|BS|\n\n";
                //menu
            }
        }           
        return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A pointer holds a value just like a float or an int holds a value. The value a pointer holds however is interpreted as being the address of some area in memory. Pointers are one of the most confusing and frustrating topics that newbs are taught. At first they might not seem to be of much value... as you get deeper into the language however you'll realize how indispensible they are. If you intend to spend more than a passing glance learning the language I'd strongly suggest you devote the time necessary (as much as you can spare) to understand pointers.


    In answer to your problem: Easiest way would be to make those two variables that you need be in the scope of main instead of in the scope of just the first loop. That way you'd be able to access them anywhere throughout the main function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yes i tried that, but i keep getting my "Length Mismatch" Error, and when i take that part of the program out i get an error when the program runs, and it has to close. Any ideas?

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Nevermind, thanks for the help, i did what you said and took the length mismatch out of the program and now it seems to be working fine. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM