Hold letters and numbers in std::string C++ [closed] - c++

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 8 years ago.
Improve this question
I'm trying to create a simple program to take serial numbers as an input and then searche them to find out what year the product was made in.
I am having some trouble getting a std::string to hold both numbers and letters.
std::string serial;
serial = b1234;
For example, if I run this through my compiler, I get the error message 'invalid digit in decimal constant'.
Is there a simple way to hold letters and numbers together in a string?

Basic C++ syntax says that string literals are delimited by " quotation marks.
const std::string serial = "b1234";

No. A a std::string is a string of characters, so you can't save integers in that string.
You're trying to write "b1234", I guess. Have you read any tutorial on C++ strings?

Related

regex: Check the first character AND match [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 3 years ago.
Improve this question
I'm working on a regular expression email validation assignment and it is working pretty well. I can match based on all the criteria I'm asked to. Here is my current pattern:
^([a-zA-Z0-9.\-\_\+]+)#([a-zA-Z0-9.\-\_\+]+)$
I also want to check that it begins with an alphabetic character. So I changed my pattern to this:
^[a-zA-Z]([a-zA-Z0-9.\-\_\+]+)#([a-zA-Z0-9.\-\_\+]+)$
And that works fine and only produces the match when the first character is alphabetic. BUT, the matches that are produced cut off the first character. So for example, if I try:
Billy#bobby.com
The match for the user-id before the # sign is coming back as "illy". I want it to come back as "Billy"
See the regex in action here: https://pythex.org/?regex=%5E%5Ba-zA-Z%5D(%5Ba-zA-Z0-9.%5C-%5C_%5C%2B%5D%2B)%40(%5Ba-zA-Z0-9.%5C-%5C_%5C%2B%5D%2B)%24&test_string=b2ill..%2B..DSD_y.23%40bobby.com&ignorecase=0&multiline=0&dotall=0&verbose=0
Nick gave me the help I needed. Here was my final pattern
^([a-zA-Z][a-zA-Z0-9.-_+]+)#([a-zA-Z0-9.-_+]+[a-zA-Z])$

Cpp file says [converted] and is a bunch of random characters [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 3 years ago.
Improve this question
My code was converted to these random characters at some point after I saved my program using Vi. I did this project for a grade in one of my college courses and didn’t get any credit, despite the fact that I spent hours working on my code for this to happen. If anyone knows how to convert it back to C++ I would be thankful.
Turns out I had saved my file under the wrong folder and I was able to recover my original file. Thanks to all for helping out with this! It seems like it always tends to be something so simple...

Perl matching string with mix of alphanumeric,_ and any number of square braces [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 6 years ago.
Improve this question
How do I match a string with " alphanumeric characters, underscore and any number of open and closed square braces ".
Example : " CDN_MBIT_hresp_s_reg[0]_MB_hresp_s_reg[1]bbjabs_chiansmokrs[6] "
I tried $line=~/[a-zA-Z0-9_/[/]]/;
This seems doesn't work.
P.S. This question is quite similar to Regex Matching Square Brackets
but not same
Thank you in advance.
Wrong slash used for escaping.
/[a-zA-Z0-9_\[\]]/
Alternatively, you could simply use
/[\w\[\]]/
Both of those match exactly one character. If you wanted to capture the string, you'd want
/([\w\[\]]+)/

C++ character check [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I have just started to learn C++ and i would like to get some help.
The user needs to type an ID number and the format has to be the following. The first character B and the other 4 any integer.
Im trying to check if the character format are right.
So far i have this:
if ((isalpha(id[0])=='B' ) && (isdigit(id.at(1))) && (isdigit(id.at(2))) ......
{
//do something
}
else
{
cout << "Wrong format" << endl;
}
but even if i type example B8745 it says wrong format.
You are comparing the result of isalpha, which is boolean, to character literal 'B'.

SIGINT when transforming a std::string to upper case? [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 8 years ago.
Improve this question
A program I'm debugging is receiving SIGINT at this point:
#0 0x00007ffff590784e in toupper () from /lib/x86_64-linux-gnu/libc.so.6
The code in question is this:
std::string search = name;
std::transform(search.begin(), search.end(), search.begin(), ::toupper);
Right now I don't know whats the content, but special symbols are allowed.
I already got that I'm doing something wrong here, sinse its actually UTF-8 and I should be converting it to std::wstring before performing case transformations. Howover, even though I was doing it the wrong way I don't understand why it should crash.
What are the reasons for toupper to crash for SIGINT? And how can I avoid that some user input can this to my server application?
UTF-8 uses numbers that will appear negative when viewed as a signed char (which is the usual). Giving those negative numbers to toupper gives undefined behavior.
Try:
std::transform(search.begin(), search.end(), search.begin(),
[](unsigned char ch) {return ::toupper(ch); });
...and see if things don't work a little better.