c++ - Scanning User Input for Strings Declared in an Array -
i creating program scans user input words listed in array. find()
function seems it'll work, can't find showing how implement want do. i'm pretty new 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 you","how's going","sup dude","hey","hello"}; string negvibe [4]={"bad","terrible","lousy","meh"}; string userfeeling; int main() { srand(time(0)); int rando = rand() %5;//generates random number between 0 , 4 int rando1 = rand() %5; int rando2 = rand() %5; cout << greeting [rando1] << "." << endl; getline(std::cin,userfeeling); if .... // has done here? find(negvibe, negvibe + 4, userfeeling) != negvibe + 4); // that? // ... { cout << subj[rando] << verb[rando1] << obj[rando2] <<"." <<endl; } return 0; }
that find
function find element of negvibe
array equal userfeeling
. if checking whether element of negvibe
substring of userfeeling
, should loop through negvibe
, use std::string::find
method.
bool found_negvibe = false; (int = 0; < sizeof(negvibe) / sizeof(*negvibe); i++) { found_negvibe = found_negvibe || userfeeling.find(negvibe[i]) != string::npos; }
also, don't need specify size of negvibe
array, can write this:
string negvibe[] = {"bad","terrible","lousy","meh"};
one more thing, might prefer use std::vector
on array, if because c++'s faculties getting size of vector more succinct getting size of array.
vector negvibe = {"bad","terrible","lousy","meh"}; bool found_negvibe = false; (int = 0; < negvibe.size(); i++) { found_negvibe = found_negvibe || userfeeling.find(negvibe[i]) != string::npos; }
Comments
Post a Comment