Question

In: Computer Science

C++ - Almost done, I have a problem with read access violation error. This is the...

C++ - Almost done, I have a problem with read access violation error. This is the link to my assignment first part: https://pastebin.com/yvcvdqLY

second part: https://pastebin.com/vY7MK1Bf,

My header file: https://pastebin.com/pcJnctgu

My .cpp file: https://pastebin.com/9jAfP9u8

My source file: https://pastebin.com/kFsidY2k

Solutions

Expert Solution

Input:

main.cpp:

#include "GorgVsBoov.h"


Boov::Boov(std::string Name, int Health)

{

    name = Name;

    health = Health;

}

std::string Boov::GetName()

{

    return name;

}

int Boov::GetHealth()

{

    return health;

}

bool Boov::IsDefeated()

{

    if (health <= 0)

        return true;

    else

        return false;

}

void Boov::Print()

{

    std::cout << "Boov's name: " << name << std::endl;

    std::cout << "Boov's health: " << health << std::endl;

    if (Boov::IsDefeated() == 0)

        std::cout << "Boov isn't defeated yet!" << std::endl;

    else

        std::cout << "Boov is defeated!" << std::endl;

}

void Boov::GetsAttacked(Boov& Target, int attack, int damage)

{

    if (attack > 1)

        Target.health -= damage;

}

Gorg::Gorg(std::string Name, int Health)

{

    name = Name;

    health = Health;

}

std::string Gorg::GetName()

{

    return name;

}

int Gorg::GetHealth()

{

    return health;

}

bool Gorg::IsDefeated()

{

    if (health <= 0)

        return true;

    else

        return false;

}

void Gorg::Print()

{

    std::cout << "Gorg's name: " << name << std::endl;

    std::cout << "Gorg's health: " << health << std::endl;

    if (Gorg::IsDefeated() == 0)

        std::cout << "Gorg isn't defeated yet!" << std::endl;

    else

        std::cout << "Gorg is defeated!" << std::endl;

}

void Gorg::GetsAttacked(Gorg& Target, int attack, int damage)

{

    if (attack > 1)

        Target.health -= damage;

}

int main()

{

    srand(static_cast<unsigned int>(time(NULL)));

    double count = 0;

    int gorgWins = 0;

    int boovWins = 0;

    int size = 100000;

    std::cout << "Creating one Gorg object to test the Print() function" << std::endl;

    Gorg george("George", 10);

    george.Print();

    std::cout << "\nCreating 100,000 Boov objects to get the average times the Boov can take damage before it is defeated" << std::endl;

    Boov **boovArr = new Boov*[size];

    Gorg **gorgArr = new Gorg*[size];


    for (int i = 0; i < size; ++i)

    {

        int health = rand() % 5 + 7;

        boovArr[i] = new Boov("Oh", health);

        gorgArr[i] = new Gorg("George", 10);

        // if (gorgArr[i]->IsDefeated() == 1)

        //  gorgArr[i] = new Gorg("George", 10);

        

        while (boovArr[i]->IsDefeated() == 0)

        {

            int gAttack = rand() % 3 + 1;

            int gDamage = rand() % 4 + 2;

            int bAttack = rand() % 2 + 1;

            int bDamage = rand() % 2 + 2;

            std::cout << bAttack << std::endl;

            gorgArr[i]->Print();

            std::cout << bDamage << std::endl;

            gorgArr[i]->GetsAttacked(*gorgArr[i], bAttack, bDamage);

            boovArr[i]->GetsAttacked(*boovArr[i], gAttack, gDamage);

            if (boovArr[i]->IsDefeated() == 1)

                ++gorgWins;

            if (gorgArr[i]->IsDefeated() == 1)

                ++boovWins;

            ++count;

        }

    }

    std::cout << "100,000 Boov killed after an average of " << count / 100000 << " tries" << std::endl;

    std::cout << gorgWins << std::endl;

    std::cout << boovWins << std::endl;

    system("PAUSE");

    return 0;

}

Output:

Explanation:

  • I have combined your file (class.cpp) and source file (main.cpp) in the same code so that it's easy to navigate. I found a semantic error in the main function.
  • In your main function, there is a conditional block
    if (gorgArr[i]->IsDefeated() == 1)
    gorgArr[i] = new Gorg("George", 10);

    You are basically trying to access a memory location before an object is assigned to it.
  • This will throw an exception since it's still unallocated space.
  • Since all Gorg have a health of 10, there is no point in keeping this conditional block before the battle i.e. while loop
  • Also, each Gorg in the array must be initialized. So the flow of execution will be Boov initialization, Gorg initialization and then battle. You will find the same in the modified code.

Related Solutions

PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! AS YOU CAN SEE I AM DONE WITH ALMOST...
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! AS YOU CAN SEE I AM DONE WITH ALMOST HALF THE TABLE. I DIDN'T FIND A SINGLE AMOUNT FOR THE COLUMNS: RENT EXPENSE, SALARIES EXPENSE, SUPPLIES EXPENSE, AUTO EXPENSE, MISC EXPENSE. THE SAME QUESTIONS HAS BEEN ASKED MANY TIMES ON CHEGG, BUT ALL THE AMOUNTS POSTED FOR THOSE MISSING COLUMNS ARE WRONG IN EACH AND EVERY SINGLE POST, SO PLEASE DON'T COPY AND PASTE FROM ANY OF THEM. I LITERALLY CHEKCED EACH AND...
Can anyone explain to me why I am getting an access violation when I try to...
Can anyone explain to me why I am getting an access violation when I try to add notes? It occurs when I try to set the "pLast" pointer to the previous node (for a doubly linked list). The code worked as singly linked list. When I added a "pLast" pointer and the needed code to make it a doubly linked list everything broke. #include <iostream> #include <stdlib.h> using namespace std; //build class that has a private function to inc count....
Below is a school problem of mine. WHAT I KNOW AND HAVE DONE. i have three...
Below is a school problem of mine. WHAT I KNOW AND HAVE DONE. i have three variables for input n for nunber of wnemies k for fight capacity an arraylist set to the size of n because it only needs to be as large as the number of enemies coming. and x which is just the time stamps that will go into the arraylist. i also have tHe array sorted from least to greatest because it doesnt matter what order...
I have done the first part of this problem, but I cannot figure out the second...
I have done the first part of this problem, but I cannot figure out the second part. I know there are several different answers already posted on Chegg for this particular problem with different acceleration and time, but I need this one specifically, with a step by step solution. Again...just PART B....the answer to Part A is 201.72 A helicopter carrying Dr. Evil takes off with a constant upward acceleration of 4.20 m/s2. Secret agent Austin Powers jumps on just...
hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
I need almost two pages for this section Section #3: Issues Regarding Data Access With the...
I need almost two pages for this section Section #3: Issues Regarding Data Access With the amount of information being stored allowing easier access along with the ability to cross-reference many different sources, do you see any areas needing improvement? This could be achieved either by better organization for easier access or through more regulation on availability. Some examples to consider include the rights for the public to obtain data through the US Freedom of Information Act (FOIA) after a...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I have a quick question about this C ++ problem in my intro class. I have...
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2) Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT