Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
consider:
const int CAP = 20;
struct bookType
{
string bookTitle = "EMPTY";
string ISBN = "EMPTY";
string author = "EMPTY";
string publisher = "EMPTY";
string dateAdded = "EMPTY";
int qty = 0;
double wholesale = 0.00;
double retail = 0.00;
};bookType book[CAP];
What I need to do here is hopefully simple, though I can't seem to get a straight answer on it. I want to search this array of structs (book[]) for a matching bookTitle. for instance, if I have a book named "Star Wars" I need to be able to search the array of structs by typing in "star" and finding a book, "Star Wars". I've been searching for hours, but all the solutions I've found don't seem to actually work.
I don't know the rest of you code so I'll try to give a generic answer.
It seems like you are looking for the find() function for string objects. The find function will return std::string::npos if it does not find anything.
So inside a loop, test:
Booktype[x].bookTitle.find("Star")!=std::string::npos
Change Star to the whatever you are searching for. If this condition is true then you haave a match.
Just a heads up, this is case sensitive so you might want create temporary variables and convert the titles and queries into lowercases and run the loop on them.
Hope this helps.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I want to make mysql query it look like this:
SELECT name FROM AUTHORS where name LIKE %[here argument]%
I search for solution and i find putting arg in "+[arg]+" like this;
const char * author = getString("Author: ", 100).c_str();
// res type MYSQL_RES* res
res = exec_query(conn, "select name from authors where name like '%"+author+"%'");
But i gives me an error:
expression must have integral or unscoped enum type
You have two problems with your code:
you are storing a dangling pointer in author
you are trying to concatenate multiple const char* pointers.
Change your code to treat author and your concatenated SQL as std::string instead. You can use std::string::c_str() when passing the final SQL string to mysql, eg:
std::string author = getString("Author: ", 100);
// res type MYSQL_RES* res
std::string sql = "select name from authors where name like '%"+author+"%'";
res = exec_query(conn, sql.c_str());
Do be aware that the code above is subject to SQL Injection attacks. You really should be using a parameterized query instead.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am getting values from an api call and it returns one json value/key pair as a string at a time. I need to count how many times items with a certain prefix (which encodes the type of the item) occur:
Lets say I am getting 'abc123' as the 1st value
def getType(nodeName):
nodeCount = 0
if "abc" in nodeName:
count = count + 1
return "ABC", count
How do I retain this nodeCount value so that next time an item with prefix 'abc' comes in from the api call, the count can be incremented to 2.
Also, I need to create other counters to keep track of the count of other node types, such as 'xyz777'.
I tried to declare nodeCount as global variable but if I add "global count", that will defeat the purpose of retaining the count value for the next api call/iteration.
I am very new to python, so please let me know if there is any easy way.
Many Thanks!
You may use a collections.Counter like this:
from collections import Counter
def getType(counter, nodeName):
nodetype= nodeName.rstrip('0123456789')
counter[nodetype] += 1
return nodetype.upper(), counter[nodetype]
c= Counter()
for n in ['abc123', 'def789', 'ghijk11', 'def99', 'abc444']:
nodetype, nodecount = getType(counter= c, nodeName= n)
print('type {} \t: {}'.format(nodetype, nodecount))
print('summary:')
print(c)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to identify a list of dates with a specific day of the week between 2 dates in Powershell
No problem, it's fairly straightforward:
$date = [datetime]::parseexact('07-Feb-20', 'dd-MMM-yy', $null)
$date2 = [datetime]::parseexact('28-Feb-20', 'dd-MMM-yy', $null)
$fridays = 1..($date2 - $date).Days | % {$date.AddDays($_)} | ? {$_.DayOfWeek -eq 'Friday'}
Just make sure you put your dates in correctly! There's likely a more succinct way to do it.
Every PowerShell [datetime] object contains a .DayOfWeek parameter which will tell you the day of the week. You can interate through the date items with another [datetime] feature, the .AddDays() method. So something like this:
$StartDate = [datetime]'datehere'
$EndDate = [datetime]'datehere'
$ThisDate = $StartDate
$AllFridays = #()
While ($ThisDate -le $EndDate)
{
If ($ThisDate.DayOfWeek -eq 'Friday') { $AllFridays += $ThisDate }
$ThisDate.AddDays(1)
}
This was just a first crack. Obviously, you could find the first friday, and then add days 7 at a time until you were past the End date, but I leave that optimization as an exercise for the reader. :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I'm trying to use list types in haskell.
I have the following types in my .hs file:
type Name = String
type PhoneNumber = Int
type Person = (Name, PhoneNumber)
type PhoneBook = [Person]
I'm looking to add the function
add::Person -> PhoneBook -> PhoneBook
add ........
that adds an entry to the phone book at the beginning of the list. Testing it would be done through the terminal
This is trivially the cons operator (:)
add :: Person -> PhoneBook -> PhoneBook
add = (:)
However I think you're abusing tuples here in Person. You should consider using a custom data type rather than a tuple in most cases. Using record syntax makes life easy for you:
data Person = Person { getName :: Name
, getPhoneNumber :: PhoneNumber }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr1 = 1,2,3,4,5;
arr2 = 1,2,3;
want to compare and output as arr3=4,5;
Please help
thanks in advance
arry::utils error out, looks like some problem with the package, so that option is ruled out.
sub diff_array {
my ($a1, $a2) = #_;
my %h;
#h{#$a2} = ();
return grep !exists $h{$_}, #$a1;
}
my #arr1 = (1,2,3,4,5);
my #arr2 = (1,2,3);
my #arr3 = diff_array(\#arr1, \#arr2);