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.
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 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 want to build a program to save text to files , but I want my program to secure or encrypt the content of the text , for example , if the user input "salamence" to the program , the program would output (into a file) "hjkjupfqp" or something like that so people can't read it unless they have access to the program (I want the program to be able to decrypt the text file too) so it is possible in c++ to read strings input one by one character and modify them into another characters , and how to do that ?
A string is a sequence of chars put in a container that has other stuff in it. The chars themselves can be accessed through the [] operator. A char is basically an 8-bit integer that can be displayed. An integer can be manipulated arithmetically(+,-,*,...), bit-wise(&,^,|,<<,...), etc.
So you could do something like this:
#include <iostream>
#include <string>
using namespace std; //bad idea, but simplifies stuff
int main(){
string s;
cin>>s; //reads the string
for(int i=0;i < s.size;i++){ //loops through all characters of the string
s[i]++; //adds one to the string
}
cout<<s; //outputs the modified string
}
This will turn "abc" into "bcd", wich is a rather stupid form of encryption, but it proves the concept.
To decrypt you would need to copy the loop, but replace s[i]++ with s[i]--.
Since you seem to be a beginner, I would actually recommend using c-style strings, but that is outside the scope of this question.
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
getline(userfile, buffer);
new_user.birth_year = atoi(buffer.c_str());
for (stringstream s(buffer); s >> id;)
{
new_user.friends.push_back(atoi(id.c_str()));
getline(userfile, buffer);
Reads in a line of text into the variable buffer.
new_user.birth_year = atoi(buffer.c_str());
The atoi function requires a C-Style string. The method c_str() returns a C-Style string from the std::string.
The atoi function converts a C-Style string to an integer.
The integer result is then assigned to new_user.birth_year.
for (stringstream s(buffer); s >> id;)
{
The first part of the for loop, stringstream s(buffer), creates a string stream from the buffer string. The stringstream allows a string to be treated as a stream.
The s >> id reads a value from the string and places the result into id.
new_user.friends.push_back(atoi(id.c_str()));
This line converts the id string to an integer (see above about atoi) and appends the integer to the vector friends inside the new_user object.
This apparently expects to read some data in a format something like this:
1992 17 3 34 9
The first number is a user's year of birth. The numbers after that are ID numbers of that person's friends.
The first bit (new_user.birth_year = atoi(buffer.c_str());) reads the first number and assigns it to new_user.birth_year.
The next loop reads the rest of the numbers and adds them to a collection (probably std::vector) holding the IDs of that user's friends. Minor probable bug: it looks like it adds their birth year as an ID of a friend, along with the rest of the IDs numbers.
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.
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+='*';
}