while-loop

while loop won't terminate with a logical conditional

懵懂的女人 提交于 2019-12-02 11:19:39
This loop won't terminate if I add an OR conditional statement. If one is false, then it should terminate. //global var int x = 100; char *n= malloc (64); void add(void) { do { printf("Would you like to add 1? (y/n) "); fgets(n, 64, stdin); //removes newline n[strlen(n)-1] = '\0'; x++; } //if I add || (x!=100) it keeps looping me even if I press "n" //otherwise without it it works fine while((strncmp(n, "n", 1) != 0) || x!=100 ); free(n); } At the bottom of your loop, you're doing x++ . Once you hit the while condition, x == 101 , so your loop never terminates, x never equals 100 when the

Why is the following program stuck in a loop?

旧时模样 提交于 2019-12-02 11:06:12
I wrote a solution to an exercise that asks to write a program that acts as a simple "printing" calculator, and also detects division by zero and checks for unknown operators. The program works as intended when expected operators are entered. For example: "100 S" prints "= 100.000000" "2 /" prints "= 50.000000" "10 *" prints "= 500.000000" It also detects division by zero and unknown operators. However, when I enter operators in wrong order, like this: "/ 2" or "* 10", the program is stuck in a loop. How do I fix this bug so that when the operators are entered in wrong order, it just prints

While loop executes only once

孤人 提交于 2019-12-02 10:36:10
I have a hard time figuring out why the while loop won't actually loop. It runs through once and stops. import java.util.*; public class mileskm { public static void main(String[] args) { Scanner inp = new Scanner(System.in); boolean continue1 = true; while (continue1 == true) { System.out.println("Would you like to convert m-km or km-m(km for m-km and m for km-m)"); String convert = inp.nextLine(); if (convert.equalsIgnoreCase("km")) { System.out.println("Enter the mileage to be converted."); double num = inp.nextDouble(); double con = num * 1.60934; System.out.println(con); continue1 = true;

Combine text from two files, output to another

淺唱寂寞╮ 提交于 2019-12-02 10:25:31
问题 i'm having a bit of a problem and i've been searching allll day. this is my first Unix class don't be to harsh. so this may sound fairly simple, but i can't get it I have two text files file1 David 734.838.9801 Roberto‭ ‬313.123.4567 Sally‭ ‬248.344.5576 Mary‭ ‬313.449.1390 Ted‭ ‬248.496.2207 Alice‭ ‬616.556.4458 Frank‭ ‬634.296.1259 file2 Roberto Tuesday‭ ‬2 Sally Monday‭ ‬8 Ted Sunday‭ ‬16 Alice Wednesday‭ ‬23 David Thursday‭ ‬10 Mary Saturday‭ ‬14 Frank Friday‭ ‬15 I am trying to write a

Find the first missing number in a sequence of numbers

别来无恙 提交于 2019-12-02 10:23:16
It's the second day I try to find a solution for this problem. I have an array. $datas; $datas[0]; // 8000 $datas[1]; // 8001 $datas[2]; // 8003 $datas[3]; // 8004 I have to find the first missing number starting from 8000 in this case it's 8002. My idea is to do somethig like this: $datas[0] +1 = $datas[1] if it's true port it's not free and I have to check the next one, if it's false it's the first free number. I know it's not a correct syntax but I have some problems writing it in the right way. You can use array_diff to find the missing value. I create a new array with values from min of

Compiling c code with bool without using c99 standard

拟墨画扇 提交于 2019-12-02 10:19:16
I've tried to compile a code using a bool variable in C and I've included the stdbool header but when I compiled it I didn't specify that I want to compile it with the c99 standard (so it was compiled with ANSI C standard) but it worked anyway. I was wondering why is that ? Here's the code : #include <stdio.h> #include <stdbool.h> int main() { char name[20]; printf("What's your name ? "); gets(name); printf("Nice to meet you %s.\n", name); bool exit = false; char c; printf("Do you wish to exit the program ? (Y/N) "); while (!exit) { c = getchar(); if (c == '\n') { continue; } printf("Do you

Cin in a while loop

邮差的信 提交于 2019-12-02 10:17:35
问题 So, I've looked around and haven't been able to figure out what's going on with cin during my While loop. I'm working through the book C++ Primer (5th edition) and I noticed that during one of the exercises I couldn't use cin to grab strings without it not terminating the while loop. I fixed this issue by just using getline(). The goal of the current exercise is to ask for user input from the values of 0 - 15, and converting that number into it's "Hex Equivelant" (Where 0 = 1, 1 = 2, 2 = 3, .

Testing while for multiple conditions (C language)

心不动则不痛 提交于 2019-12-02 10:09:36
I have to create a menu wherein if the input is not valid. It should keep asking for a valid input. I've written it below (in C) #include <stdio.h> int main() { int input = 0; printf("What would you like to do? \n 1 (Subtraction) \n 2 (Comparison) \n 3 (Odd/Even) \n 4 (Exit) \n "); scanf_s("%d", &input); while (input != 1 || input != 2 || input != 3|| input != 4) { printf("Please enter a valid option \n"); scanf_s("%d", &input); } // At this point, I think it should keep testing variable input and if it's not either 1 or 2 or 3 or 4. It would keep looping. But what's happening is it loops even

Python: While, if, else counting

我的未来我决定 提交于 2019-12-02 09:53:15
I'm having a bit of an issue with a while/if statement. I have a list of values, normally these values will be strings, but sometimes it can return None. Here are two of my attempts: x = ['One','Two','Three',None,None] New = [] count=0 for y in x: while isinstance(y,str): New.append(y) count+=1 break else: count+=1 New.append('New - '+str(count)) print New,count >>> The list repeats several times New = [] for y in x: count=0 if y is not None: New.append(y) count+=1 else: count+=1 New.append('New - '+str(count)) >>>['One','Two','Three','New - 1','New - 1'] I would like the output to be: ['One',

GUI Freezes During While Loop in Java

亡梦爱人 提交于 2019-12-02 09:42:11
http://i.stack.imgur.com/XvHm5.png When I click the On button it should start spamming 1's across the JTextField. Instead The entire GUI freezes, including the close button. I am using a while loop that will stop when you click the jButton again. Because the jButton is frozen i cannot stop this program. I have read that Threads and Multithreading would help but all of the tutorials are too complex for me to understand. They say to put the loop and GUI on different threads. I want to understand this to fix another program that I am working on which is much more complex. Thats why I made a