i want to create like a very basic AI for a console game. i need a function that will recognize 2 or 3 words within a string created via cin. any help would be greatly appreciated as iv been at this for a while now and cant seem to find a solution.
#include <string>
#include <iostream>
using namespace std;
//-------------------------------------------globals
string sentence;
int main()
{
getline(cin, sentence);
string search;
size_t pos;
search = "hi";
pos = sentence.find(search);
if (pos != string::npos)
cout << "hello sir " << endl;
else
search = "cya";
pos = sentence.find("cya");
if (pos != std::string::npos)
std::cout << "goodbye sir " << endl;
else
search = "goodbye";
pos = sentence.find("goodbye");
if (pos != std::string::npos)
std::cout << "shuting down sir " << endl;
else
return 0;
}
what i would like is to have the initial search look for other words such as hello hey, and so forth, and the similer thing with the others
Related
I am trying to get a frequency table from a given text.
But the output is showing segmentation fault 11. I don't know why.
I am a newbie. Your help with the code will be very well appreciated.
You can edit my code to make me learn a better method of writing code as well. Thanks a lot.
#include<iostream>
#include<map>
#include<string>
#include<vector>
void make_table(vector<pair<char, int> > &table , string path){
string text = "Hello thusnvkj.ernbuilvgqboipghq3pojavnaj.,fbvlkarebihfg094why091[3tugjvlksbdfv ajklvrpt-30qjhrgiaoehk.BL;H]IH;LGBJSFDNOWI;HBPWRHGB;ORTWIHGOQHRWI0TUGJRLKEWHUGIH49P0-IT302-UR9GM,NXM,BNX,MNMB/E/RGP'KGP34OR[2=O-O-=0-3-1I0-439890375892R0U;L.GNLS.N.SVMS/FS/FKWEP[IF0W))_*(&*(^^&$%#^%$&%*(^*&)(*)_*_(()())))]]'";
map<char, int> m;
for(int i=0; text[i]!='\0'; i++){
m[text[i]]++;
}
map<char,int>::iterator it;
int j=0;
for(it=m.begin();it!=m.end(); it++){
table[j].first=it->first;
table[j].second=it->second;
j++;
//cout << it->first << " " << it->second << endl;
}
return;
}
int main(){
vector<pair<char , int> > table;
string path;
//cin >> path;
path= "Hi";
make_table(table, path);
// make_table function will give us the sorted table(vector of pair) in the decreasing order of frequency of a character.
/*for(int i =0; i<table.size(); i++){
cout << table[i].first << " " << table[i].second << endl;
}
*/
return 0;
}
You get an error because you try to access elements in an empty vector
table[j].first=it->first;
table[j].second=it->second;
Here you can add one line outside the for loop
table.resize(text.size());
I am new to OpenMP... Please help me with this dumb question. Thank you :)
Basically, I want to use OpenMP to speed up two for loops. But I do not know why it keeps saying: invalid controlling predicate for the for loop.
By the way, my GCC version is gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and OS I am using is Ubuntu 16.10.
Basically, I generate a toy data that has a typical Key-Value style, like this:
Data = {
"0": ["100","99","98","97",..."1"];
"1": ["100","99","98","97",..."1"];
...
"999":["100","99","98","97",..."1"];
}
Then, for each key, I want to compare its value with the rest of the keys. Here, I sum them up through "user1_list.size()+user2_list.size();". As for each key, the sum-up process is totally independent of other keys, which means this works for parallelism.
Here is my toy example code.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include "omp.h"
using namespace std;
int main(){
// Create Data
map<string, vector<string>> data;
for(int i=0; i != 1000; i++){
vector<string> list;
for (int j=100; j!=0; j--){
list.push_back(to_string(j));
}
data[to_string(i)]=list;
}
cout << "Data Total size: " << data.size() << endl;
int count = 1;
#pragma omp parallel for private(count)
for (auto it=data.begin(); it!=data.end(); it++){
//cout << "Evoke Thread: " << omp_get_thread_num();
cout << " count: " << count << " / " << data.size() << endl;
count ++;
string user1 = it->first;
vector<string> user1_list = it->second;
for (auto it2=data.begin(); it2!=data.end(); it2++){
string user2 = it2->first;
vector<string> user2_list = it2->second;
cout << "u1:" << user1 << " u2:" << user2;
int total_size = user1_list.size()+user2_list.size();
cout << " total size: " << total_size << endl;
}
}
return 0;
}
When I run it on the terminal it works fine but the loop. The for loop just doesn't do anything at all. I'm learning C++, so I don't know much.
#include <iostream>
#include <cstring>
using namespace std;
int main( int argc, char *argv[] ) {
if (argc == 2) {
cout << "The first argument is " << argv[0] << endl;
cout << "The second argument is " << argv[1] << endl;
} else if (argc > 2) {
cout << "Too many arguments" << endl;
exit(0);
} else {
cout << "Only one argument" << endl;
cout << "The argument is " << argv[0] << endl;
exit(0);
}
if (atoi(argv[1]) < 0) {
cout << "Error negative number" << endl;
exit(0);
}
// this loop does not work, everything else does.
for (int i = 1; i >= atoi(argv[1]); i++){
int count = atoi(argv[1]--);
cout << count << endl;
int sum = sum + i;
}
cout << "The sum is: " << endl;
return(0);}
I think that could be the if statements what are messing around with the loop.
I think you made mistake in the for loop.
You show use "<=" instead of ">=" in the for loop.
Hope this might helps you.
I guess your code is not reaching the for loop as you have exit() conditions on each and every condition of if. Your code only reaches the loop if you are passing 2 arguments in the terminal while you are running your code
I am new and learning C++ using the Programming Principles ... book by Bjarne Stroustrup. I am working on one problem and can't figure out how to make my code work. I know the issue is with if (words[i]==bad[0, bad.size() - 1]) in particular bad.size() - 1])
I am trying to out put all words in the words vector except display a bleep instead of any words from the words vector that match any of the words in the bad vector. So I need to know if words[i] matches any of the values in the bad vector.
#include "../std_lib_facilities.h"
using namespace std;
int main()
{
vector<string> words; //declare Vector
vector<string> bad = {"idiot", "stupid"};
//Read words into Vector
for(string temp; cin >> temp;)
words.push_back(temp);
cout << "Number of words currently entered "
<< words.size() << '\n';
//sort the words
sort(words);
//read out words
for(int i = 0; i < words.size(); ++i)
if (i==0 || words[i-1]!= words[i])
if (words[i]==bad[0, bad.size() - 1])
cout << "Bleep!\n";
else
cout << words[i] << '\n';
return 0;
}
You need to go through all of the entries in the bad vector for each entry in the words vector. Something like this:
for(const string& word : words)
{
bool foundBadWord = false;
for(const string& badWord : bad)
{
if(0 == word.compare(badWord))
{
foundBadWord = true;
break;
}
}
if(foundBadWord)
{
cout << "Bleep!\n";
}
else
{
cout << word << "\n";
}
}
I am trying to use a number randomly selected between 1 and 2 and use that to display a random cout. I can't quite get this to work. Any suggestions? Thanks!
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
for (int i = 1; i <= 1; i++)
{
int d1 = rand() % 2 + 1;
cout << d1 << endl;
cout << endl;
system("pause");
if (d1 == "1");
{
cout << "hello";
}
if (d1 == "2")
{
cout << "goodbye";
}
return 0;
}
First i recommend you make sure that you are not comparing an int to a string. Also remove the semicolon from after the first if statement.
if (d1 == 1)
{
cout << "hello";
}
Next make an else if statement instead of a new if statement.
else if (d1 == 2)
{
cout << "goodbye";
}
This should fix just about everything. Hope this helps. Goobyebye!
You're comparing an int to a string. change to, e.g. d1 == 1
That should get you going...
Compare the int to an int, then use the else clause.
Also, your if statement doesn't need a semicolon.
if (d1 == 1)
{
cout << "hello";
}
else
{
cout << "goodbye";
}
When you make a statement like:
if (d1 == "1")
You are comparing a string and an integer. Try:
if(d1 == 1)