while-loop

SQL Server How to output one table result from multiple results with a WHILE query

元气小坏坏 提交于 2019-11-30 23:19:02
From this answer: Is there a way to loop through a table variable in TSQL without using a cursor? I'm using the method WHILE EXISTS(SELECT * FROM #Temp) The problem is that it's outputting multiple tables, if possible I'd like to output as a single table. Declare @Id int WHILE EXISTS(SELECT * FROM #Temp) Begin Select Top 1 @Id = Id From #Temp --Do some processing here Delete #Temp Where Id = @Id End So right now it outputs this: x y -- -- 1 a x y -- -- 1 b But I'd like it to output this: x y -- -- 1 a 2 b What I'm trying to achieve , I have this in a field: 1234,1432,1235 I have a process that

How does reading file with while loops work in C++?

痴心易碎 提交于 2019-11-30 22:47:23
Why is it possible to read a file using a while loop such as while (file >> variable) Or while (getline(xx, yy)) Do the >> and getline functions return boolean values ? The stream operators evaluate to a reference to the stream itself. This allows chaining e.g. file >> variable >> variable >> variable . When you combine it with the fact that the stream object is convertible to boolean (whose value is true iff no error flags are set) and, yes, you get that effect. Mats Petersson If we have something like: a file with data: 11 22 13 45 19 and then a C++ file: int variable; int count = 0; while(

Accepting any number of inputs from scanf function

妖精的绣舞 提交于 2019-11-30 21:55:22
I am trying to read an unknown number of inputs using scanf function. int a[100]; int i = 0; while((scanf("%d", &a[i])) != '\n') i++; // Next part of the code But this function is not going to next part of the code, seems like there is an infinite while loop. How Do I solve this logical error? Is there any other alternatives to scanf like sscanf to read integers into an array? scanf returns the number of input items that have been successfully matched and assigned, thus it is reasonable to do: while(scanf(...) == 1) Now you want to be able to read multiple numbers, each defined on the new line

When would a do-while loop be the better than a while-loop?

老子叫甜甜 提交于 2019-11-30 21:47:03
This is a highly subjective question, so I'll be more specific. Is there any time that a do-while loop would be a better style of coding than a normal while-loop? e.g. int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);` It doesn't seem to make sense to me to check the while condition after evaluating the do-statement (aka forcing the do statement to run at least once). For something simple like my above example, I would imagine that: int count = 0; while(count < 10) { System.out.println("Welcome to Java"); count++; } would be generally considered to have

do-while and while comparison

懵懂的女人 提交于 2019-11-30 21:07:26
do-while: do { i++; ++j; System.out.println( i * j ); } while ((i < 10) && (j*j != 25)); I am learning about do-while vs while at the moment and would like to rewrite the above java fragment (already declared and initialized) using a while instead. Are the below rewritten codes correct way to do so: while: while ((i < 10) && (j*j != 25)) { i++; ++j; System.out.println( i * j ); } Cheers The difference between a do-while and a while is when the comparison is done. With a do-while , you'll compare at the end and hence do at least one iteration. Equivalent code for your example do { i++; ++j;

Validation check with scanf does not work inside a loop

懵懂的女人 提交于 2019-11-30 21:04:27
问题 I am trying to make a function to check the input of the user and let them try again in case they enter a wrong kind of input. So when I enter a wrong input into the function it throws me inside an infinite loop. What can I do to fix it? I am allowed to use only getchar and scanf for user input. int sizeOfField() { int size,inputCheck,flag=0; while (!flag) { inputCheck= scanf(" %d ", &size ); if ( inputCheck < 1 ) { printf( "Invalid Input!\n" ); printf( "Try agian"); } else if (inputCheck ==

Counting digits using while loop

血红的双手。 提交于 2019-11-30 20:20:09
I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code: int x; cout << "Enter a number: "; cin >> x; x /= 10; while(x > 0) { count++; x = x/10; } From what I can tell (even with my limited experience) is that it seems crude and rather unelegant. Does anyone have an idea on how to improve this code (while not using an inbuilt c++ function)? In your particular example you could read the number as a string and count the number of characters. But for the general case, you can do it your way or you can use a

How to close a stage after a certain amount of time JavaFX

喜欢而已 提交于 2019-11-30 19:57:23
I'm currently working with two controller classes. In Controller1 it creates a new stage that opens on top of the main one. Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("Controller2.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); Now once that stage is open, I want it to stay open for about 5 seconds before closing itself. Within Controller2, I've tried implementing something like long mTime = System.currentTimeMillis(); long end = mTime + 5000; // 5 seconds while (System.currentTimeMillis() > end) { //close this stage } but I

How come if I enter a letter input my program gets stuck in its while loop?

女生的网名这么多〃 提交于 2019-11-30 19:36:09
问题 Sorry if I fail to be clear enough or make any mistakes, this is my first time posting. My code runs without errors when complied but the first while loop (in int main ) gets stuck looping whenever a user types a letter (like "a" ) for cin >> select; instead of the required 1 , 2 , or 3 . However, when I input "4" or any other random string of numbers, it runs fine and goes to my error message like it should. Why is this and what can I do to make it run normally? (run the error message in

How does reading file with while loops work in C++?

只谈情不闲聊 提交于 2019-11-30 17:48:40
问题 Why is it possible to read a file using a while loop such as while (file >> variable) Or while (getline(xx, yy)) Do the >> and getline functions return boolean values ? 回答1: The stream operators evaluate to a reference to the stream itself. This allows chaining e.g. file >> variable >> variable >> variable . When you combine it with the fact that the stream object is convertible to boolean (whose value is true iff no error flags are set) and, yes, you get that effect. 回答2: If we have