mardi 28 juin 2016

C++ Text adventure game - Make words upper case

Basically, i'm following this tutorial : http://cplussplussatplay.blogspot.com.cy/2012/11/text-adventure-games-c-part-1.html

I have understood everything up to now except from this:

// Make words upper case
// Right here is where the functions from cctype are used
    for(i = 0; i < words.size(); i++)
    {
        for(j = 0; j < words.at(i).size(); j++)
        {
            if(islower(words.at(i).at(j)))
            {
                words.at(i).at(j) = toupper(words.at(i).at(j));
            }
        }
    }

At this point, we have a words vector which is full of characters. I don't understand the need of two for loops, nor this words.at(i).at(j)). Is it a 2D vector or something?

Thanks in advance.

EDIT : Thank you all very much for your help! I have understood it now! This is the first time using Stack Overflow and i'm loving it so far! :)

One more thing, another question has arised!

string sub_str;
vector words;
char search = ' ';



    // Clear out any blanks
    // I work backwords through the vectors here as a cheat not to       invalidate the iterator
    for(i = words.size() - 1; i > 0; i--)
    {
        if(words.at(i) == "")
        {
            words.erase(words.begin() + i);
        }

1. What does the second comment mean?

2.How can there be blanks in the vector? The first loops according to the creator clears out any blanks. This is the code previously :

for(i = 0; i < Cmd.size(); i++)
    {
        if(Cmd.at(i) != search)
        {
            sub_str.insert(sub_str.end(), Cmd.at(i));
        }
        if(i == Cmd.size() - 1)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
        if(Cmd.at(i) == search)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
    }

Thanks again!

Aucun commentaire:

Enregistrer un commentaire