while-loop

What for (;;) and while(); mean in C [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-01 07:24:43
问题 This question already has answers here : What does “for(;;)” mean? (5 answers) Closed last year . I am looking at some example codes and I saw someone did this for (;;) { // ... } Is that equivalent to while(1) { } ? And what does while(condition); do? I don't get the reason behind putting ';' instead of {} 回答1: yes, for(;;){} is an infinite loop 回答2: And what does while(condition); do? I don't get the reason behind putting ';' instead of {} Well, your question is what happens if you put or

How to record sound in buffer using ALSA

谁都会走 提交于 2019-12-01 06:33:37
问题 I'm begining to learn linux and ALSA and I was wondering if there is a way to store the sound I record from a microphone a directly to the buffer. I read here http://www.linuxjournal.com/article/6735?page=0,2 how to make my recording program. But what I need is a little more complex. I need to record sound untill I hit a key. The reason I need this is because I'm messing with a RaspberryPI(debian on it) and to see if I could turn it into a sound monitoring/detecting device. My main problem is

How to break from a loop after the condition is met

孤者浪人 提交于 2019-12-01 06:28:07
问题 Hi I have been trying for the past hour to break from this loop and continue since already met my condition once. My application pretty much reads a serie of lines and analyzes it and then prints the variable stated. An example of how the lines look like (the . are not included): 10 c = 9+3 20 a = c+1 30 print c 40 goto 20 50 end It does everything right, when it gets to line 40 goes to line 20 as expected, but i want it to go to line 50 since already went to line 40 once. Here is my code for

How to stop a while loop

浪尽此生 提交于 2019-12-01 06:07:01
问题 This while loop never ends. For example, when i enter a wrong password it will keep on going to the "incorrect password" part over and over again. Logo(); inFile.open("UsernamePassword.txt"); if (!inFile) cout << "Unable to Open File"; else { cout << endl << endl << endl; cout << " Please enter username: "; cin >> user; cout << " Please enter password: "; cin >> pass; while (username != user) { inFile >> username >> password; if (user == username && pass == password) { cout << endl; cout <<

Loops in C - for() or while() - which is BEST?

空扰寡人 提交于 2019-12-01 05:14:15
for() or while() - which is BEST? for (i=1; i<a; i++) /* do something */ OR i=1; while (i<a) { /* do something */ i++; } The first is the idiomatic way; it is what most C coders will expect to see. However, I should note that most people will also expect to see for(i = 0; i < a; i++) Note that the loop starts at zero. This will do something a times. If you're going to write a while loop that is equivalent to a for loop as above I strongly encourage you to write it as a for loop. Again, it is what C coders expect to see. Further, as a for loop it is easier to read as everything (initialization,

Using FOR loop in VHDL with a variable

╄→尐↘猪︶ㄣ 提交于 2019-12-01 04:59:38
问题 Is there any possible way to create a for loop in the form: for i in 0 to some_var loop // blah,blah end loop; If not, is there any alternative way to create the same loop? Since While loops allows to use variable as the limit, but they are not synthesizeable in my project. Thanks in Advance, Bojan Matovski 回答1: The variable works just fine for testbench applications. For synthesis you can get the same effect by using a static range and an exit condition. Set the range to be the maximum you

How can I remove the last comma from a loop in C++ in a simple way?

痴心易碎 提交于 2019-12-01 04:48:16
问题 This program is for printing prime numbers till the input given and separating every prime number with a comma. void main(){ int N, counter=0, isPrime; int k, j; cout << "Enter maximum range: "; cin >> N; for (j=2; j<=N; j++){ isPrime = 0; k = 2; while (k<j){ if (j%k==0){ isPrime++; } k++; } if (isPrime==0){ if (k==N){ cout << j; } else{ cout << j << ","; } counter++; } } cout << endl; system("pause"); } It is only removing the last comma for prime number inputs, not for any other input. How

How to get next minimum date that is not within 30 days and use as reference point in SQL?

一世执手 提交于 2019-12-01 04:44:18
问题 I have a subset of records that look like this: ID DATE A 2015-09-01 A 2015-10-03 A 2015-10-10 B 2015-09-01 B 2015-09-10 B 2015-10-03 ... For each ID the first minimum date is the first index record. Now I need to exclude cases within 30 days of the index record, and any record with a date greater than 30 days becomes another index record. For example, for ID A, 2015-09-01 and 2015-10-03 are both index records and would be retained since they are more than 30 days apart. 2015-10-10 would be

Why do i have to input EOF 3 times when using fgets?

大憨熊 提交于 2019-12-01 01:29:48
So basically I want to copy everything i write to stdin (including newline char) to string for hash purposes. I managed to accomplish that and made small code to represent my problem. #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFFERSIZE 10000 int main() { char *myStr = calloc(1,1); char buffer[BUFFERSIZE]; while( fgets(buffer, BUFFERSIZE , stdin) != NULL ){ myStr = realloc(myStr, strlen(myStr)+1+strlen(buffer) ); strcat( myStr, buffer ); } printf("\n%s\n",myStr); } everything works when I enter some text then press ENTER and after I call EOF. But when I start program

R: Distributing an amount as evenly as possible II

笑着哭i 提交于 2019-12-01 01:19:08
We have have a certain amount e.g. 300 units. This amount should be as evenly as possible distributed over 40 "slots" or "bins". It would be easy if each slot would be the same - so it would be 7,5 at each slot. However, the slots vary in size and we cannot "fill in" there more than its "size" allows for e.g. if its only 4. What we cannot "fill in" more than 4. Hence, we have to distribute more over the other ones. Lets assume that there is another limitation: A general filling in limit of e.g. 5. That would mean that even if we have enough size in the slot to fill in say 12 and enough units