What Are Some Better Ways to Avoid the Do-While(0); Hack in C++

What are some better ways to avoid the do-while(false); hack in Java?

What is this "while(false)" nonsence? Use a label!

myLabel: {
if(!check()) break myLabel;
...
...
if(!check()) break myLabel;
...
...
if(!check()) break myLabel;
...
}

It's core Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

do while conditions issue

You can use the code below to achieve what you are looking for. Note that the answer is very similar to that of this answer:

How to scanf only integer and repeat reading if the user enter non numeric characters?

#include<stdlib.h>
#include<stdio.h>

int clean_stdin()
{
while (getchar()!='\n');
return 1;
}

int main ()
{
float vkmh, vmax = 100.0;

setbuf(stdout, NULL);

vkmh = vmax + 1.0; /* value to start the loop */
while ( vkmh < 0 || vkmh > vmax) {
printf("Please enter value for the velocity in km/h: ");
if (scanf("%f",&vkmh) == 1) {
if (vkmh < 0) {
/* This was your 2nd if condition, but would never have executed assuming that vmax > 0. */
printf("Error, speed must be positive.\n");
exit(1);
} else if (vkmh <= vmax) {
/* Do something */
} else {
/* No need for the else if, this is the only other possibility */
printf("Error, the max speed of this vehicle is listed as %.fkm/h.\n"
"It cannot exceed that value. Please enter a value under %.f.\n", vmax, vmax);
}
} else {
printf("Error in input.\n");
clean_stdin();
}
}
printf("\nValue read: %f km/h\n",vkmh);
return 0;
}

do while loop exiting pre-maturely

One possible reason may be getkeyStatus() return "#" more than once when you press only one time. it is possible keypad device need some sleep time to clear its buffer memory i.e. as soon as you press a key its store into buffer which is getting multiple times.

Try

a. if possible printf("%c\n", key) and press only once and see how many times printed.

b. if any function to clear key functions ? OR sleep(x ms).

c. when you uncomment pulseIODevice () its working normal may be due to pulseIODevice () blocks few milli seconds by that time pressed key cleared from keypad buffer.

Alternatives to `while (1)` to simplify branching

The following is a method very similar to what you're doing with the loops, but without the need for a counter or a break statement at the end.

do
{
// process
if (!success) break;
// process
if (!success) break;
// process
if (!success) break;
...
// No need for a break statement here
}
while(0);

Do while loop skipping in c

It's because scanf() is a horrible function!

In this case, a '\n' was left in the input stream; when you execute scanf() again it consumes it and continues with the next one. You might be able to check that destination is indeed '\n' after the first iteration.

Note that scanf() is well designed; it's just not intuitive and that's why I say it's horrible, because it's incredibly difficult to use it correctly.

Most books teach taking input with scanf() and none of them explains how it works fully or how it should be used if you were to use it at all. Read this please scanf(3), but read it thoroughly. You will then notice that scanf("%d", &value) without checking what it returns, is simply wrong.

Weird issue when looping using the Do While in C

Please learn how to use indent (a C code beautifier).

Your indents and your logical flow are completely different.

Firstly, Why is the first if indented and the corresponding else should be at the same level of indentation.

The cause of your error is that the do loop is part of the "else" section of your if statement. But you indent it as if you want it to execute always.

Do-while endlessly looping cout, ignores cin

I prefer not to use istream::fail() for loop control. See Why is iostream::eof inside a loop condition considered wrong? for a similar issue.

Instead, I rely upon the return value of istream::operator >>.

I also use the following function to reset the flags and clear the input on input stream:

void flush_stream(std::istream& stream)
{
stream.clear();
stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

See How do I flush the cin buffer? for more on this.

So I'd code your input checks thus:

int get_valid_number(const std::string& prompt)
{
int number = 0;

bool valid = false;
while (!valid)
{
std::cout << prompt << std::endl;
if (std::cin >> number)
{
valid = true;
}
flush_stream(std::cin);
}

return number;
}

Hopefully the benefits of extracting this into a function are obvious. See it run.



Related Topics



Leave a reply



Submit