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++
Related
This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?
In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in 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!
This question already has answers here:
Handling large numbers in C++?
(10 answers)
Closed 8 years ago.
Is it possible to take an input of range greater than what C/C++ provides? Is it possible to accept an input range greater than that of unsigned long long and even larger up to the range of 10^1000?
If it is possible in C/C++, please answer how it can be done, thanks.
There's no bigint in C or C++, however library like this one can provide it: https://code.google.com/p/infint/
Input into a string. Then convert the string into the desired type.
If you use a library that provides types for large integers, such a library might also offer input functions.
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.
This question already has answers here:
Multicharacter literal in C and C++
(6 answers)
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 9 years ago.
C++ seems to allow up to four characters to be held in single quotes, such as:
char c = 'abcd';
but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.