do-while

C: Do-While Loop Repeating Too Much!

大憨熊 提交于 2019-12-02 08:44:01
I have a small program that which is confusing me. I am trying using a loop to take input from user. In case input is wrong, it is repeated again but if it is right, it exits. The code snippet is: void main() { char user_status; // Checks User Status q = Quiz Master and p = Participant int valid_status = '0'; // Checks If User Status Is Valid Or Not. Used In Some Loops. 0 = Invalid, 1 = Invalid. printf("Welcome to General Knowledge Quiz Management System.\nThis application has been designed to help you conduct a quiz or test your GK."); do { user_status = '0'; printf("\n\nPlease enter your

Java do-while loop isn't working

泪湿孤枕 提交于 2019-12-02 08:25:38
I want my program to keep asking the question until it gets a response it can use, specifically a number from 0 to 20. I have a lot of other stuff on this class, so here is a small excerpt where the do-while is (I have named the variables and all that for everything). public static void main(String[] args) { do { halp = 1; System.out.println("What level is your fort?"); Scanner sc = new Scanner(System.in); try { fortLevel = Integer.parseInt(sc.nextLine()); } catch(NumberFormatException e){System.out.println("Numbers only, 0-20"); halp = 0; } if(halp < 1) { work = false; } if(halp > 1) { work =

While “!b.equals(x) || !b.equals(y)” is an infinite loop?

不问归期 提交于 2019-12-02 07:32:25
Hey guys this is my code and what it is doing is going in an loop but what its suppose to do is if the user types in borrow then it will ask the user how much which it does but then they type in a number and it will ask them again would you like to borrow or sell and it is in an infinite loop. case 3: do{ System.out.println("What would you like to do? Please type borrow to borrow money or sell to sell assets: "); b = scan.nextLine().toLowerCase(); if(b.equals("borrow")){ System.out.print("how much would you like to borrow Remmber if you go over 50000 debt its game over."); try { input = scan

Putting loop inside C macro

和自甴很熟 提交于 2019-12-02 06:31:08
问题 I'm looking for a way to convert the following function structure to a macro. I know, it's a silly and pointless example, but it illustrates the point since I cannot give out my actual source code. int foo(int x, int y) { do { --x; ++y; }while(x > y); return x * y; //note that x and y have changed values here. } So that I can call the function in main or some other function like so: int next_x = foo(x,y); I cannot seem to get the syntax 100% correct here. This is my poor attempt: #define FOO

Exception handling with a do-while loop in Java

一世执手 提交于 2019-12-02 05:20:26
The algorithm should take in 3 integers to an ArrayList. If the input is not an integer, then there should be a prompt. When I execute my code the catch clause is executed, but the program runs into a infinite loop. Could someone guide me into the right direction, I appreciate the help. :-D package chapter_08; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class IntegerList { static List<Integer> numbers = new ArrayList<Integer>(); public static void main(String[] args) { Scanner input = new Scanner(System.in); int counter = 1; int inputNum; do { System.out

Understanding do-while loop

喜你入骨 提交于 2019-12-02 05:17:16
问题 I'm doing the oracle certified associate Java SE7 Programmer practice exams (the book) and came across a question, and I don't understand the answer even with the explanation. Here's the explanation and the code: It will print 3. The loop body is executed twice and the program will print 3. I don't understand how the loop body is executed twice, maybe I don't understand what the b=!b means. Can someone explain please? class TestClass { public static void main(String args[]){ boolean b = false

scanf() in a do-while loop causes infinite loop even with test of input

牧云@^-^@ 提交于 2019-12-02 04:15:57
I have this: float a, b, c; int isInt; do { puts("Please enter three positive lengths:"); isInt = scanf(" %f %f %f", &a, &b, &c); } while(a <= 0 || b <= 0 || c <= 0 || isInt != 3); Let's say my input is 1 2 f , then a == 1 , b == 2 , and c == 0 . Even more, isInt == 2 , which makes me wonder why it doesn't loop normally again...? The problem in your code is what you do (more specifically, what you do not do) when scanf(...) returns a number other than 3. Currently, your code continues asking for input and loops on, without taking anything from the input buffer. This, of course, makes your

Foreach loop (or do while maybe?) - Want to return only one record depending on page

烂漫一生 提交于 2019-12-02 02:53:38
问题 Solved this with edited code below. Thanks to all who helped! I have two records in my db. Each record has 6 fields (challengeId, partnerName, code, challengeTitle, description, image_url). I select a given partnerName from my parent page view to go to my child page view. I was using a foreach loop and having problems. I have now taken out my foreach loop and replaced it with <?php $challengename = $this->challengenames[$k] = current($this->challengenames); ?> but now cannot get the child

Understanding do-while loop

雨燕双飞 提交于 2019-12-02 01:06:01
I'm doing the oracle certified associate Java SE7 Programmer practice exams (the book) and came across a question, and I don't understand the answer even with the explanation. Here's the explanation and the code: It will print 3. The loop body is executed twice and the program will print 3. I don't understand how the loop body is executed twice, maybe I don't understand what the b=!b means. Can someone explain please? class TestClass { public static void main(String args[]){ boolean b = false; int i = 1; do{ i + + ; } while (b = !b); System.out.println(i); } } b = !b is an assignment which

Foreach loop (or do while maybe?) - Want to return only one record depending on page

丶灬走出姿态 提交于 2019-12-02 01:00:51
Solved this with edited code below. Thanks to all who helped! I have two records in my db. Each record has 6 fields (challengeId, partnerName, code, challengeTitle, description, image_url). I select a given partnerName from my parent page view to go to my child page view. I was using a foreach loop and having problems. I have now taken out my foreach loop and replaced it with <?php $challengename = $this->challengenames[$k] = current($this->challengenames); ?> but now cannot get the child page to display the values for challengeTitle that correspond to 'partnerName' I chose on my parent page.