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 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I want to let the users input strings with more than 4,095 characters, but std::cin/std::istream seems to have some sort of undocumented limit and keeps cutting off the string to only allow 4,095 characters.
Is there some way to get past this "limit" using one of the many input functions, preferably allowing any length? There might not be a limit in the cpp standard, but the operating system I am currently using may have an input buffer limit - Arch Linux.
The strings may reach lengths of 1 ≤ L ≤ 10e6 where L is the length of the string.
int main() {
/* The user inputs a string with more than
4095 characters.
Let's say that every character in the string
is 'a' except for the 4095th character in the
string, which is 'B', and the end of the string
looks like "...aaBaaaaaaaaaaaaa"
The string will get cut off to "...aaB" (4095
characters) meaning that there seems to be some
sort of limit. */
std::string str;
/* Manually paste a string that has 10,000
characters into a terminal emulator (Alacritty)
with CTRL+C → SHIFT+INSERT */
std::cin >> str;
/* This next line is supposed to output 10,000,
but instead, it outputs 4,095. */
std::cout << str.length() << std::endl;
}
The provided example should be simple enough to understand without needing a better reproducible example. Providing a better example would only clutter the post with a lot of unneccessary information since it should be simple enough as it is.
The input data I give it looks like this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa but imagine 10,000 a's and it does not have any whitespaces.
Note: This program is compiled with this command:
g++ -g -O2 -std=gnu++17 -static light.cpp && ./a.out
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 have declared this vector
vector<char> germat{ SekuencaEfjaleve.begin(), SekuencaEfjaleve.end() };
where SekuencaEfjaleve is a string that i get as an input from the user. The input always contains a space in the middle, so the user inputs something like this 423 fgfh=, and when I print out the list it stops at 3 it has only 3 elements.
I read it as cin >> SekuencaEfjaleve; and i print it as
cout << germat[i];
}```
Why not you use string rather then using character type vector, like this way, it is lot easier to use.
string germat;
getline(cin, germat); // used C++ builtin function `getline()` for taking string input with spaces
// now you can access by `germat[index]`
or you can take your SekuencaEfjaleve string with getline(), like:
getline(cin, SekuencaEfjaleve) and now you can access with SekuencaEfjaleve[index].
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
I need to store characters from user's input in array, BUT not one by one. User will input them as one line like this;
....
I need to save each dot in array, but I can't do this:
scanf("%s%s%s%s", &s[0], &s[1], &s[2], &s[3])
because user can enter N number of dots. So it must be dynamic, I guess.
scanf() is a C runtime function. In C++, you should be using std::cin instead. For instance, with std::getline(). You can treat the returned std::string like an array of characters.
User will input them as one line like this; .... I need to save each dot in array,
C solution:
Define upper sane bound like 1000 and use a scanset "%[]".
// Read up to 1000 `.`
char dot[1000 + 1];
if (scanf(" %1000[.]") == 1) {
// Success
puts(dot);
}
Additional code needed if other non-., non -white-space characters need to be handled.
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 6 years ago.
Improve this question
How can we test the end of an array of unsigned char ? With a string char it's easy, I can just make a test like
while (str[i] != '\0')
or just
while (str[i])
But with an array of unsigned char that doesn't work and I don't really understand why. That's an example where I need help
BYTE* a = getB();
int i=0;
while(a[i]!=0)
{
printf("%C", a[i]);
i++;
}
Thanks
It is completely up to you.
That is, you define what the end of an array means.
Conventionally, sure, the end of a character array is a "null byte", signifiying the terminating position. There's no reason you can't do that with an array of unsigned char.
But, just like with char, this need not necessarily be the case. Maybe you signify the end of the array by counting its elements (specifying a size), or by declaring that some other character is the final one.
We cannot tell you what that is. Only the person who creates the data (as far as we're concerned here, that's you) can do so.
So, either consult the documentation for the library that gave you the array, or reach into your memory. :)
#eugenesh is right. You can't do do it. Either you need to have extra space at the end and add some kind of delimiter or keep a count of charactera
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to be able to iterate through all of the utf-8 characters in a string.
Imagine that all code points had a index, the first code point had index 0 and the last code point had index N. I want the iterator to be able to jump X indexes forward and also tell me the index of a code point.
I want to do something similar to http://www.nubaria.com/en/blog/?p=371, but I am not sure how to iterate over the bytes so that the iterators always refer to the start of a legal utf-8 code points.
Warning: this method only works if you already know that your bytes contain clean UTF-8. It will not work properly in the presence of malformed or invalid characters.
The second through last bytes of a UTF-8 encoded codepoint will always have a bit sequence of 10xxxxxx. Skip over those and you'll be at the start of the next codepoint.
for (int i=0; i<X && *p!=0; ++i)
{
++p;
while ((*p & 0xc0) == 0x80) ++p;
}
The *p!=0 is there to make sure you don't run past the end of the string.