Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanning User Input for Strings Declared in an Array

Tags:

c++

arrays

I am creating a program that scans user input for words that are listed in an array. The find() function seems like it'll work, but I can't find anything showing how to implement it for what I want to do. I'm pretty new to programming (obviously).

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string subj [5]={"I","He","She","We","They"};
string verb [5]={" like"," hate"," sacrifice"," smell"," eat"};
string obj [5]={" bacon","s cats","s bagels","s children","s cops"};
string greeting [5]={"How are you","How's it going","Sup dude","Hey","Hello"};
string negvibe [4]={"bad","terrible","lousy","meh"};

string userfeeling;

int main()
{
    srand(time(0));
    int rando = rand() %5;//generates a random number between 0 and 4
    int rando1 = rand() %5;
    int rando2 = rand() %5;

    cout << greeting [rando1] << "." << endl;
    getline(std::cin,userfeeling);

    if .... // What has to be done here?
         find(negvibe, negvibe + 4, userfeeling) != negvibe + 4);
    // Something like that?

    // then ...
    {
        cout << subj[rando] << verb[rando1] << obj[rando2] <<"." <<endl;
    }
    return 0;
}
like image 702
face with skin Avatar asked Apr 20 '26 09:04

face with skin


1 Answers

To make find work properly you should user iterators like so

if(find(std::begin(negvibe), std::end(negvibe), userfeeling) != std::end(negvibe)){
  //code you want to happen if your word is found
}

Also in your current code, the if statement doesnt actually do anything since you end it with a semicolon and not {} or leave it blank if its one line. You can see an example of the if statement as well

Below is a link to find and iterators http://www.cplusplus.com/reference/algorithm/find/

like image 138
Lucas Hendren Avatar answered Apr 21 '26 23:04

Lucas Hendren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!