2

I've recently made a short a program about a new lesson that I've learned online about programming(which I'm a beginner in)C++, which I do every time I learn a new concept, to be in-depth with the concept of it and it's uses.(Its how I teach myself in programming). I used vector in my program and it solved my problem. But, it created a new one.

(You might have already seen this code)

#include <iostream>
#include <string>
#include <windows.h>
#include <vector>

using namespace std;

int main()
{
    string LetterInput, LetterLoad, input;
    string Words[5] = {"fire","eggs","roll","camera","lotion"};
    vector <string> PossibleAnswers;
    int Number,a = 0;
    int Size,Num;

    cout << "Lets play HANGMAN! " << endl;
    Sleep(500);

    cout << "Think of a word and type in the number" << endl;
    cout << "of letters there are" << endl;
    cin >> Size;

    for (int i = 0; i <= Size; i++){
        LetterLoad += "_";
    }

    Num = sizeof(Words)/sizeof(string);

    for (int i = 0; i <= Num ; i++){
        if ((unsigned)Size == Words[i].size()){
            PossibleAnswers.push_back(Words[i]);
        }
    }


    for (int i = 0;i <= Num;i++){
        cout << PossibleAnswers[i] << endl;
    }

    cout << "Okay lets start" << endl;
    Sleep(750);

    while(a == 0)
   {

    cout << PossibleAnswers[0] << endl;
    cout << PossibleAnswers[1] << endl;

    cout << LetterLoad << endl;

    cout << "Type in the position of the letter you want to guess" << endl;
    cin >> Number;

    cout << "What letter do you want to put?" << endl;
    cin >> LetterInput;

    LetterLoad[Number-1] = LetterInput[0];

   for (size_t i = 0; i <= PossibleAnswers.size(); i++){
    for (int n = 0; n <= Size; n++){
        if (LetterInput[n] == PossibleAnswers[i][n]){
            cout << "Got one" << endl;
        }
    }
   }

   }
    return 0;

}

The program was able to take the right words. But, it stops working when it is about to reach the cout << "Okay lets start" << endl; and then everything below that line of code. I have heard that vectors require "memory allocation" from other people. Does that have something to with the program not running properly? and how do I fix it?

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Jan 31 '19 at 08:58
  • `sizeof(Words)/sizeof(string);` while correct, safer is `sizeof(words)/sizeof(*words)` (or `.../sizeof(words[0])`, if you prefer): If you exchange type, lets say from string to wstring, you don't need to adjust... – Aconcagua Jan 31 '19 at 09:03
  • 1
    @Aconcagua: Even safer: `template constexpr std::size_t size(const T (&)[N]) {return N;}` and then `size(Words)`. – Jarod42 Jan 31 '19 at 09:07
  • 1
    *'[...] that vectors require "memory allocation" [...]'* - depends on the point of view... `std::vector` *does* allocate the memory it needs dynamically - but it hides that away from you, so you as just using it don't have to cope with (that's the main reason `std::vector` exists at all...). – Aconcagua Jan 31 '19 at 09:10
  • 2
    OP, suggestion for the next course: subdivide a program into testable functions and tests them with unit tests. This will be a **huge boost** in your programming skills. – YSC Jan 31 '19 at 10:34

2 Answers2

2

If the condition if ((unsigned)Size == Words[i].size()){ is not met for any number of cases, then you won't have pushed back enough strings. When that happens, you get a crash in the following for (int i = 0;i <= Num;i++){ loop because you're trying to access more element that you have. I would recommend doing this instead:

for (std::string &s : PossibleAnswers){
    std::cout << s << std::endl;
}

You can also do a loop from 0 to PossibleAnswers.size(), like you do further below.

I have heard that vectors require "memory allocation" from other people.

Nah, you must have misunderstood something. This was just an out of range error, I recommend to avoid those by always looping though vectors using range based for loops or by looping from 0 to vec.size().

Blaze
  • 16,736
  • 2
  • 25
  • 44
0

You are asking questions about: I am trying to write an essay, but in fact, you don't know the alphabet. In my experience as a teacher, it's better to learn the programming basics first, rather than learning programming with libraries. This statement from you proves my opinion:

I have heard that vectors require "memory allocation" from other people.

Learn the different types of memory blocks. It is necessary for every programmer.

std::string
std::vector

These are linear data structures - don't use them, if you cannot implement them by yourself.

while (a==0) ...

You are allocating 4 bytes (in most architectures nowadays) for variable, which you don't even use it. Most of the compilers will change that code to:

while (true) ...

Because it is equivalent to it.

Everytime someone writes a magical number - a kitty around the world dies. Don't use magical numbers, save a kitty. Even in training sessions. You had time to write the description to the console, but you didn't make a const variable for the size of the array of strings.

It is good that you are motivated to learn programming. But if you are learning it wrong, there won't be any good results. My advice is to change the source, which you are learning from.

  • 3
    "don't use them, if you cannot implement them by yourself" Given that you can't portably re-implement `std::vector`, this is rather extreme. And one of the most useful parts of programming is using abstraction. – Caleth Jan 31 '19 at 09:22
  • Implementing a simple std::vector is not even part of the data structures course. Its implementation is about 30 mins. Sorry, sir, but I cannot agree with you about that. But before you learn what is an abstraction, you need to know what is variable, pointer, different types of memory blocks. – Radoslav Mitev Jan 31 '19 at 10:00
  • 3
    No, implementing something *similar to* `std::vector` is simple. Actually doing *all* the things that `std::vector` does requires implementation defined behaviour – Caleth Jan 31 '19 at 10:06
  • 2
    _"it's better to learn the programming basics first, rather than learning programming with libraries"_ I strongly disagree: `std::vector` (as of all the standard library) _is part of the language_. Dissociating the two means teaching C with classes. This was fine back in the 80's, it is now unacceptable. – YSC Jan 31 '19 at 10:28
  • 1
    notice: I'm not the downvoter; I generally don't express opinion with votes. – YSC Jan 31 '19 at 10:30