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
recently I made a program that, given a string, the function returns its corresponding ascii code. For example, the string "h", the function returns an int 104. Now, I want to do the reverse process, ie, given an int, return its corresponding ascii character. For example, given the 104 int return the string "h". Please, help.
Looking at the string constructors, we can see one that takes a count and a character value. So we can use that:
return std::string(1, ascii_value);
You probably don't need a whole string for this. Given that you're looking for a single character, the correct type to use is a char:
int x = 65;
char xc = (char)x;
assert(xc == 'A');
char c;
...
std::string mystring(1, c);
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 1 year ago.
Improve this question
vector<string> dictionary = {"dog", "cat", "yard"}
string current_letter = "y"
// traverses dictionary and locates strings that start with corresponding letter
for (const string& s : dictionary)
{
if (s[0] == current_letter)
{
// prints out word that matches current_letter
}
}
Hello, I am trying to find string in a vector dictionary that start with the 'current' letter but I am running into C++ no operator matches these operands operand types are: const char == std::string" in the if condition.
How do I fix this?
You cannot compare a char (s[0]) with a string (current_letter) which is what the error is telling you. You can make current_letter a char or use std::string::starts_with (c++20)
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 8 years ago.
Improve this question
I have been looking and I have not found anything on scanf that really helps me. I am completly new to this and was hoping for help on reading a string with scanf. The first thre numbers can be any digits. I was attempting to read them into a variable int. the last one in the string is a char. this is my string
(1,2,123, 0)
(1,2,1,s)
This is my code:
int i,j,k;
char c, final;
scanf ("%c", c, "%d",&i, "%c", c, "%d", &j, "%d",&k, "%c", final);
I know this is not right but any help is appreciated
If the first 3 are digits and the last one is a character and are seperated by a space,which you want to assign to 3 integer variables and a character variable, use scanf like this:
int a,b,c;
char ch;
scanf ("%d%d%d %c",&a,&b,&c,&ch);
Or else if you want to extract 3 integers and a character from a string , use sscanf. It is not possible to do it with scanf.
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 8 years ago.
Improve this question
Trying to write the following function but confused, as get() only reads in the first character?
Write C-string's chars to the screen one char at a time.
void writeString(const char*)
Rule:
cannot use [].
Hints:use put();
make use of '\0' – but don't write it out.
It sounds like you just need a simple loop to output the string. Something like this perhaps.
void writeString(const char* str)
{
while(str++ != '\0') put(*str);
}
The while(str++ != '\0') will iterate over the string buffer pointed to by str and output each character. It also increments the str pointer to the next character and checks for null terminator ('\0').
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 8 years ago.
Improve this question
This is for some added functionality that I need to build into an existing library. I am trying to to check if a string contains a substring. I have pointers to the start and one-past-the-end of the main string,(which is a substring of a much larger string) and the word to search for is a String datatype.
char * start;
char * end;
String wordtosearchfor
I read about find in C++, but I am having trouble figuring out how to work with the pointer inputs. Is there an inbuilt function that works with inputs in this form? Or would it be efficient to read from start to end into a new String that can be used with find?
Thanks!
First of all you can indeed use member function find of class std::string. For example
char * start;
char * end;
std::string wordtosearch;
// setting values for variables
std::string::size_type n = wordtosearch.find( start, 0, end - start );
Also there is standard algorithm std::search declared in header <algorithm>
For example
char * start;
char * end;
std::string wordtosearch;
// setting values for variables
std::string::iterator it = std::search( wordtosearch.begin(), wordtosearch.end(),
start, end );
In these examples I suppose that end points after the last character of seached substring.
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 8 years ago.
Improve this question
void insert_into_stream(std::ostream& stream, int number)
{
int length = sizeof(int);
char insert_buffer[sizeof(int)];
memcpy(insert_buffer, &number, length);
stream.write(insert_buffer, length);
}
int int_from_string(std::string string)
{
int a;
std::istringstream(string)>>a;
return a;
}
This code used to work before, I don't remember what slight change I did and it isn't working anymore.
When a number (for example, 8001) comes in, and I'm debugging just before the last statement, insert_buffer contains 'A', so obviously something is going wrong and the 2nd function doesn't retrieve 8001.
Note - I do convert the stream to a string before sending it to the 2nd function.
Where is the first function wrong?
---edit----
Yes, I was wrong, the first function is actually doing exactly what it should, the second is wrong, can anyone please fix that?
These two functions are doing completely different things.
The first function is writing out the raw binary representation of an integer to a stream.
You have just copied the bits, this is the correct thing to do if you are serialising out to a binary file.
To convert it back, you would have to read in those 4 bytes, and cast that into an integer, just like you're doing the other way round.
Of course when you examine the characters they're going to be a one byte ascii representation of the integer bits. So 'A' is a perfectly reasonable thing to be there, as is anything else as it is entirely meaningless.
The second function however, is doing an ASCII Number to Integer number conversion. i.e. atoi. This is meaningless for what you're trying to do as the characters aren't ascii numbers, they're binary integer numbers.
Edit for edit: You want something like this, just the opposite of what you did above.
int int_from_string(const char* number)
{
int a;
memcpy(&a, number, sizeof(int));
return a;
}
Try to use
stream.write((const char*)&number, sizeof(number));
It's much shorter and you can also change the type of number and it will work (For simple types).