Thread: Program bombing out

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Hi. Thanks for the reply.

    However can I just ask why them bieng global is a bad thing. Also what's the difference static and global? I think I know, but I might be wrong. Cheers.

    EDIT: What the hell? Why's my reply at the top??? Oh well ...

    I also forgot to mention in my origional post that I get stuck in the main function while loop. Thats why I checked the variables, to see if they were the same and even if they are, I still get stuck in the loop. What's that about?
    Last edited by Necrofear; 11-04-2006 at 12:04 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Program bombing out

    Hey guys!

    Haven't posted here in ages. Anyway, I have a question about this project I've started. I'm not really sure how to explain what the program actually does, but my question only concerns the first part of the program anyway. (This is the only part I've written so far.)

    I'll go right ahead and post the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    
    using namespace std;
    
    #define nul '\0'
    
    int Initiation;
    
    int Asize;
    char Ainput;
    string Apassword;
    string Apassword2;
    
    ////////////////////////////////////////////////////////////////////////////////
    // Accounts Structure
    ////////////////////////////////////////////////////////////////////////////////
    
    struct Accounts
    {
        string Username;
        string Password;
    };
    
    ////////////////////////////////////////////////////////////////////////////////
    // End of Accounts Structure
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    // Setup function.
    ////////////////////////////////////////////////////////////////////////////////
    
    bool InitiationFunction()
    {
        Asize = 0;
        Ainput = nul;
        Apassword = "";
        Apassword2 = "";
        
        cout<<"\n\n\n    NEMESIS PROGRAM INITIATED\n\n\n\n    Nemesis is currently under maintenance, and some functions may not work\n    correctly. Nemesis will be fully operational soon and we apologise for\n    any inconvenience this delay may have caused.";
        cout<<"\n\n    As this is the first time you have executed Nemesis, an administration\n    account must be created. The username for this account will be\n    automatically set to Nemesis.\n\n    Please enter your desired password for this account.\n\n\n\n    Desired Password:    ";
        
        while ( Ainput != EOF && Ainput != '\n' && Ainput != '\r' )
        {
            Ainput = getch();
        
            if ( Ainput == '\b' && Asize > 0 )
            {
                cout<<"\b \b";
                Asize--;
                Apassword.erase(Apassword.size()-1);
            }
        
            else
            {
                if ( Ainput == ' ' || Ainput == '\\' || Ainput == '|' || Ainput == ',' || Ainput == '<' || Ainput == '.' || Ainput == '>' || Ainput == '/' || Ainput == '?' || Ainput == ';' || Ainput == ':' || Ainput == '\'' || Ainput == '@' || Ainput == '#' || Ainput == '~' || Ainput == ']' || Ainput == '}' || Ainput == '[' || Ainput == '{' || Ainput == '=' || Ainput == '+' || Ainput == '-' || Ainput == '_' || Ainput == ')' || Ainput == '(' || Ainput == '*' || Ainput == '&' || Ainput == '^' || Ainput == '%' || Ainput == '$' || Ainput == '&#163;' || Ainput == '"' || Ainput == '!' ) 
                {
                    if ( Asize < 50 )
                    {
                        cout<<"*";
                        Apassword += Ainput;
                        Asize++;
                    }
                }
                
                if ( isalnum(Ainput) && Asize < 50 )
                {
                    cout<<"*";
                    Apassword2 += Ainput;
                    Asize++;
                }
            }
        }
        
        Asize = 0;
        Ainput = nul;
        
        cout<<"\n\n    Confirm Password:    ";
        
        while ( Ainput != EOF && Ainput != '\n' && Ainput != '\r' )
        {
            Ainput = getch();
            
            if ( Ainput == '\b' && Asize > 0 )
            {
                cout<<"\b \b";
                Asize--;
                Apassword2.erase(Apassword2.size()-1);
            }
            
            else
            {
                if ( Ainput == ' ' || Ainput == '\\' || Ainput == '|' || Ainput == ',' || Ainput == '<' || Ainput == '.' || Ainput == '>' || Ainput == '/' || Ainput == '?' || Ainput == ';' || Ainput == ':' || Ainput == '\'' || Ainput == '@' || Ainput == '#' || Ainput == '~' || Ainput == ']' || Ainput == '}' || Ainput == '[' || Ainput == '{' || Ainput == '=' || Ainput == '+' || Ainput == '-' || Ainput == '_' || Ainput == ')' || Ainput == '(' || Ainput == '*' || Ainput == '&' || Ainput == '^' || Ainput == '%' || Ainput == '$' || Ainput == '&#163;' || Ainput == '"' || Ainput == '!' ) 
                {
                    if ( Asize < 50 )
                    {
                        cout<<"*";
                        Apassword2 += Ainput;
                        Asize++;
                    }
                }
                
                if ( isalnum(Ainput) && Asize < 50 )
                {
                    cout<<"*";
                    Apassword2 += Ainput;
                    Asize++;
                }
            }
        }
        
        return true;
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    // End of Setup function.
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    // Function to read whole file.
    ////////////////////////////////////////////////////////////////////////////////
    
    bool ReadFile ( string Filename, string &Text )
    {
        ifstream Read ( Filename.c_str() );
        
        if ( Read )
        {
            string Temp;
            Text.clear();
            
            while ( getline ( Read, Temp ) )
            {
                Text += Temp;
                Text += "\n";
            }
            
            Read.close();
        }
        
        else
        {
            Read.close();
            return false;
        }
        
        return true;
    }    
    
    ////////////////////////////////////////////////////////////////////////////////
    // End of function.
    ////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////
    // MAIN
    ////////////////////////////////////////////////////////////////////////////////
    
    int main()
    {
        ifstream S_Initiation ( "C:/Dev-Cpp/Programs/Random Program Testing/Random/Initiation.txt" );
        S_Initiation>> Initiation;
        
        S_Initiation.close();
        
        if ( Initiation != 0 )
        {
            InitiationFunction();
            
            while ( Apassword != Apassword2 || Apassword == "" || Apassword2 == "" )
            {
                system("cls");
                InitiationFunction();
            }
        }        
        
        cin.get();
    }
    My problem is that if the user inputs a letter when it first says desired password, and hits backspace, the program outputs some wierd message and then closes so I can't read it. The thing that gets me is that when it says confirm password, it works fine, and as far as I can see, the code is exactly the same except from the variable names.

    Also I was doin' some testing on the two password variables, printing them just before the while loop in the main function. The other problem is that I separated them with a new line, but it totally misses out the newline. I also put some text after printing the first variable, but it printed the text first, then a newline, then the variables.

    I have got no idea what's going on here. Does this happen to anyone else?
    Great thanks to anyone who can help.
    Last edited by Necrofear; 11-05-2006 at 06:35 AM.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I don't have time to have a good look I am sure others can help you more, but I noticed this:

    Code:
    int Initiation;
    
    int Asize;
    char Ainput;
    string Apassword;
    string Apassword2;
    You have delcare these as global variables. Try to make these local ( or static )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM