jeudi 6 août 2015

How to use an if statement in a string array?

The following code is supposed to randomly pick a string from the array and then say "Yay!" if Happy has been chosen.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

int main()
{

    srand(time(NULL));
    string textArray[4] = { "Happy", "Sad", "Mad", "Overjoyed." };
    int RandIndex = rand() % 4;
    cout << textArray[RandIndex] << endl;
    if (textArray == "Happy") 
    {
        cout << "Yay!" << endl;
    }
    cin.get();
}

My problem is that the operand types are incompatible with strings and chars. What would be the best solution to this problem?




Aucun commentaire:

Enregistrer un commentaire