mercredi 1 février 2017

Access Violation Writing Location Stack Overflow error C++

I was given a project to fix as a way of getting a taste of C++. The basic concept is to compare a book category with the current category, and if it matches, add it onto the stack, but otherwise, don't.

This is the code that I've got:

StoragePile::StoragePile(string categoryB) {
PileCategory = categoryB;
}

bool StoragePile::AddBook(Book b) {
    if (b.category == PileCategory) {
        AddBook(b);
        return true;
    }
    else {
        cout << "Book is not of the same category as this storage pile." << endl;
        return false;
    }
}

Stepping through it shows me that it's continually iterating over the lines if (b.category == PileCategory) { and AddBook(b);, and never returns to Tru or False. Eventually, it gives off 2 errors:

Unhandled exception at 0x0F985B2D (ucrtbased.dll) in Practical attempt.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00802FFC).

Followed by:

Exception thrown at 0x77885D28 (ntdll.dll) in Practical attempt.exe: 0xC0000005: Access violation writing location 0x00800FA8.

If there is a handler for this exception, the program may be safely continued.

Why is this happening? What is the fix for it?

issue with accepting user input [Java] [duplicate]

This question already has an answer here:

My program creates an array and asks the user whether he wants to sort it or not by entering 'Y' or 'N'. When I enter anything as the input, it still says "Invalid Input"..

Code:

        do{
        System.out.println ("Do you want to sort your array? (Y/N)");
        System.out.print   ("> ");
        ch = br.readLine().toUpperCase();

        if(!ch.equals('N')||!ch.equals("Y")) System.out.println("Invalid Entry. Please Enter 'Y' or 'N'");
        else break;
    }while(true);

    if(ch.equals("N"))
    {
        System.out.println("Thank You.");
        return;
    }

OUTPUT:
Do you want to sort your array? (Y/N)
Y
Invalid Entry. Please Enter 'Y' or 'N'
Do you want to sort your array? (Y/N)

How can you compare a variable to an array in PowerShell?

I am attempting to create an audit of mailbox permissions in powershell and would like to remove specific accounts from the output within the powershell script rather than manually afterwards.

In order to do this I am looking for a way to compare the contents of an array against a single string in powershell.

For example, if I was to declare an array:

$array = "1", "2", "3", "4"

I then want to find a way to do something like the below:

$a = "1"
$b = "5"

if ($a -ne *any string in $array*) {do something} #This should return false and take no action

if ($b -ne *any string in $array*) {do something} #This should return true and take action

I am at a loss for how this would be accomplished, any help is appreciated

Second If statement gets called before first statement finished in one function

I have one function with 2 if statements inside it. Code:

func pictureFromFirebase(loginMethod: Int)
    {
    if loginMethod == 0 //FB
    {
        var profilePic = FBSDKGraphRequest(graphPath: "me/picture", parameters: ["height":300, "width":300, "redirect":false], httpMethod: "GET")
        let profilePicRef = storageRef.child((user?.uid)!+"/profile_pic.jpg")
        profilePicRef.data(withMaxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                // Uh-oh, an error occurred!
            } else {
               if (data != nil)
               {
                print("no need to download image from facebook")
                self.profileImage.image = UIImage (data: data!)
                }
            }
        }
        if profileImage.image == nil
        {
        print("downloading image from facebook")
        profilePic?.start(completionHandler: {(_ connection, _ result, _ error) -> Void in
            if (error == nil)
            {
                if let dictionary = result as? [String:Any], let data = dictionary["data"] as? [String:Any],
                    let urlPic = data["url"] as? String {
                    if let imageData = NSData(contentsOf: NSURL(string: urlPic)! as URL)
                    {
                        let uploadTask = profilePicRef.put(imageData as Data, metadata: nil){
                         metadata, error in
                            if (error == nil)
                            {
                                let downloadUrl = metadata!.downloadURL
                            }
                            else
                            {
                                print("error in downloading image")
                            }
                        }
                        self.profileImage.image = UIImage(data: imageData as Data)
                    }
                }

            }
        })
    }
    }
    }

It gets only called when this function is triggered:

FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
            print("called")
            if let user = user {
                if(activeUser != user){
                let name = user.displayName
                self.profileName.text = name
                self.pictureFromFirebase(loginMethod: 0)
                }
            }

This is the normal output from the debug, when I already have set an image so downloading an image from Facebook is not needed:

-called -downloading image from facebook -called -downloading image from facebook -no need to download image from facebook -no need to download image from Facebook

This is the output from the debug when I remove the whole second if statement (if profileImage.image == nil):

-called -called -no need to download image from facebook -no need to download image from Facebook

From what I can see, apart that the function are called twice which is also wrong, the second statement gets called before the first statement is executed. Since the first statement takes a while before the profileImage is set, its nil when the second if statement is checking this value. How can I make sure this won't happen anymore? Thank you.

Deleting elements from a list if they do not follow 'if' 'or' statements

I am trying to get rid of unwanted variables in a list. I need to have two condition: one is if making sure the values in my array are smaller than a variable A, and the other is making sure they are not equal to another variable B.

This code dose not work:

original_Ar = [0,1,2,3,4,5,6,7,8,9,10,11,12]
new_Ar = [s for s in original_Ar if (s != 2) or (s < 10)]

print (new_Ar)

while if I split it into two statements (instead of the 'or'statement) - they do work:

original_Ar = [0,1,2,3,4,5,6,7,8,9,10,11,12]
print ([s for s in original_Ar if (s != 2)])
print ([s for s in original_Ar if (s < 10)])

Any idea how can I do that in one line?

Refactor repeated if statements

Is there a way to make this not have to check conditions multiple times

if(a || b || c){
    print('header');
    if(a){do_something_a();}
    if(b){do_something_b();}
    if(c){do_something_c();}
}else{
    print('alt-header');
}

I do not like the idea of needing to or all of the conditions before checking each one individually but I am not sure there is a better approach

if statement is giving me an error that i can't understand in java [duplicate]

This question already has an answer here:

I've got this code:

private void jToggleButton1MouseClicked(java.awt.event.MouseEvent evt) {                                            
    Connection c = MysqlConnect.ConnectDB();
    PreparedStatement s = null;
    ResultSet rs = null;

    int N_Mec = Utilizador.N_Mec;
    JOptionPane.showMessageDialog(null, N_Mec);

    try{
        final String SQL = " Select Password from utilizador where N_Mec=" +N_Mec ;
        s = c.prepareStatement(SQL);
        rs = s.executeQuery();

        if(rs.next()){
        JOptionPane.showMessageDialog(null, "if" );
        String PassOrigem = Password_origem.getText();
        String password = rs.getString(1);
        JOptionPane.showMessageDialog(null, PassOrigem );
        JOptionPane.showMessageDialog(null, password );
        if(PassOrigem == password) {
            JOptionPane.showMessageDialog(null, "passei");
        } else {
            //JOptionPane.showMessageDialog(null, "deu merda");
        }
        //String Pass = Password.getText();
        //String PassNova = Password_Nova.getText();
        }

    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

the thing is: it sould compare a password that comes from database and a password that the user give. The variables "PassOrigem" and "password" are the same, i writted them and they are. But the programme doesn't enter in the if statement (it goes directly to the else statement). Help me please ps: sorry for my bad english and i'm learning the lenguage so if you see something dumb, don't kill me.