How can I use wildcards with string::find? - c++

I want to be able to use a wildcard in string::find and then fetch what was in that wildcard place.
For example:
if (string::npos !=input.find("How is * doing?")
{
cout<<"(the wildcard) is doing fine."<<endl;
}
And so if I ask, "How is Mom doing?", the output would be "Mom is doing fine."
What libraries would I use for this, or how would I write the code manually? If I should use AIML, can AIML execute .bat files?

Regarding your question on how to do it manually, i will give you an idea with this simple code, which solves the example which you gave in the question.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string expr="How is * doing?";
string input="How is Mom doing?";
int wildcard_pos=expr.find("*");
if(wildcard_pos!=string::npos)
{
int foo=input.find(expr.substr(0,wildcard_pos)),bar=input.find(expr.substr(wildcard_pos+1));
if(foo!=string::npos && bar!=string::npos)
cout<<input.substr(wildcard_pos,bar-wildcard_pos)<<" is doing fine\n";
}
}
You can easily modify this idea to suit your needs. Else, follow the answer given by src

C++ 2011 provides a regular expression library. If you can't use C++ 2011 yet you can use the Boost.Regex library or any C library like PCRE.

Related

How do I detect if a key is being held down in C++?

How do I detect if a key is being held down in C++? An example of how I'd like to use it:
while (varOne == 0) {
if ('A' Key Pressing Code Detection Magic Goes Here) {
std::cout << "The 'A' key has been pressed.";
varOne = 2;
}
}
I have found a few articles saying to use the conio.h library, however a bunch of people advise against using it. Should I use conio.h? Or is there some other method? (Not sure if it matters, but I'm using VS Code on Linux.)
Since your on linux, you might want to look for a curses library. Possible helpful info for ncurses (the modern C++ curses library):
https://www.linuxjournal.com/content/getting-started-ncurses,
https://invisible-island.net/ncurses/,
https://www.cyberciti.biz/faq/linux-install-ncurses-library-headers-on-debian-ubuntu-centos-fedora/.
I'm not a real expert on linux, but this may help.
The accepted answer here : Where is the <conio.h> header file on Linux? Why can't I find <conio.h>? explains a bit on conio.h and ncurses.

sending an email with C++ (Linux/Mac)

I was used to use Matlab, and for very long simulation I created a function which sent me an email whenever Matlab finished. It was a very easy Matlab function, you just had to add your email, password and the SMTP (I think).
Now, because of university stuff, I have to use C++ (I'm not very familiar with it, as you have probably guessed) but I can't find an equivalent way for sending an email to myself.
I compile my .cpp in Terminal, using g++.
Can you please help me? I don't know if I miss some libraries or what.
If you want to do this in C++ it would be the best to go for some library like
libquickmail
vmime
If it is ok for you to call some other program (like linux terminal program) go and check this stackoverflow answers send-mail-from-linux-terminal-in-one-line
Using the last method will leave with you with something like that (minimal example):
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[]){
int status;
status = system(R"(echo "this is the body" | mail -s "this is the subject" "to#address")");
return 0;
}
R"()" is c++ string literal so you don't have to care about escape characters (but is available since C++11).
Here see the documentation for system to check how it work.

C++ Static Code Analysis - Show header used in statement or line

I'm searching for a tool to get the used header (if there is one/more) for every line/statment in my c++ code.
Example:
#include<iostream>
std::cout << "hallo";
The output i'd like to see:
line 2: std::cout uses "iostream"
I found this question, the tools there do most of the part, they show dependency per file.
Does anyone know such a tool or how to acomplish this with the tools given in the answers in the question above?
Goal: I'm checking code for the conformity to a standard which i have a list of allowed headers for. With the desired output I can create a metric saying something like: 60% of the code is using allowed headers, 15% is using other headers or something like that.
This is not completely what you want but you can use Eclipse CDT to know where std::cout is declared.
If you press F3 when cout is selected in Eclipse, you will jump to this line of code inside iostream header file on the system with gcc 7:
extern ostream cout; /// Linked to standard output
You can try CppDepend to get all the methods called by a specific one with the location of each method called.

Regex library not working correctly in c++

I have been looking up places to work with regex in c++ , as I want to learn regular expressions in c++ (do give me a step by step link also if you guys have any). I am using g++ to compile my programs and working in Ubuntu.
earlier my program were not compiling but then I read this post where it said to compile the program by
"g++ -std=c++0x sample.cpp"
to use the regex header.
My first program works correctly, i tried implementing regex_match
#include<stdio.h>
#include<iostream>
#include<regex>
using namespace std;
int main()
{
string str = "Hello world";
regex rx ("ello");
if(regex_match(str.begin(), str.end(), rx))
{
cout<<"True"<<endl;
}
else
cout<<"False"<<endl;
return(0);
}
for which my program returned false ... ( as the expression is not matching completely)
I also rechecked it by making it match...it works.
Now I am writing another program to implement regex_replace and regex_search . Both of which doesnt work ( for regex_search just replace regex_match in the above program with regex_search. kindly help.I dont know where I am getting wrong.
The <regex> header is not fully supported by GCC.
You can see GCC support here.

C++: Unable to resolve identifier cout, Netbeans, Ubuntu

I am using C++ on Netbeans 7.1 on Ubuntu 11.04. For some reason, the following code results in the error message "Unable to resolve identifier cout".
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello,world!\n";
return 0;
}
Any help resolving this problem would be greatly appreciated.
The solution for your problem is at least strange ;)
Once iostream header is added, one has to reparse code. Click right on a project, go to code assistance and click to reparse project. Worked for me.
I was using netbeans for mac.
check whether iostream is really getting included;
i have tried your code on my machine using eclipse cdt it worked fine.so, please check the
includes.
What sort of file is this in? Is it a .h file, or .hpp file? I had this same issue. Netbeans can be ridiculous sometimes with C++. For me, I changed #include <iostream> to #include<iostream.h>
This may seem too simple, but...
In my NetBeans installation, when I go to create a new project, specify C/C++, it brings up a dialog box prompting for "Project Name:", location, folder, makefile name, and then...
a check box for "Create Main File", an edit box with "main" filled in, and to the right of that is a drop down list that reads "C". If you hit Finish, this will create "main.c" (C, but NOT a C++ file). Instead, in the drop down list, select "C++". Then the IDE creates main.cpp, which will be compiled with g++ and will find those includes and functions.
There is a difference between std::cout and cout. You don't currently have std::cout defined in your file. std::cout is a c standard out. In C++ we only need cout to work with iostream.
If you must use a standard c out then do the following:
Add this to the top under iostream
#include <iostream> //Input output stream in C++
#include <cstdlib> //Stands for c standard library
using namespace std;
Your code will now work because:
This change defines std::cout and std::cin among other things. (standard in, standard out respectively.)
However, I'd recommend this alternative if you don't need standard in outs:
Replace std::cout with cout, because cout is defined in iostream in C++. Your program would have worked without the std:: portion of your cin cout commands because you originally included iostream.
Try taking out the using namespace std; - it's generally considered bad form anyway :-)
I'm not sure that will fix the problem but most people either use the namespace or fully qualify things like std::cout. I've never seen code that does both.
The other thing to check is that the iostream header actually is being bought in. In other words, are there any errors on that line. A lot of problems (at least in the Windows world, so it may not necessarily apply to you) seem to be due to faulty path setup in NetBeans.
Hey look at your Output Debug. You may see "no permission". After I changed the file permission of "/YourProjekt/dist/Debug/GNU-Linux/file" to runable and everyone can read and write the error disappeared. (BTW: I had the bug because I was on a NTFS System with my Projekt, it have to be ext partition)
Hope I can help you with that.
Try taking out the std:: next to cout