Taking input from user within a printed text - c++

I want to take input from the user in between the () in the given "cout" statement, in place of the '_'
cout<<"Warning! you are going to resize, which may result in loss of data are you sure you want to continue: Y/N(_)";
How may I be able to do it? Are there any escape sequence for doing it? Or any other way?

Use this code fragment:
scanf("(%s)",&str)

Related

perform mathematical operations on a number without changing the attached text

I need a formula that can multiply or divide all the numbers in a string without changing the text attached to the numbers.
I need the numbers in the next column to automatically change according to the given mathematical operation, but the text from the original line must remain unchanged.
I've tried using a combination of REGEXMATCH and REGEXEXTRACT and by doing this I just get the result of multiplying/dividing all the numbers in the string (no text whatsoever).
I also had no success using REGEXREPLACE. I'm not even sure we can actually use it in this case, and maybe I need a different formula instead. Maybe you first need to extract the numbers, multiply them and use something like TEXTJOIN or CONCATENATE to put them together in a string with the values already changed, and is this even possible in this specific example? It's totally fine to perform the operation in several steps if needed (for example, adding SPLIT function or something like that), but the format of the raw data we need to enter and recalculate, unfortunately, cannot be modified.
A sample table for better visualisation can be seen below. Any help would be greatly appreciated!
Raw data
Operation
Desired outcome
25STR/40DEX/70FRES
*0.25
6.25STR/10DEX/17.5FRES
80VIT/30INT/50CRES
*0.75
60STR/22.5INT/37.5CRES
60VIT/20STR/45LRES
*1.25
75VIT/25STR/56.25LRES
You may try:
=byrow(index(bycol(split(A2:A,"/"),lambda(z,ifna(ifs(left(B2:B,1)="*",regexextract(z,"\d+")*mid(B2:B,2,99),left(B2:B,1)="/",round(regexextract(z,"\d+")/mid(B2:B,2,99),2))&regexextract(z,"\d+(.*)"))))),lambda(y,if(y="",,join("/",y))))

Visual Studio C++ removing last character from string

I need a little help with my calculator program. I have created the code for the main buttons like the numbers 0-9 and the arithmetic operators to make it perform simple calculations.
What I'm having problems with right now is making the CE button work, after clicking the CE button I need the last entered character to be removed from the display label.
I have tried to adapt this code somehow, but it doesn't work:
lblResult->substr(0, lblResult->size()-1);
I know I'm doing somehting wrong here, can you please help me?
Thanks in advance
...Now that we know that lblResult is a System.Windows.Forms.Label, we can look at the documentation.
A Label has a Text Property, which is a String^ (i.e. a string reference).
For what you want to do, the Remove Method of String is appropriate. But note in the documentation it says that it "Returns a new string in which a specified number of characters from the current string are deleted." This means that it does not modify the string, but returns a modified copy.
So to change the label's text, we need to assign to its Text property what we want: the current string with all of the characters except the last:
lblResult->Text = lblResult->Text->Remove(lblResult->Text->Length - 1);
lblResult->resize(lblResult->size() - 1);
In this case you can use components Remove and Length methods.
Use the following code to access the components text:
component->Text
Than remove the last character of string by accessing Remove and component Length method
= component->Text->Remove(component->Text->Length - 1)
I hope you find this useful.
Just asking the obvious -- the whole statement is
*lblResult = lblResult->substr(0, lblResult->size()-1);
right?

Data validation from getline in C++

I want to ask the user if they want to continue playing another round or quit. If they want to continue I expect them to enter two words in getline, but if they enter quit, I want to break;. Is this possible?
Can I check to see if the first letter is a "q" and then use putback if it is not? Or can I check the whole word and putback if it is not quit (I would not know the length of the word). Id there some other way I can approach this?
Thank you!
Yes.
I recommend using getline to read each line of user input. Then parse the string in order to determine what action to take. I don't see how a putback would be useful if it were possible. If necessary you can use a stringstream to get numeric info out of the string, or you can simply compare characters. It is up to you to determine the approach but there are a variety of ways in which you could design the program.
Take a look at this. Try to avoid the common gotchas associated with standard streams otherwise you will soon be googling for answers to other questions.
http://www.parashift.com/c++-faq/input-output.html

Good way to tokenize a string to store values? Or alternative for user input

