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

I have a C problem. I need to read the data of a txt file to...
I have a C problem. I need to read the data of a txt file to array by struct, then use these data to sum or sort. The txt file and the struct like aa.txt 1 2 3 4 ***************************** struct aaa{ int num1,num2; }bbb[2]; num1 for 1,3, num2 for 2 4 I made a void readfile () function for read data. How can I pass the numbers to another function ( void sum () and void sort() ) for...
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...
problem should be done in C++ read in the adjacency matrix for a graph (with 5...
problem should be done in C++ read in the adjacency matrix for a graph (with 5 vertices) from a file, "adjacency.txt".  An example file is here:  adjacency.txt This example file contains the following matrix: 1 2 4 1 0 2 0 1 1 3 4 1 2 1 0 1 1 1 0 1 0 3 0 1 3 Store the information in a 2D array. to return the degree of a vertex in a generic graph. For example, the following...
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...
C language problem. Suppose I open I file and read a integer 24. And I want...
C language problem. Suppose I open I file and read a integer 24. And I want to store them into a array byte []. The answer should be byte[0] = 0x9F. How can I do that?
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);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT