Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am coding for an arduino project and i came across this problem, can anyone help me out!
if(condition1){
//my codes here
}
if(condition2){
//my codes here
}
if(condition3){
//my codes here
}
......
if(condition100){
//my codes here
}
else{
my codes here
}
I want to check all my if conditions, and execute the codes if the conditions are true, and run the else statement only if none of the if condition become true.
Note that i cant use else if because i want to check all the if conditions, and if none are true i want to run the else
If conditions are not dependent on each other
You can use a Boolean flag which is set in any of your ifs.
bool noPathTaken = true;
if ( condition1 ) {
noPathTaken = false;
// ...
}
if ( condition2 ) {
noPathTaken = false;
// ...
}
// ...
if ( noPathTaken ) { // this would be your "else"
// ...
}
must set off all else flag in each test
bool elseFlag = 1;
if(condition1){
elseFlag = 0;
my codes here
}
if(condition2){
elseFlag = 0;
my codes here
}
if(condition3){
elseFlag = 0;
my codes here
}
......
if(condition100){
elseFlag = 0;
my codes here
}
if (elseFlag) {
my codes here
}
because else gets binded only into preceding test operation, here if(condition100) {
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 4 days ago.
Improve this question
I've an issue using stringbuf in MVSC
stringbuf is a basic std type from iostream which is included above that block code.
for example a method using stringbuf reference :
`bool SJsonMon::Initialize(const stringbuf& DeviceP)
{
/*
******************************************************
*** Init lifetime monitor
******************************************************
*/
DeviceParameters = json::parse(DeviceP.str());
if (DeviceParameters["TypeElement"] == "LS14250")
{
mRL_MonitorLifetime_M.RL_MonitorLifetime_InstP_ref = &RL_LS14250_MonitorLifetime_P;
}
else if (DeviceParameters["TypeElement"] == "LS14500")
{
mRL_MonitorLifetime_M.RL_MonitorLifetime_InstP_ref = &RL_LS14500_MonitorLifetime_P;
}
else if (DeviceParameters["TypeElement"] == "LS17500")
{
mRL_MonitorLifetime_M.RL_MonitorLifetime_InstP_ref = &RL_LS17500_MonitorLifetime_P;
}
else if (DeviceParameters["TypeElement"] == "LS26500")
{
mRL_MonitorLifetime_M.RL_MonitorLifetime_InstP_ref = &RL_LS26500_MonitorLifetime_P;
}
else if (DeviceParameters["TypeElement"] == "LS33600")
{
mRL_MonitorLifetime_M.RL_MonitorLifetime_InstP_ref = &RL_LS33600_MonitorLifetime_P;
}
mRL_MonitorLifetime_M.blockIO = &mRL_MonitorLifetime_B;
mRL_MonitorLifetime_M.dwork = &mRL_MonitorLifetime_DW;
/* Pack model data into RTM */ // Context to back up
// Initialization input output for step method
RL_MonitorLifetime_initialize(
&mRL_MonitorLifetime_M,
&mRL_MonitorLifetime_U,
&mRL_MonitorLifetime_Y);
// Pack Device parameters in json object
DeviceParameters = json::parse(DeviceP.str());
return 1;
}`
the error :
Erreur C2027 utilisation du type non défini 'std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char>>'
Is someone can help about that issue?
Thanks a lot!
I used this code on stm32IDE and it worked and I decide to export the project on MVSC to simplify the DLL compilation.
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 1 year ago.
Improve this question
public async Task < IActionResult > Events(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest httpRequest,
ILogger log) {
ValidateResponse res = eventService.ValidateSchema();
if (res.Valid == false && res.Errors != null) {
string[] messages = new string[100];
int k = 0;
for (int i = 0; i < res.Errors.Count; i++) {
string value = res.Errors[i].ToString();
if (value.StartsWith("Invalid type")) {
messages[k++] = value.Substring(10);
continue;
}
}
return new OkObjectResult($ "No of events posted {cloudEvents.Count} and errors :{messages[k]}");
}
But the values in messages are not returned. I understand that its an array . But unable to figure out the ways to loop here in return all array values once.
Once option is to join the messages array to a string:
return new OkObjectResult($"No of events posted {cloudEvents.Count} and errors:{String.join(\",\", messages}");
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 4 years ago.
Improve this question
I want to generate and handle exceptions (SEH).
How can I write a code that will lead to an "illegal instruction"-exception?
You can do something like this:
#include <Windows.h>
#include <cstdio>
::DWORD FilterFunction(::DWORD const _exception_code)
{
if(EXCEPTION_ILLEGAL_INSTRUCTION == _exception_code)
{
::std::printf("filtered\n");
return EXCEPTION_EXECUTE_HANDLER;
}
else
{
::std::printf("not filtered\n");
return EXCEPTION_CONTINUE_SEARCH;
}
}
int main()
{
__try
{
::std::printf("trying...\n");
::RaiseException
(
EXCEPTION_ILLEGAL_INSTRUCTION // exception id
, 0 // continuable exception
, 0 // args count
, nullptr // args
); // no arguments
::std::printf("finished trying\n");
}
__except(FilterFunction(GetExceptionCode()))
{
::std::printf("exception handled\n");
}
return 0;
}
trying...
filtered
exception handled
You can raise a structured exception in Windows with:
RaiseException(EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_NONCONTINUABLE, 0, nullptr);
Reference:
https://msdn.microsoft.com/en-us/library/ms680552%28VS.85%29.aspx
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Simple question.
I have this code in front of me:
if ( somethings() == false ) {
log("error when ...somethings()");
return false;
}
// do somethings
if ( somethings2() == false ) {
log("error when ...somethings2()");
return false;
}
// ...
// eventually
if ( somethingsN() == false ) {
log("error when ...somethingsN()");
delete myData;
return false;
}
Not sexy, right ? What would be the best method(/pattern ?) for improving this code ?
Solution 1:
I was wondering about using exceptions
try {
if ( somethings() == false )
throw std::runtime_error("error when ...somethings()");
// do somethings
if ( somethings2() == false )
throw std::runtime_error("error when ...somethings2()");
// ...
if ( somethingsN() == false )
throw std::runtime_error("error when ...somethingsN()");
}
catch ( std::exception& e ) {
log(e.what());
delete myData;
return false;
}
Solution 2:
Using nested if statements
if ( somethings() ) {
// do somethings...
if ( somethings1() ) {
// ... (eventually too many indent)
if ( somethingsN() ) {
return true;
}
}
}
delete myData;
return false;
What are your methods ?
I just want to improve my coding style here.
Note (edit)
"Do not use 'delete', use smart pointers instead such as unique_ptr" (thanks all)
Throw an exception on error rather than returning a magic value for the caller to check; avoid dynamic allocation except when you really need it; use smart pointers or other RAII types to manage it when you really do. Then the code becomes rather more straightforward:
somethings();
somethings2();
// ...
somethingsN();
#define CHECK(x) \
do { if (!(x)) throw MyException(#x, __FILE__, __LINE__); } while(0)
try {
// call third-party or
// C functions that don't throw
CHECK(thisone());
CHECK(thatone());
CHECK(theotherone());
// call my own functions
anotherone(); // throws
yetanotherone(); // throws
} catch (std::exception& e) {
log(e.what());
}
And of course no delete. unique_ptr is your friend.
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 9 years ago.
Improve this question
can someone explain me on what does these codes did? this is only part of the code...
// Create the window of the application
sf::RenderWindow myWindow(sf::VideoMode(myWorldWidth, myWorldHeight, 32), "Battleship!");
myWindow.setVerticalSyncEnabled(true);
bool showHardwareMouse;
bool started;
bool drag;
float dragOffsetX, dragOffsetY;
bool LeftMouseButtonDown = false;
bool reset = true;
//----- Main Loop Start here -----
while (myWindow.isOpen())
{
if (reset)
{
// Reset
showHardwareMouse = true;
drag = false;
dragOffsetX = dragOffsetY = 0.0f;
started = true;
reset = false;
}
the program is actually based on SFML library, what does dragOffsetX = dragOffsetY = 0.0f; means?
and this is how actually the program work like...
http://i1146.photobucket.com/albums/o530/HTHVampire/C%20plus%20plus/Capture2_zps1fe188cd.jpg
I will post the full codes of it if you guys can't get it. Thanks!
dragOffsetX = dragOffsetY = 0.0f;
is the same as
dragOffsetX = (dragOffsetY = 0.0f);
where an assignment a = b has the "value" b. So the above line is the same as:
dragOffsetX = 0.0f;
dragOffsetY = 0.0f;
The rest of the code consists mainly of declarations and initializations and should be obvious.