-2

This program is supposed to have a tortoise and hare race each other and print the race out for the user. It is giving me a segmentation fault and I do not know why. I even went through the whole code with a pen and paper to see if it worked and to my knowledge it should work but I am also an amateur to C++ so I do not know.

#include <iostream>
#include <stdlib.h> 
#include <ctime>
#include <iomanip>
#define RACE_LENGTH 50

void advanceTortoise(int* ptrTortoise)
{
    int torNum = rand()%10+1;
    if (torNum <= 6)
    {
        *ptrTortoise = *ptrTortoise + 1;
    }else if (torNum = 7) {
        *ptrTortoise = *ptrTortoise + 2;
    }else if (torNum = 8){
        *ptrTortoise = *ptrTortoise + 3;
    }else if (torNum > 8){
        *ptrTortoise = *ptrTortoise;
    if (*ptrTortoise > 50)
    {
        *ptrTortoise = 50;
    }
        return;
    }
}
void advanceHare(int* ptrHare)
{
    int hareNum = rand()%10+1;
    if (hareNum = 1)
    {
        *ptrHare = *ptrHare + 1;
    }else if (hareNum > 1 && hareNum <= 4){
        *ptrHare = *ptrHare + 2;
    }else if (hareNum > 4 && hareNum <= 7){
        *ptrHare = *ptrHare + 3;
    }else if (hareNum = 8){
        *ptrHare = *ptrHare - 2;
        if (*ptrHare < 1)
        {
            *ptrHare = 1;
        }

    }else if (hareNum > 8){
        *ptrHare = *ptrHare - 3;
        if (*ptrHare < 1)
        {
            *ptrHare = 1;
        }
    if (*ptrHare > 50)
    {
        *ptrHare = 50;
    }
        return;
    }
}
void printPosition(int* ptrTortoise, int* ptrHare)
{
    if (*ptrTortoise = *ptrHare)
    {
        *ptrTortoise = *ptrTortoise - 1;
    }
    if (*ptrTortoise > *ptrHare)
    {
    std::cout << std::setw(*ptrHare - 1) << "H" << std::setw(*ptrTortoise - *ptrHare) << "T" << std::setw(51 - *ptrTortoise) << "|" <<std::endl;
    }
    if (*ptrHare > *ptrTortoise)
    {
    std::cout << std::setw(*ptrTortoise - 1) << "H" << std::setw(*ptrHare - *ptrTortoise) << "T" << std::setw(51 - *ptrHare) << "|" <<std::endl;
    }
    }
int main()
{
    srand(time(NULL));
    int* ptrTortoise;
    int* ptrHare;
    *ptrTortoise = 1;
    *ptrHare = 1;
    while(*ptrTortoise < 50 && *ptrHare < 50)
    {
        advanceHare(ptrHare);
        advanceTortoise(ptrTortoise);
        printPosition(ptrTortoise, ptrHare);
    }

    if (*ptrHare = 50)
    {
        std::cout<<"The Hare has won"<<std::endl;
    }else{
        std::cout<<"The Tortoise has won"<<std::endl;
    }
}
  • 3
    You also need to learn the difference between assignment using `=` and comparing for equality with `==`. – Some programmer dude Feb 09 '18 at 21:52
  • As for your problems, you define a couple of pointer variables, but nowhere do you make them actually *point* anywhere. Learn how to use the *address-of operator `&`*. Then you don't need any pointers in the `main` function. Or better yet, how to `return` values from your functions, and you won't need any pointers at all here. [Get a couple of good beginners books](https://stackoverflow.com/a/388282/440558) to read! – Some programmer dude Feb 09 '18 at 21:53
  • 2
    Why are you using pointers here? You never allocate any memory for them in main. – NathanOliver Feb 09 '18 at 21:53
  • 1
    You can't just assign to a pointer like that. First you have to allocate memory (new would be a good idea here, or better yet, get rid of the pointers). If you want to modify a parameter, use references. – Arnav Borborah Feb 09 '18 at 21:54
  • Also you need to turn the warnings on for whatever compiler you're using. gcc, clang, and Visual Studio all will complain about the incorrect lines of code. – zzxyz Feb 09 '18 at 22:12

1 Answers1

1

A few things here.

  1. You cannot assign a value to a pointer without allocating any memory for it.
  2. You shouldn't be using pointers if you want to modify variables. References were meant for this.
  3. If you want to check if two values are equal, then use the equality (==), not the assignment (=) operator.

Thus, you should change:

int* ptrTortoise;
int* ptrHare;

To:

int* ptrTortoise = new int{ 1 };
int* ptrHare = new int{ 1 }; // Don't forget to get rid of the two lines after this

Or better yet:

int tortoise = 1;
int hare = 1;

If you used the last option, then change all your function signatures to accept a reference (Such as changing int* ptrTortoise to int& ptrTortoise). Next, remove all instances where you have to dereference a pointer pretty much everywhere in the code and instead just directly assign to the variables, which makes more sense and is less typing (Aren't references great?).

Rule of Thumb: Don't use pointers unless you absolutely have to.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88