Using The va_list to generate a string [duplicate] - c++

This question already has answers here:
Variable number of arguments in C++?
(17 answers)
Variable number of parameters in function in C++
(8 answers)
Closed 3 years ago.
Im using c++ to create a custom monitor using the lvgl library. So ive saw that the "..." can handle infinite variables. I wanted to create a function to convert the string and the following parameters into a pure string. I want the function to take in a character pointer, then the "...". I want it to take out the "%d" parts of the character pointer and replace it with the corresponding value in the va_list. If the va_list is empty, it can return the same character pointer. How can I achieve this? i have no knowledge about the "...", i only know they are called varidic functions.
Thanks all for your kind help!

Related

How to change the name of the file taken as input by a function [duplicate]

This question already has answers here:
How do I concatenate multiple C++ strings on one line?
(24 answers)
Creating file names automatically C++
(5 answers)
Closed last year.
I have this function in C++:
void file_to_vect(double * a, char * name_file);
that takes in input a vector and writes it on the file whose name is specified by name_file. The problem is that I want to use this function on a file whose name changes. For example
file_to_vect(a,"file_18.txt")
Where the number after "file_" is not fixed. I've tried to do that by:
int n=18;
file_to_vect(a,"file_"+char(n)+".txt");
But it doesn't work. Any suggestion on how to fix this?

How to use strings with switch [duplicate]

This question already has answers here:
Why can't the switch statement be applied to strings?
(22 answers)
Evaluate a string with a switch in C++ [duplicate]
(7 answers)
C/C++ switch case with string [duplicate]
(10 answers)
C/C++: switch for non-integers
(17 answers)
Closed 2 years ago.
I am brand new to programming. I have only learned up to functions in c++ so far, so please answer with in the scope of my knowledge.
I am working of a bank account program and I want to use a switch to get the user to input whether they want to deposit or withdraw.
I know I can use an int or even a char and do some like “D” for deposit but that’s not what I want to do.
I want to able to use the whole string “deposit” with the switch statement.
Thank you!
The switch statement in c++ only supports integer types or values that can be evaluated to an integer. At best you could write a function to evaluate your strings to an integer as mentioned here
ref: Evaluate a string with a switch in C++

C++ strings vs vector<char> [duplicate]

This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?
I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.
To put it briefly: if you're storing text, then string, otherwise vector<char>.

What is the size of argument list in C/C++ main function and indetermined behavior [duplicate]

This question already has answers here:
C argv what is the maximum size of data [duplicate]
(4 answers)
Closed 7 years ago.
I would like to know what is the maximum size of the list of arguments in C/C++ main function? Does it depend on the OS? And what happens if I pass the very large number of arguments to the main function, the main function behavior will be indetermined?
Thanks,
Yes, it depends on OS, but the limitation is about number of characters that can be passed to a process rather than the number of arguments. See this: Maximum Length of Command Line String

How to validate an integer properly in C++? [duplicate]

This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
How to convert a command-line argument to int?
(8 answers)
Closed 9 years ago.
I want to validate the input by user to make sure it's an integer (fully). I've tried a few methods, like !cin, but none of them work properly..
Most of methods fail to validate input like this:
32tgf
When there is a number first and then letters, it doesn't fail, but it takes it as valid entry..
Note: It's a project for college and it's specified that the variable should be of type int.
Read into a string, then use e.g. std::stoi or std::strtol to both convert to an integer and validate the input.
Or read into a string, put that string in a std::istringstream which you use to extract the integer. Then check if there's anything more in the istringstream.
I'd recommend the first method though.