How to use multi words un 1 variable? [duplicate] - c++

This question already has answers here:
std::cin input with spaces?
(8 answers)
Reading string with spaces in c++
(3 answers)
Closed 6 years ago.
I am trying to asign more than one words to a variable, at once, but after inserting space, it looks for another variable. Lets say I need to write Hello c++ world in a variable. The onliest solution I've found, is the following C++ code:
#include <iostream>
using namespace std;
int main()
{
string word1,word2,word3;
cin>>word1>>word2>>word3;
return 0;
}
Ok, but what if you dont know how much words is the sentance going to be?
Example entry:
say Hello there!
Example print: Hello there
Example entry: say This is much longer, we can't create milions of variables!
Example print: This is much longer, we can't create milions of variables!
Please help! What I need here is not only use spaces in a variable, I want before this to have another "command" like in the case above: the command say.

Related

How do i "subtract" a character from a string in c++ [duplicate]

This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();

RegEx for ${any_text} [duplicate]

This question already has answers here:
Replace all text between braces { } in Java with regex
(5 answers)
Closed 4 years ago.
I'm trying to find any solution to extract all variables definition from freemaker template. Lets assume that template looks like this:
Hello ${test_1} Hello ${test_2} Hello ${test_3} Hello ${test_4}
At the output I want to have list of test_1, test_2, test_3, test_4. I tried almost every approach but this $ (special character sign) is causing me a lot of problems also important thing (that also cause me problem) are variables that relate for objects for example ${test_1.user.name}
Thanks for dawg user the solution for this looks like:
\$\{([\w.]+)\}
One more time than you very much.
Demo

Is it possible to modify string input? [closed]

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.

Using value of variable as a name next variable in C++ [duplicate]

This question already has answers here:
How can I make a integer for every name in a txt file?
(4 answers)
Closed 7 years ago.
I have one question about C++.
I would like to use value of variable as a name of next variable.
Example:
User write value
cin>>PlayerName;
//PlayerName = 'John';
Now app should add +1 to variable "John"
John=John+1;
How to do that?
Regards
You cannot do this in C++ (At least not without crazy hackery). What you are trying to do is "reflection" - to edit your program during runtime. This is very easy in Python, but requires shenanigans in C++.
To answer the spirit of your question, which is "How can I programmatically edit things based on user input" is to use a map, as Neil Kirk suggested, where they key is a string.
Then you'd do something like
std::map<std::string, int> playerScores;
playerScores["john"] = 0;
cin >> playerName;
playerScores[playerName] += 1;

How can I concatenate an int to a string? [duplicate]

This question already has answers here:
How to concatenate a std::string and an int
(25 answers)
C++ int to string, concatenate strings [duplicate]
(6 answers)
Closed 8 years ago.
I have done a good hour or so of research, but I can't find a method that works for me that I actually understand, as I don't understand how buffers work. I've tried the to_string method, I've tried the .append() method, and obviously, a simple concatenation method.
My code:
fileLog[fileLogIndex].append(playerTurn+":"+moveColumn);
//fileLog[fileLogIndex] += playerTurn && ":" && moveColumn && ", ";
So. The purpose of this, or my idea, was to keep track of each move in a connect 4 game I wrote today. This was supposed to append each move, in the format aforementioned. PlayerTurn is stored as an int, 1 being player 1, 2 being player 2, and the moveColumn variable is the column that the user selected to drop their piece. If player 1 dropped a piece into column 4, the idea was to append "1:4, " to the variable and then write that line to a file.
As you can see, I have tried a few methods, like append, and even though you can't see, I have tried to to_string () method, but I either get an error, or "1:1" gives me a smiley face in the dos window, or a null in notepad++ when opening the file.
I don't want to use a method I don't understand, so I apologize if this is a repeat of another thread, I'm just tired of staring at this 1 line of code and getting nowhere with the methods I am trying. If I have to use a buffer to do this, fine, but can someone explain to me, in somewhat newbie terms, what each line is doing?
For the record, I am using Visual Studio 2010, and I'm 90% sure I don't have C++11, which I read somewhere is the reason to_string isn't working as expected for me..
The C++ way to do this (i.e., without using C's buffers and sprintf) that doesn't use C++11's to_string is by constructing the string using an ostringstream:
#include <sstream>
ostringstream out;
out << playerTurn << ":" << moveColumn;
fileLog[fileLogIndex] += out.str();