Hello again Stackoverflow, I'm here again asking a question for my C++ programming class. The problem I am facing is mostly to due with user input from the keyboard. I need to be able to take the user input to decide what function to call and what arguments to give the function. For example something like add 5 would call the add function with the argument 5. At first I tried overloading the >> operator to take both a string and an int but the problem I ran into was the program was unable to take input without the int such as deletemax so I had to throw that idea out. So now I am back to tokenizing the input but we are not allowed to use Boost for this program so I came up with something like this using sstream
bool out = false;
string token;
string In;
int num;
do
{
cout << "heap> ";
cin >> In;
istringstream iss(In);
while(getline(iss, token, ' '))
{
cout << token << endl; //I know this is incorrect but just not what to replace it with
}
out = ProcessCommand (token, num); //Takes string and int to call correct functions
} while (out != true);
The problem lies in that I'm not quite sure how to correctly tokenize the string so I can get 2 string and convert the second string to an int. Can anyone offer me some assistance? I would greatly appreciate it. Also if there is a better way to go about this than I am trying I would also like to hear it.
Thanks for any help you can give me.
Googling "C++ string tokenize" will get you plenty of hits, with the first hit being on Stackoverflow. But you should take a stab at it. I'm guessing it's the point of the exercise.
You said "argumentS", which suggests that commands you support take varying numbers of arguments. I'd break it down like this:
read a line from the user
split line into 'tokens' on space boundaries, store tokens in a list
based on the first token in the list, choose a command to execute
pass the list of tokens to the command, so it can validate/interpret them as arguments
The tricky part is #2. Do you know about container classes yet? You can use vector<string> to store the chunks you parse. To do the actual parsing, you iterate through the characters of the string. Skip whitespace until you find a non-whitespace character (or run out of characters). Save this position: start. Then skip non-whitespace until you find whitespace (or run out of characters). Save this position: end. Copy the substring represented between from start to end and copy that to your token list.
Working out the actual details of this, making sure you don't have off-by-on-errors, etc. is going to be challenging if you've never done it before, which I'm guessing is the point.
You don't need to read in the whole of user input all at once.
For example you could read in the first bit of user input (the operation, add or deletemax, etc). From there depending on the operation you could continue to read arguments from input (in the case of add) or begin performing the operation immediately (in the case of deletemax).
One way would be to have a std::map of function names as keys and required number of arguments as values. You'd read a line of input, get the function name and then decide whether you need aditional arguments. I'd write a function that'd return a vector of arguments extracted from a string stream or an empty vector in case the input was invalid.

Restrict users to enter numbers valid only till 2 decimal places C/C++

I am making an currency change program where I would be providing exact change to the input amount, for example a value of 23 would be one 20 dollars and 3 one dollar bills
I want to restrict the user to input the value only till 2 decimal places. For example: the valid inputs are
20, 20.4, 23.44 but an invalid input would be 20.523 or 20.000.
How can I do this is C/C++.
I read about one function that is setprecision but that is not what I want, setprecision allows to display the value till that decimal point, it still doesn't stop the user from entering any value.
Is there any way to do this?
Read the amount from the user as a string, either character by character or the entire line, and then check its format, and then convert it.
It's generally easier to let the user type whatever they want followed by the program rejecting the input if it isn't valid rather than restricting what they can type on a keystroke basis.
For keystroke analysis you would need a state machine with 4 states, which we can call Number, Numberdot, Numberdotone, and Numberdottwo. Your code would have to make the proper transitions for all keystrokes, including the arrow keys to move the cursor to some arbitrary place and the Backspace key. That's a lot of work.
With input validation, all you have to do is check the input using a regular expression, e.g. ^(([0-9]+) | ([0-9]+.[0-9]) | ([0-9]+.[0-9][0-9])$. This assumes that "20." is not valid. Then if it's invalid you tell the user and make them do it again.
I do not believe that there is any way to set the library to do this for you. Because of that you're going to have to do the work yourself.
There are may ways you can do this, but the only true way to handle restricting the input is to control reading it in yourself.
In this case you would loop on keyboard input, for ever keystroke you would have to decided if it can be accepted in the context of the past input, then display it. That is, if there is a decimal point you would only accept to more numbers. This also allows you to limit input to numbers and decimal places as well, not to mention input length.
The down side is you will have to handle all the editing commands. Even bare bones you would need to support delete and enter.
This is rather a task for the GUI you are using, than for core C/C++. Depending on your GUI/Web Toolkit you can give more or less detailed rules how data can or can not be entered.
If you are writing a normal GUI application you can control and modify the entered keys (in C or C++).
In a WEB application you can do similar things using javascript.
The best solution would be when all illegal input is impossible.