0

I am trying to program my own hangman game without refrencing other programs as a way to get back into programming.

Right now I am trying to program a structure that will hold all of the letters that will be alphabetized and displayed for the user. In the middle of trying to realloc i got this error

34  67  C:\Users\hanna\Documents\C Codes\Testing Hangman.cpp    [Error] invalid conversion from 'void*' to 'Guess*' [-fpermissive]

Here is the code I am working with right now:

#include <ctime>
#include <stdio.h>
#include <iostream>
#include<stdlib.h>
#include <cstdlib> //has rand function
using std::cout;
using std::cin;

struct Guess {
        char Letter[1];
    };

int count = 0;
struct Guess*guessKeeper;
    
    
int main()
{

    char choice;

    cout << "Do you want to add another guess? \n";
    cin >> choice;
    cout << choice << "\n";
    
    if (choice == 'y')
    {
    
        struct Guess newGuess;
        
        cout << "What is your guess? \n";
        cin >> newGuess.Letter;
    
        guessKeeper = realloc(guessKeeper,(count+1)*sizeof(struct Guess));
            
        count++;
            
        guessKeeper[count-1] = newGuess;
        
        cout << "Do you want to add another guess? \n";
        cin >> choice;
    };
    
    free(guessKeeper); //Free Memory
}

Any recomendations on how to alphabetize would also be appretiated.

I have tried refrencing other online tutorials and some of my old code from my college classes; it is why I landed on structures as I have an old assignment that had used a structure and has code for alphabetizing I was hoping to refrence.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • why are you using `realloc` ? because of online tutorials? Try here https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Jan 23 '23 at 19:38
  • Use `std::string`. – n. m. could be an AI Jan 23 '23 at 20:02
  • `realloc` is unable to allocate C++ objects. Even if this code compiled and seemed to work, it would have undefined behavior. You should be very suspicious of any learning materials that instruct you to use `realloc` in C++. It's a tool for a different programming language. – Drew Dormann Jan 23 '23 at 20:32
  • You may not want to reference "other programs" but you do need to be referencing good learning material. For what you're trying to do, when implemented in anything resembling modern C++, you want to ignore malloc/realloc/free and use things like `std::vector` and `std::string`. – TheUndeadFish Jan 23 '23 at 21:12

1 Answers1

1

In C++ opposite to C you may not assign a pointer of the type void * to a pointer to an object type. You need to cast the return value of calls of for example realloc or malloc to the destination pointer type.

For example

guessKeeper = static_cast<Guess *>( realloc(guessKeeper,(count+1)*sizeof(struct Guess)) );

And in C++ you should use operators new and delete instead of realloc and free,

Also these statements in the end of the if statement

    cout << "Do you want to add another guess? \n";
    cin >> choice;

does not make sense because there is no loop in the program.

And this semicolon that defines a null-statement

    //...
};

is redundant. Remove it.

And leave only the second include among these two includes

#include<stdlib.h>
#include <cstdlib> //has rand function

And remove this include

#include <stdio.h>

Also as correctly pointed @Blindy this declaration of the data member of an array type with one element does not make sense

struct Guess {
        char Letter[1];
    };

because this statement

cin >> newGuess.Letter;

results in memory corruption. That is it will try to read a string instead of a single character.

Instead you could declare the structure like for example

struct Guess {
        char Letter;
    };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    i suppose, what comes closest to an alternative to `realloc` is `std::vector` – 463035818_is_not_an_ai Jan 23 '23 at 19:40
  • Also `char letter[1]` is nonsense, which is followed by pure memory corruption on the `cin >> newGuess.letter` line. – Blindy Jan 23 '23 at 19:48
  • And one more thing: if `realloc` fails it returns a null pointer; to manage that case, the code has to hold on to the original pointer until after it checks the return value. So, typically, `X *xptr = realloc(old_xptr); if (xptr) old_xptr = xptr; else std::cout << "reallocation failed\n";`. – Pete Becker Jan 23 '23 at 20:19