Passing argument into const char * [closed] - c++

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.

Related

How to Create a List that holds 2 types of variable types in Scala [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im new to scala so please go easy on me lol.
I need to create a list of where each spot holds an Int,String. So like [(1,"string1"),(2,"String2")...]
for example, Ive tried
val string1 = "something"
val string2 = "something"
List[Int,String] = List[(1,string1), (2,string), (3,string3),(4,string4),(5,string5)]
and I get the error - identifier expected but integer literal found.
How exactly would I get something like this to work?
(1,"string1") is a tuple containing an Int and a String, so type of list should also be a tuple - (Int, String):
val string1 = "something"
val string2 = "something"
// ... rest of string values
val list: List[(Int,String)] = List((1,string1), (2,string2), (3,string3),(4,string4),(5,string5))

Counting number of item types in a series in Python [closed]

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)

Search an array of structs for a string variable [closed]

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.

process an input file with "name = value " pairs using perl [closed]

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 8 years ago.
Improve this question
The simulation code I use needs to read parameters from an input.txt file which looks like
paramA = 1,paramB = 2,
paramC = 3,paramD = 4,
When scanning a parameter (like paramC) in my simulation, I need to change the value of paramC every time manually. How can I do this with a perl script so that when I type
perl scriptname input.txt paramC 100
in the command line I can get a modified input file with paramC changed to 100
paramA = 1,paramB = 2,
paramC = 100,paramD = 4,
I can do this by creating a template file like
paramA = 1,paramB = 2,
paramC = <>,paramD = 4,
and then use perl to match the mark <> and replace it with the value I want. However is there a more direct way to match the parameter name and change its value ?
thanks.
The obvious answer is to use a regular expression. Perl is quite good at those.
So you could - for example - do:
s/paramC = \d+/paramC = $value/g;
Which'll do the trick I'd have thought?
Edit: Or use TLP's pattern in the comments:
s/^paramC = \K[^ ,]+/$value/g;
or perhaps:
s/^paramC = \K\d+/$value/g;

Outputting a string result from a list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a list of items of a custom data type Film = String String Int, where the strings are the name and director and Int is the year of release.
What's the best way I would go about making a function that outputs a String or set of strings (doesn't matter how long) which show the information like:
Title: (film title) Director: (director) Released: (released)
You need to define a function that creates a String if given a Film as
an input, e.g.:
data Film = Film String String Int
instance Show Film where
show (Film t d r) =
"Title: (" ++ t ++ ") Director: (" ++ d ++ ") Released: (" ++ show(r) ++ ")"
You can read up on type classes and user-defined show here and here.